| @@ -25,22 +25,39 @@ namespace Diligent.WebAPI.Business.Services | |||
| _configuration = configuration; | |||
| } | |||
| public async Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(int userId) | |||
| public async Task<List<CategoriesNamesResponse>> GetRootCategories(int userId) | |||
| { | |||
| var user = await _userManager.FindByIdAsync(userId.ToString()); | |||
| var role = (await _userManager.GetRolesAsync(user))[0]; | |||
| if(role == "SuperAdmin") return _mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync()); | |||
| else | |||
| { | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var sql = @"SELECT C.Id, C.Name FROM AspNetUsers AS U INNER JOIN UserCategories AS UC ON U.Id = UC.UserId | |||
| INNER JOIN Categories AS C ON UC.CategoryId = C.Id WHERE U.Id = @UserId"; | |||
| var categories = await connection.QueryAsync<CategoriesNamesResponse>(sql, new { UserId = userId }); | |||
| var categoriesList = categories.ToList(); | |||
| return categoriesList; | |||
| } | |||
| if(role == "SuperAdmin") | |||
| return _mapper.Map<List<CategoriesNamesResponse>> | |||
| (await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync()); | |||
| var userCategories = await _context.UserCategories | |||
| .Where(k => k.UserId == userId && k.Category.ParentCategory == null) | |||
| .Include(t => t.Category) | |||
| .ToListAsync(); | |||
| return GetCategoriesFromUserCategories(userCategories); | |||
| } | |||
| public async Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId,int userId) | |||
| { | |||
| var user = await _userManager.FindByIdAsync(userId.ToString()); | |||
| var role = (await _userManager.GetRolesAsync(user))[0]; | |||
| if (role == "SuperAdmin") | |||
| return _mapper.Map<List<CategoriesNamesResponse>>( | |||
| await _context.Categories | |||
| .Where(k => k.ParentCategory != null && k.ParentCategory.Id == parentCategoryId) | |||
| .ToListAsync()); | |||
| var userCategories = await _context.UserCategories | |||
| .Where(k => k.UserId == userId && k.Category != null && k.Category.ParentCategory != null && k.Category.ParentCategory.Id == parentCategoryId) | |||
| .Include(t => t.Category) | |||
| .ToListAsync(); | |||
| return GetCategoriesFromUserCategories(userCategories); | |||
| } | |||
| public async Task<Category> GetCategoryEntityById(int id) => | |||
| @@ -74,5 +91,16 @@ namespace Diligent.WebAPI.Business.Services | |||
| return result; | |||
| } | |||
| private List<CategoriesNamesResponse> GetCategoriesFromUserCategories(List<UserCategories> userCategories) | |||
| { | |||
| var res = new List<CategoriesNamesResponse>(); | |||
| for (int i = 0; i < userCategories.Count; i++) | |||
| { | |||
| res.Add(_mapper.Map<CategoriesNamesResponse>(userCategories[i].Category)); | |||
| } | |||
| return res; | |||
| } | |||
| } | |||
| } | |||
| @@ -9,7 +9,8 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| { | |||
| public interface ICategoryService | |||
| { | |||
| Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(int userId); | |||
| Task<List<CategoriesNamesResponse>> GetRootCategories(int userId); | |||
| Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId,int userId); | |||
| Task<Category> GetCategoryEntityById(int id); | |||
| Task<List<IsGrantedCategory>> GetCategories(int userId); | |||
| } | |||
| @@ -16,5 +16,6 @@ namespace Diligent.WebAPI.Data.Entities | |||
| public List<FileEntity> Files { get; set; } | |||
| public List<UserCategories> UserCategories { get; set; } | |||
| public Category? ParentCategory { get; set; } = null; | |||
| } | |||
| } | |||
| @@ -11,6 +11,7 @@ namespace Diligent.WebAPI.Data.Entities | |||
| public int Id { get; set; } | |||
| public int UserId { get; set; } | |||
| public Category Category { get; set; } | |||
| public int CategoryId { get; set; } | |||
| } | |||
| @@ -0,0 +1,45 @@ | |||
| using Microsoft.EntityFrameworkCore.Migrations; | |||
| #nullable disable | |||
| namespace Diligent.WebAPI.Data.Migrations | |||
| { | |||
| public partial class ChangedCategoryEntity : Migration | |||
| { | |||
| protected override void Up(MigrationBuilder migrationBuilder) | |||
| { | |||
| migrationBuilder.AddColumn<int>( | |||
| name: "ParentCategoryId", | |||
| table: "Categories", | |||
| type: "int", | |||
| nullable: true); | |||
| migrationBuilder.CreateIndex( | |||
| name: "IX_Categories_ParentCategoryId", | |||
| table: "Categories", | |||
| column: "ParentCategoryId"); | |||
| migrationBuilder.AddForeignKey( | |||
| name: "FK_Categories_Categories_ParentCategoryId", | |||
| table: "Categories", | |||
| column: "ParentCategoryId", | |||
| principalTable: "Categories", | |||
| principalColumn: "Id"); | |||
| } | |||
| protected override void Down(MigrationBuilder migrationBuilder) | |||
| { | |||
| migrationBuilder.DropForeignKey( | |||
| name: "FK_Categories_Categories_ParentCategoryId", | |||
| table: "Categories"); | |||
| migrationBuilder.DropIndex( | |||
| name: "IX_Categories_ParentCategoryId", | |||
| table: "Categories"); | |||
| migrationBuilder.DropColumn( | |||
| name: "ParentCategoryId", | |||
| table: "Categories"); | |||
| } | |||
| } | |||
| } | |||
| @@ -213,8 +213,13 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| .IsRequired() | |||
| .HasColumnType("nvarchar(max)"); | |||
| b.Property<int?>("ParentCategoryId") | |||
| .HasColumnType("int"); | |||
| b.HasKey("Id"); | |||
| b.HasIndex("ParentCategoryId"); | |||
| b.ToTable("Categories"); | |||
| }); | |||
| @@ -977,6 +982,15 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| .IsRequired(); | |||
| }); | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Category", b => | |||
| { | |||
| b.HasOne("Diligent.WebAPI.Data.Entities.Category", "ParentCategory") | |||
| .WithMany() | |||
| .HasForeignKey("ParentCategoryId"); | |||
| b.Navigation("ParentCategory"); | |||
| }); | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Comment", b => | |||
| { | |||
| b.HasOne("Diligent.WebAPI.Data.Entities.Applicant", "Applicant") | |||
| @@ -14,11 +14,18 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| _categoryService = categoryService; | |||
| } | |||
| [HttpGet("names")] | |||
| public async Task<IActionResult> GetCategoriesNames() | |||
| [HttpGet("root-categories")] | |||
| public async Task<IActionResult> GetRootCategories() | |||
| { | |||
| User? user = (User?)HttpContext.Items["User"]; | |||
| return Ok(await _categoryService.GetCategoriesNamesAsync(user.Id)); | |||
| return Ok(await _categoryService.GetRootCategories(user.Id)); | |||
| } | |||
| [HttpGet("child-categories")] | |||
| public async Task<IActionResult> GetChildCategories(int parentCategoryId) | |||
| { | |||
| User? user = (User?)HttpContext.Items["User"]; | |||
| return Ok(await _categoryService.GetChildCategories(parentCategoryId, user.Id)); | |||
| } | |||
| [HttpGet("granted-categories")] | |||