| @@ -15,6 +15,7 @@ namespace Diligent.WebAPI.Business.MappingProfiles | |||
| #region DTO to Model | |||
| CreateMap<Category, CategoryResponse>(); | |||
| CreateMap<Category, CategoriesNamesResponse>(); | |||
| CreateMap<CreateCategoryDto, Category>(); | |||
| #endregion | |||
| #region Model to DTO | |||
| @@ -209,5 +209,17 @@ namespace Diligent.WebAPI.Business.Services | |||
| return response; | |||
| } | |||
| public async Task CreateAsync(CreateCategoryDto createCategoryDto) | |||
| { | |||
| Category category = null; | |||
| if(createCategoryDto.ParentId != null) | |||
| { | |||
| category = await _context.Categories.Where(x => x.Id == createCategoryDto.ParentId).FirstOrDefaultAsync(); | |||
| } | |||
| await _context.Categories.AddAsync(new Category { Name = createCategoryDto.Name, ParentCategory = category}); | |||
| await _context.SaveChangesAsync(); | |||
| } | |||
| } | |||
| } | |||
| @@ -13,5 +13,6 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| Task<Category> GetCategoryEntityById(int? id); | |||
| Task<List<IsGrantedCategory>> GetCategories(int userId); | |||
| Task<List<TreeViewCategoryResponse>> GetAllCategories(int userId); | |||
| Task CreateAsync(CreateCategoryDto createCategoryDto); | |||
| } | |||
| } | |||
| @@ -0,0 +1,15 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Contracts.DTOs.Categories | |||
| { | |||
| public class CreateCategoryDto | |||
| { | |||
| public string Name { get; set; } | |||
| public int? ParentId { get; set; } | |||
| } | |||
| } | |||
| @@ -13,9 +13,9 @@ namespace Diligent.WebAPI.Data.Entities | |||
| public string Name { get; set; } | |||
| public List<FileEntity> Files { get; set; } | |||
| public List<FileEntity> Files { get; set; } = new(); | |||
| public List<UserCategories> UserCategories { get; set; } | |||
| public List<UserCategories> UserCategories { get; set; } = new(); | |||
| public Category? ParentCategory { get; set; } = null; | |||
| } | |||
| } | |||
| @@ -1,4 +1,5 @@ | |||
| using Microsoft.AspNetCore.Mvc; | |||
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||
| using Microsoft.AspNetCore.Mvc; | |||
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| { | |||
| @@ -18,7 +19,7 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| public async Task<IActionResult> GetRootCategories(int parentCategoryId = -1) | |||
| { | |||
| User? user = (User?)HttpContext.Items["User"]; | |||
| return Ok(await _categoryService.GetRootCategories(user.Id,parentCategoryId)); | |||
| return Ok(await _categoryService.GetRootCategories(user.Id, parentCategoryId)); | |||
| } | |||
| [HttpGet("granted-categories")] | |||
| @@ -26,11 +27,14 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| Ok(await _categoryService.GetCategories(userId)); | |||
| [HttpGet("all-categories")] | |||
| public async Task<IActionResult> GetAllCategories() | |||
| public async Task<IActionResult> GetAllCategories() => | |||
| Ok(await _categoryService.GetAllCategories()); | |||
| [HttpPost] | |||
| public async Task<IActionResult> CreateCategory([FromBody] CreateCategoryDto request) | |||
| { | |||
| User? user = (User?)HttpContext.Items["User"]; | |||
| return Ok(await _categoryService.GetAllCategories(user.Id)); | |||
| await _categoryService.CreateAsync(request); | |||
| return StatusCode((int)HttpStatusCode.Created); | |||
| } | |||
| } | |||
| } | |||