Initial commit - ECommerce project
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
using System;
|
||||
using Commerce.Contracts.DTOs;
|
||||
using Commerce.Domain;
|
||||
using Commerce.Domain.Entities;
|
||||
using Generic.Contracts.Generics;
|
||||
using Generic.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Commerce.Core.Services;
|
||||
|
||||
public class CategoryService
|
||||
{
|
||||
|
||||
private readonly Context context;
|
||||
|
||||
public CategoryService(Context context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task CreateCategory(CategoryVM categoryVM)
|
||||
{
|
||||
var category = Category.ToEntity(categoryVM);
|
||||
await context.Categories.AddAsync(category);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task UpdateCategory(CategoryVM categoryVM)
|
||||
{
|
||||
if (categoryVM.Id == null) throw new ArgumentException("Category ID is required for update.");
|
||||
|
||||
var existingCategory = await context.Categories
|
||||
.FirstOrDefaultAsync(x => x.Id == categoryVM.Id);
|
||||
|
||||
if (existingCategory == null) throw new KeyNotFoundException("Category not found.");
|
||||
|
||||
// Update properties
|
||||
existingCategory.Translations = categoryVM.Translations?.Select(t => new CategoryTranslation
|
||||
{
|
||||
Language = t.Language,
|
||||
Info = new CategoryInfo
|
||||
{
|
||||
Name = t.Info.Name,
|
||||
Description = t.Info.Description,
|
||||
TechnicalDetails = t.Info.TechnicalDetails?.Select(td => new Commerce.Domain.Entities.TechnicalDetail { Key = td.Key, Value = td.Value }).ToList() ?? new()
|
||||
}
|
||||
}).ToList() ?? new();
|
||||
|
||||
existingCategory.Photos = categoryVM.Photos ?? Array.Empty<string>();
|
||||
existingCategory.ParentCategoryId = categoryVM.ParentCategoryId;
|
||||
existingCategory.IsBundle = categoryVM.IsBundle;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task RemoveCategory(Guid id)
|
||||
{
|
||||
var category = await context.Categories.FindAsync(id);
|
||||
if (category != null) context.Categories.Remove(category);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<CategoryVM> GetCategory(Guid id)
|
||||
{
|
||||
var category = await context.Categories.FindAsync(id);
|
||||
return category != null ? Category.ToVM(category) : new CategoryVM();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<(List<CategoryVM>, int)> FetchCategory(
|
||||
CategoryFilter categoryFilter,
|
||||
Pagination pagination
|
||||
)
|
||||
{
|
||||
var query = context.Categories.AsQueryable();
|
||||
query = query.ApplyAdvancedFilter(categoryFilter);
|
||||
// query = query.ApplySorting(sortBy);
|
||||
if (categoryFilter.Random ?? false)
|
||||
{
|
||||
query = query.OrderBy(x => Guid.NewGuid());
|
||||
}
|
||||
else
|
||||
{
|
||||
// query = query.ApplySorting(sortBy);
|
||||
}
|
||||
var count = query.Count();
|
||||
query = query.Paginate(pagination);
|
||||
|
||||
var lang = categoryFilter.Language;
|
||||
return (await query.Select(x => Category.ToVM(x, lang)).ToListAsync(), count);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<List<Category>> GetAllCategories(string name = "")
|
||||
{
|
||||
|
||||
var categories = await context.Categories.ToListAsync();
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
categories = categories.Where(x => x.Translations.Any(t => t.Info.Name.Contains(name, StringComparison.OrdinalIgnoreCase))).ToList();
|
||||
return categories;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<Category>> GetAllCategories_Tree(string name = "")
|
||||
{
|
||||
|
||||
var cate = await context.Categories
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
cate = cate.Where(x => x.Translations.Any(t => t.Info.Name.Contains(name, StringComparison.OrdinalIgnoreCase))).ToList();
|
||||
}
|
||||
|
||||
var lista = cate.Where(x => x.ParentCategoryId == null).ToList();
|
||||
Console.WriteLine($"{lista.Count}");
|
||||
|
||||
|
||||
foreach (var cat in lista)
|
||||
{
|
||||
cat.ChildCategories = NewMethod(cate, cat.Id);
|
||||
}
|
||||
|
||||
Console.WriteLine($"{lista.Count}");
|
||||
|
||||
return lista;
|
||||
|
||||
|
||||
|
||||
// if (!string.IsNullOrEmpty(name))
|
||||
// categories = categories.Where(x => x.Name.Contains(name));
|
||||
|
||||
// var lista = await categories.ToListAsync();
|
||||
// return lista;
|
||||
|
||||
}
|
||||
|
||||
private static List<Category> NewMethod(List<Category> categories, Guid parent)
|
||||
{
|
||||
var toreturn = new List<Category>();
|
||||
|
||||
var children = categories.Where(x => x.ParentCategoryId == parent).ToList();
|
||||
|
||||
foreach (var item in children)
|
||||
{
|
||||
item.ChildCategories = NewMethod(categories, item.Id);
|
||||
}
|
||||
|
||||
return children;
|
||||
}
|
||||
|
||||
public async Task<List<Category>> GetChildCategories(Guid categoryId)
|
||||
{
|
||||
var categories = new List<Category>();
|
||||
|
||||
categories = await context.Categories.Where(x => x.ParentCategoryId == categoryId).ToListAsync();
|
||||
|
||||
if (categories.Count != 0)
|
||||
{
|
||||
foreach (var category in categories)
|
||||
{
|
||||
categories.AddRange(await GetChildCategories(categoryId));
|
||||
}
|
||||
}
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
public async Task<List<Category>> GetChildrenList(Guid categoryId)
|
||||
{
|
||||
return await context.Categories.Where(x => x.Id == categoryId)
|
||||
.Include(x => x.ChildCategories)
|
||||
.SelectMany(x => x.ChildCategories)
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// public async Task<List<Category>> GetRandomCategories(int categoriesNeeded, int productsNeeded)
|
||||
// {
|
||||
// var count = await context.Categories.CountAsync();
|
||||
// var categories = new List<Category>();
|
||||
// var random = new Random();
|
||||
// var randoms = new List<int>();
|
||||
// for (int i = 0; i < categoriesNeeded; i++)
|
||||
// {
|
||||
// var newRandom = random.Next(count);
|
||||
// if (!randoms.Contains(newRandom))
|
||||
// {
|
||||
// randoms.Add(newRandom);
|
||||
// categories.Add(await context.Categories
|
||||
// .Skip(random.Next(count))
|
||||
// .Take(1)
|
||||
// .Include(x => x.Products)
|
||||
// .FirstOrDefaultAsync()
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
// return categories;
|
||||
|
||||
// }
|
||||
|
||||
// public async Task<Category> GetCategoryAsync(int id)
|
||||
// {
|
||||
// return await context.Categories.FirstOrDefaultAsync(c => c.Id == id);
|
||||
// }
|
||||
|
||||
// public async Task<List<Category>> GetCategoriesPaginated(Pagination pagination)
|
||||
// {
|
||||
// var query = context.Categories.AsQueryable();
|
||||
// query = query.Skip((pagination.CurrentPage - 1) * pagination.PageSize)
|
||||
// .Take(pagination.PageSize);
|
||||
// return await query.ToListAsync();
|
||||
// }
|
||||
|
||||
// public async Task<List<CategoryVM>> GetRandomCategories(int count = 4)
|
||||
// {
|
||||
// return await context
|
||||
// .Categorys.OrderBy(x => Guid.NewGuid())
|
||||
// .Take(count)
|
||||
// .Select(x => Category.ToVM(x))
|
||||
// .ToListAsync();
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user