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.

CategoryService.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Diligent.WebAPI.Contracts.DTOs.Categories;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Diligent.WebAPI.Business.Services
  8. {
  9. public class CategoryService : ICategoryService
  10. {
  11. private readonly DatabaseContext _context;
  12. private readonly IMapper _mapper;
  13. public CategoryService(DatabaseContext context, IMapper mapper)
  14. {
  15. _context = context;
  16. _mapper = mapper;
  17. }
  18. public async Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync() =>
  19. _mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync());
  20. public async Task<Category> GetCategoryEntityById(int id) =>
  21. await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();
  22. public async Task<List<IsGrantedCategory>> GetCategories(int userId)
  23. {
  24. var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync();
  25. var allCategories = await _context.Categories.ToListAsync();
  26. var result = new List<IsGrantedCategory>();
  27. for (int i = 0; i < allCategories.Count; i++)
  28. {
  29. var newCategory = new IsGrantedCategory
  30. {
  31. Id = allCategories[i].Id,
  32. Name = allCategories[i].Name,
  33. };
  34. bool isGranted = false;
  35. for (int j = 0; j < grantedCategories.Count; j++)
  36. {
  37. if(grantedCategories[j].CategoryId == allCategories[i].Id && userId == grantedCategories[j].UserId)
  38. {
  39. isGranted = true;
  40. break;
  41. }
  42. }
  43. newCategory.IsChecked = isGranted;
  44. result.Add(newCategory);
  45. isGranted = false;
  46. }
  47. return result;
  48. }
  49. }
  50. }