|
|
|
|
|
|
|
|
using Dapper; |
|
|
using Dapper; |
|
|
|
|
|
using Diligent.WebAPI.Business.Services.Interfaces; |
|
|
using Diligent.WebAPI.Contracts.DTOs.Categories; |
|
|
using Diligent.WebAPI.Contracts.DTOs.Categories; |
|
|
using Microsoft.Extensions.Configuration; |
|
|
using Microsoft.Extensions.Configuration; |
|
|
using System; |
|
|
using System; |
|
|
|
|
|
|
|
|
private readonly DatabaseContext _context; |
|
|
private readonly DatabaseContext _context; |
|
|
private readonly IMapper _mapper; |
|
|
private readonly IMapper _mapper; |
|
|
private readonly UserManager<User> _userManager; |
|
|
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; |
|
|
_context = context; |
|
|
_mapper = mapper; |
|
|
_mapper = mapper; |
|
|
_userManager = userManager; |
|
|
_userManager = userManager; |
|
|
|
|
|
_userService = userService; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task<CategoriesParentChild> GetRootCategories(int userId,int parentCategoryId) |
|
|
public async Task<CategoriesParentChild> GetRootCategories(int userId,int parentCategoryId) |
|
|
|
|
|
|
|
|
return response; |
|
|
return response; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task CreateAsync(CreateCategoryDto createCategoryDto) |
|
|
|
|
|
|
|
|
public async Task CreateAsync(CreateCategoryDto createCategoryDto, int userId) |
|
|
{ |
|
|
{ |
|
|
Category category = null; |
|
|
Category category = null; |
|
|
if(createCategoryDto.ParentId != null) |
|
|
if(createCategoryDto.ParentId != null) |
|
|
{ |
|
|
{ |
|
|
category = await _context.Categories.Where(x => x.Id == createCategoryDto.ParentId).FirstOrDefaultAsync(); |
|
|
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 _context.SaveChangesAsync(); |
|
|
|
|
|
|
|
|
|
|
|
await _userService.GrantCategoryToUserAsync(new GrantUserCategoryRequestDto { UserId = userId, Categories = new List<GrantCategoryToUserRequestDto> { new GrantCategoryToUserRequestDto { Id = newCategory.Id, IsChecked = true } } }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |