| @@ -1,4 +1,5 @@ | |||
| using Dapper; | |||
| using Diligent.WebAPI.Business.Services.Interfaces; | |||
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||
| using Microsoft.Extensions.Configuration; | |||
| using System; | |||
| @@ -15,12 +16,14 @@ namespace Diligent.WebAPI.Business.Services | |||
| private readonly DatabaseContext _context; | |||
| private readonly IMapper _mapper; | |||
| private readonly UserManager<User> _userManager; | |||
| private readonly IUserService _userService; | |||
| public CategoryService(DatabaseContext context, IMapper mapper, UserManager<User> userManager) | |||
| public CategoryService(DatabaseContext context, IMapper mapper, UserManager<User> userManager, IUserService userService) | |||
| { | |||
| _context = context; | |||
| _mapper = mapper; | |||
| _userManager = userManager; | |||
| _userService = userService; | |||
| } | |||
| public async Task<CategoriesParentChild> GetRootCategories(int userId,int parentCategoryId) | |||
| @@ -210,16 +213,21 @@ namespace Diligent.WebAPI.Business.Services | |||
| return response; | |||
| } | |||
| public async Task CreateAsync(CreateCategoryDto createCategoryDto) | |||
| public async Task CreateAsync(CreateCategoryDto createCategoryDto, int userId) | |||
| { | |||
| 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}); | |||
| var newCategory = new Category { Name = createCategoryDto.Name, ParentCategory = category }; | |||
| await _context.Categories.AddAsync(newCategory); | |||
| await _context.SaveChangesAsync(); | |||
| await _userService.GrantCategoryToUserAsync(new GrantUserCategoryRequestDto { UserId = userId, Categories = new List<GrantCategoryToUserRequestDto> { new GrantCategoryToUserRequestDto { Id = newCategory.Id, IsChecked = true } } }); | |||
| } | |||
| } | |||
| } | |||
| @@ -13,6 +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); | |||
| Task CreateAsync(CreateCategoryDto createCategoryDto, int userId); | |||
| } | |||
| } | |||
| @@ -9,7 +9,7 @@ namespace Diligent.WebAPI.Data | |||
| { | |||
| public class DataSeeder | |||
| { | |||
| public static async void Seed(IServiceProvider serviceProvider) | |||
| public static async void SeedUser(IServiceProvider serviceProvider) | |||
| { | |||
| using var scope = serviceProvider.CreateScope(); | |||
| var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>(); | |||
| @@ -31,5 +31,41 @@ namespace Diligent.WebAPI.Data | |||
| await databaseContext.SaveChangesAsync(); | |||
| } | |||
| } | |||
| public static async void SeedTags(IServiceProvider serviceProvider) | |||
| { | |||
| using var scope = serviceProvider.CreateScope(); | |||
| var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>(); | |||
| if(!databaseContext.Tags.Any()) | |||
| { | |||
| var tags = new List<Tag> | |||
| { | |||
| new Tag | |||
| { | |||
| Name = "T1" | |||
| }, | |||
| new Tag | |||
| { | |||
| Name = "T2" | |||
| }, | |||
| new Tag | |||
| { | |||
| Name = "T3" | |||
| }, | |||
| new Tag | |||
| { | |||
| Name = "T4" | |||
| }, | |||
| new Tag | |||
| { | |||
| Name = "T5" | |||
| } | |||
| }; | |||
| await databaseContext.Tags.AddRangeAsync(tags); | |||
| await databaseContext.SaveChangesAsync(); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -12,6 +12,6 @@ namespace Diligent.WebAPI.Data.Entities | |||
| public string Name { get; set; } | |||
| public List<FileEntity> Files { get; set; } | |||
| public List<FileEntity> Files { get; set; } = new(); | |||
| } | |||
| } | |||
| @@ -36,7 +36,8 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| [HttpPost] | |||
| public async Task<IActionResult> CreateCategory([FromBody] CreateCategoryDto request) | |||
| { | |||
| await _categoryService.CreateAsync(request); | |||
| User? user = (User?)HttpContext.Items["User"]; | |||
| await _categoryService.CreateAsync(request, user.Id); | |||
| return StatusCode((int)HttpStatusCode.Created); | |||
| } | |||
| } | |||
| @@ -33,6 +33,7 @@ public static class DataConfigurationExtension | |||
| } | |||
| } | |||
| DataSeeder.Seed(app.Services); | |||
| DataSeeder.SeedUser(app.Services); | |||
| DataSeeder.SeedTags(app.Services); | |||
| } | |||
| } | |||