| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using Diligent.WebAPI.Contracts.DTOs.Categories;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class CategoryService : ICategoryService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
-
- public CategoryService(DatabaseContext context, IMapper mapper)
- {
- _context = context;
- _mapper = mapper;
- }
-
- public async Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync() =>
- _mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync());
-
- public async Task<Category> GetCategoryEntityById(int id) =>
- await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();
-
- public async Task<List<IsGrantedCategory>> GetCategories(int userId)
- {
- var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync();
- var allCategories = await _context.Categories.ToListAsync();
- var result = new List<IsGrantedCategory>();
- for (int i = 0; i < allCategories.Count; i++)
- {
- var newCategory = new IsGrantedCategory
- {
- Id = allCategories[i].Id,
- Name = allCategories[i].Name,
- };
- bool isGranted = false;
- for (int j = 0; j < grantedCategories.Count; j++)
- {
- if(grantedCategories[j].CategoryId == allCategories[i].Id && userId == grantedCategories[j].UserId)
- {
- isGranted = true;
- break;
- }
- }
- newCategory.IsChecked = isGranted;
- result.Add(newCategory);
- isGranted = false;
- }
-
- return result;
- }
- }
- }
|