| #region DTO to Model | #region DTO to Model | ||||
| CreateMap<Category, CategoryResponse>(); | CreateMap<Category, CategoryResponse>(); | ||||
| CreateMap<Category, CategoriesNamesResponse>(); | CreateMap<Category, CategoriesNamesResponse>(); | ||||
| CreateMap<CreateCategoryDto, Category>(); | |||||
| #endregion | #endregion | ||||
| #region Model to DTO | #region Model to DTO |
| return response; | 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(); | |||||
| } | |||||
| } | } | ||||
| } | } |
| Task<Category> GetCategoryEntityById(int? id); | Task<Category> GetCategoryEntityById(int? id); | ||||
| Task<List<IsGrantedCategory>> GetCategories(int userId); | Task<List<IsGrantedCategory>> GetCategories(int userId); | ||||
| Task<List<TreeViewCategoryResponse>> GetAllCategories(int userId); | Task<List<TreeViewCategoryResponse>> GetAllCategories(int userId); | ||||
| Task CreateAsync(CreateCategoryDto createCategoryDto); | |||||
| } | } | ||||
| } | } |
| 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; } | |||||
| } | |||||
| } |
| public string Name { get; set; } | 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; | public Category? ParentCategory { get; set; } = null; | ||||
| } | } | ||||
| } | } |
| using Microsoft.AspNetCore.Mvc; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using Microsoft.AspNetCore.Mvc; | |||||
| namespace Diligent.WebAPI.Host.Controllers.V1 | namespace Diligent.WebAPI.Host.Controllers.V1 | ||||
| { | { | ||||
| public async Task<IActionResult> GetRootCategories(int parentCategoryId = -1) | public async Task<IActionResult> GetRootCategories(int parentCategoryId = -1) | ||||
| { | { | ||||
| User? user = (User?)HttpContext.Items["User"]; | 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")] | [HttpGet("granted-categories")] | ||||
| Ok(await _categoryService.GetCategories(userId)); | Ok(await _categoryService.GetCategories(userId)); | ||||
| [HttpGet("all-categories")] | [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); | |||||
| } | } | ||||
| } | } | ||||
| } | } |