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> GetCategoriesNamesAsync() => _mapper.Map>(await _context.Categories.ToListAsync()); public async Task GetCategoryEntityById(int id) => await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); public async Task> GetCategories(int userId) { var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync(); var allCategories = await _context.Categories.ToListAsync(); var result = new List(); 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; } } }