Просмотр исходного кода

Get categories for specific user

BE_dev
Dzenis Hadzifejzovic 2 лет назад
Родитель
Сommit
e31ff72bcc

+ 29
- 0
Diligent.WebAPI.Business/Services/CategoryService.cs Просмотреть файл

@@ -23,5 +23,34 @@ namespace Diligent.WebAPI.Business.Services

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;
}
}
}

+ 1
- 0
Diligent.WebAPI.Business/Services/Interfaces/ICategoryService.cs Просмотреть файл

@@ -11,5 +11,6 @@ namespace Diligent.WebAPI.Business.Services.Interfaces
{
Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync();
Task<Category> GetCategoryEntityById(int id);
Task<List<IsGrantedCategory>> GetCategories(int userId);
}
}

+ 9
- 0
Diligent.WebAPI.Contracts/DTOs/Categories/IsGrantedCategory.cs Просмотреть файл

@@ -0,0 +1,9 @@
namespace Diligent.WebAPI.Contracts.DTOs.Categories
{
public class IsGrantedCategory
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
}
}

+ 4
- 0
Diligent.WebAPI.Host/Controllers/V1/CategoriesController.cs Просмотреть файл

@@ -17,5 +17,9 @@ namespace Diligent.WebAPI.Host.Controllers.V1
[HttpGet("names")]
public async Task<IActionResult> GetCategoriesNames() =>
Ok(await _categoryService.GetCategoriesNamesAsync());

[HttpGet("granted-categories")]
public async Task<IActionResult> GetCategories(int userId) =>
Ok(await _categoryService.GetCategories(userId));
}
}

Загрузка…
Отмена
Сохранить