using Karsha_Site.Application.Interfaces.Contexts; using Karsha_Site.Common.Dto; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Karsha_Site.Application.Services.Common.Queries.GetMenuItem { public interface IGetMenuItemService { ResultDto> Execute(); } public class GetMenuItemService : IGetMenuItemService { private readonly IDataBaseContext _context; public GetMenuItemService(IDataBaseContext context) { _context = context; } public ResultDto> Execute() { var category = _context.Categories .Include(p => p.SubCategories) //.Where(p=> p.ParentCategoryID == null) .ToList() .Select(p => new MenuItemDto { CatId = p.ID, Name = p.Title, Child = p.SubCategories.ToList().Select(child => new MenuItemDto { CatId = child.ID, Name = child.Title, }).ToList(), }).ToList(); return new ResultDto>() { Data = category, IsSuccess = true, }; } } public class MenuItemDto { public long CatId { get; set; } public string Name { get; set; } public List Child { get; set; } } }