Sfoglia il codice sorgente

Implemented add category

BE_dev
bronjaermin 2 anni fa
parent
commit
764250d7f0

+ 1
- 0
Diligent.WebAPI.Business/MappingProfiles/CategoryMappingProfile.cs Vedi File

#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

+ 12
- 0
Diligent.WebAPI.Business/Services/CategoryService.cs Vedi File



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

+ 1
- 0
Diligent.WebAPI.Business/Services/Interfaces/ICategoryService.cs Vedi File

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

+ 15
- 0
Diligent.WebAPI.Contracts/DTOs/Categories/CreateCategoryDto.cs Vedi File

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

+ 2
- 2
Diligent.WebAPI.Data/Entities/Category.cs Vedi File



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

+ 10
- 6
Diligent.WebAPI.Host/Controllers/V1/CategoriesController.cs Vedi File

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

Loading…
Annulla
Salva