浏览代码

Get categories for specific user

BE_dev
Dzenis Hadzifejzovic 3 年前
父节点
当前提交
e31ff72bcc

+ 29
- 0
Diligent.WebAPI.Business/Services/CategoryService.cs 查看文件



public async Task<Category> GetCategoryEntityById(int id) => public async Task<Category> GetCategoryEntityById(int id) =>
await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); 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 查看文件

{ {
Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(); Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync();
Task<Category> GetCategoryEntityById(int id); Task<Category> GetCategoryEntityById(int id);
Task<List<IsGrantedCategory>> GetCategories(int userId);
} }
} }

+ 9
- 0
Diligent.WebAPI.Contracts/DTOs/Categories/IsGrantedCategory.cs 查看文件

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 查看文件

[HttpGet("names")] [HttpGet("names")]
public async Task<IActionResult> GetCategoriesNames() => public async Task<IActionResult> GetCategoriesNames() =>
Ok(await _categoryService.GetCategoriesNamesAsync()); Ok(await _categoryService.GetCategoriesNamesAsync());

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

正在加载...
取消
保存