You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IGetMenuItemService.cs 1.6KB

2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Karsha_Site.Application.Interfaces.Contexts;
  2. using Karsha_Site.Common.Dto;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.EntityFrameworkCore.Storage;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Karsha_Site.Application.Services.Common.Queries.GetMenuItem
  11. {
  12. public interface IGetMenuItemService
  13. {
  14. ResultDto<List<MenuItemDto>> Execute();
  15. }
  16. public class GetMenuItemService : IGetMenuItemService
  17. {
  18. private readonly IDataBaseContext _context;
  19. public GetMenuItemService(IDataBaseContext context)
  20. {
  21. _context = context;
  22. }
  23. public ResultDto<List<MenuItemDto>> Execute()
  24. {
  25. var category = _context.Categories
  26. .Include(p => p.SubCategories)
  27. //.Where(p=> p.ParentCategoryID == null)
  28. .ToList()
  29. .Select(p => new MenuItemDto
  30. {
  31. CatId = p.ID,
  32. Name = p.Title,
  33. Child = p.SubCategories.ToList().Select(child => new MenuItemDto
  34. {
  35. CatId = child.ID,
  36. Name = child.Title,
  37. }).ToList(),
  38. }).ToList();
  39. return new ResultDto<List<MenuItemDto>>()
  40. {
  41. Data = category,
  42. IsSuccess = true,
  43. };
  44. }
  45. }
  46. public class MenuItemDto
  47. {
  48. public long CatId { get; set; }
  49. public string Name { get; set; }
  50. public List<MenuItemDto> Child { get; set; }
  51. }
  52. }