| @@ -1,6 +1,9 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||
| using Dapper; | |||
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||
| using Microsoft.Extensions.Configuration; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Data.SqlClient; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| @@ -11,15 +14,34 @@ namespace Diligent.WebAPI.Business.Services | |||
| { | |||
| private readonly DatabaseContext _context; | |||
| private readonly IMapper _mapper; | |||
| private readonly UserManager<User> _userManager; | |||
| private readonly IConfiguration _configuration; | |||
| public CategoryService(DatabaseContext context, IMapper mapper) | |||
| public CategoryService(DatabaseContext context, IMapper mapper, UserManager<User> userManager, IConfiguration configuration) | |||
| { | |||
| _context = context; | |||
| _mapper = mapper; | |||
| _userManager = userManager; | |||
| _configuration = configuration; | |||
| } | |||
| public async Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync() => | |||
| _mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync()); | |||
| public async Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(int userId) | |||
| { | |||
| var user = await _userManager.FindByIdAsync(userId.ToString()); | |||
| var role = (await _userManager.GetRolesAsync(user))[0]; | |||
| if(role == "SuperAdmin") return _mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync()); | |||
| else | |||
| { | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var sql = @"SELECT C.Id, C.Name FROM AspNetUsers AS U INNER JOIN UserCategories AS UC ON U.Id = UC.UserId | |||
| INNER JOIN Categories AS C ON UC.CategoryId = C.Id WHERE U.Id = @UserId"; | |||
| var categories = await connection.QueryAsync<CategoriesNamesResponse>(sql, new { UserId = 9 }); | |||
| var categoriesList = categories.ToList(); | |||
| return categoriesList; | |||
| } | |||
| } | |||
| public async Task<Category> GetCategoryEntityById(int id) => | |||
| await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); | |||
| @@ -32,7 +32,7 @@ namespace Diligent.WebAPI.Business.Services | |||
| } | |||
| public async Task<List<FileEntityResponse>> GetAllAsync() => | |||
| _mapper.Map<List<FileEntityResponse>>(await _context.Files.Include(x => x.Tags).ToListAsync()); | |||
| _mapper.Map<List<FileEntityResponse>>(await _context.Files.Include(x => x.Tags).Where(x => x.Deleted == false).ToListAsync()); | |||
| public async Task UploadPdfAsync(FileEntity file) | |||
| { | |||
| @@ -43,7 +43,7 @@ namespace Diligent.WebAPI.Business.Services | |||
| public async Task<IEnumerable<FileEntity>> GetAll() | |||
| { | |||
| return await _context.Files.ToListAsync(); | |||
| return await _context.Files.Where(x => x.Deleted == false).ToListAsync(); | |||
| } | |||
| public async Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(String content) | |||
| @@ -51,7 +51,7 @@ namespace Diligent.WebAPI.Business.Services | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var sql = @"select * from Files inner join DocumentOrganizerDocStore on Files.DocumentId = DocumentOrganizerDocStore.stream_id | |||
| where contains(DocumentOrganizerDocStore.file_stream,@content)"; | |||
| where contains(DocumentOrganizerDocStore.file_stream,@content) AND Files.Deleted = 0"; | |||
| var files = await connection.QueryAsync<FileEntity>(sql,new {content=content}); | |||
| return files.ToList(); | |||
| @@ -60,11 +60,11 @@ namespace Diligent.WebAPI.Business.Services | |||
| { | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var sql = @"SELECT Files.Id as FileId, stream_id, DocumentOrganizerDocStore.name as FileName, file_stream, file_type, cached_file_size, Title, Tags.Id as TagId, Tags.Name as TagName, Categories.Id as CategoryId, Categories.Name as CategoryName | |||
| var sql = @"SELECT Files.Id as FileId, Files.Deleted, stream_id, DocumentOrganizerDocStore.name as FileName, file_stream, file_type, cached_file_size, Title, Tags.Id as TagId, Tags.Name as TagName, Categories.Id as CategoryId, Categories.Name as CategoryName | |||
| FROM Files inner join FileEntityTag on Files.Id = FileEntityTag.FilesId | |||
| inner join Tags on FileEntityTag.TagsId = Tags.Id | |||
| inner join Categories on Files.CategoryId = Categories.Id | |||
| inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId" + | |||
| inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0" + | |||
| (filters.Content is null ? "" : $" where contains(DocumentOrganizerDocStore.file_stream,@content);"); | |||
| var files = await connection.QueryAsync<FileFilterReturnDto, TagResponse, CategoryResponse, FileFilterReturnDto>(sql,(file, tag, category) => | |||
| @@ -88,5 +88,26 @@ namespace Diligent.WebAPI.Business.Services | |||
| Total = filtered.Count | |||
| }; | |||
| } | |||
| public async Task<FileEntity> GetFileEntityByIdAsync(Guid id) | |||
| { | |||
| var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync(); | |||
| if (file is null) | |||
| { | |||
| throw new EntityNotFoundException("File not found"); | |||
| } | |||
| return file; | |||
| } | |||
| public async Task DeleteFileAsync(Guid id) | |||
| { | |||
| var file = await GetFileEntityByIdAsync(id); | |||
| file.Deleted= true; | |||
| await _context.SaveChangesAsync(); | |||
| } | |||
| } | |||
| } | |||
| @@ -9,7 +9,7 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| { | |||
| public interface ICategoryService | |||
| { | |||
| Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(); | |||
| Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(int userId); | |||
| Task<Category> GetCategoryEntityById(int id); | |||
| Task<List<IsGrantedCategory>> GetCategories(int userId); | |||
| } | |||
| @@ -15,5 +15,7 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| Task<IEnumerable<FileEntity>> GetAll(); | |||
| Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(string content); | |||
| Task<object> GetAllFiltered(FileFilter filters); | |||
| Task<FileEntity> GetFileEntityByIdAsync(Guid id); | |||
| Task DeleteFileAsync(Guid id); | |||
| } | |||
| } | |||
| @@ -9,9 +9,8 @@ | |||
| private readonly DatabaseContext _databaseContext; | |||
| private readonly IEmailer _emailer; | |||
| private readonly ILogger<UserService> _logger; | |||
| private readonly ICategoryService _categoryService; | |||
| public UserService(IOptions<FrontEndSettings> frontEndSettings, UserManager<User> userManager, IMapper mapper, DatabaseContext databaseContext, IEmailer emailer, ILogger<UserService> logger, ICategoryService categoryService) | |||
| public UserService(IOptions<FrontEndSettings> frontEndSettings, UserManager<User> userManager, IMapper mapper, DatabaseContext databaseContext, IEmailer emailer, ILogger<UserService> logger) | |||
| { | |||
| _frontEndSettings = frontEndSettings.Value; | |||
| _userManager = userManager; | |||
| @@ -19,7 +18,6 @@ | |||
| _databaseContext = databaseContext; | |||
| _emailer = emailer; | |||
| _logger = logger; | |||
| _categoryService = categoryService; | |||
| } | |||
| public async Task<IEnumerable<User?>> GetAll() | |||
| @@ -19,5 +19,7 @@ namespace Diligent.WebAPI.Contracts.DTOs.File | |||
| public string Extension { get; set; } | |||
| public List<TagResponse> Tags { get; set; } | |||
| public bool Deleted { get; set; } | |||
| } | |||
| } | |||
| @@ -17,5 +17,6 @@ namespace Diligent.WebAPI.Contracts.DTOs.Files | |||
| public int cached_file_size { get; set; } | |||
| public List<TagResponse> Tags { get; set; } = new(); | |||
| public CategoryResponse Category { get; set; } | |||
| public bool Deleted { get; set; } | |||
| } | |||
| } | |||
| @@ -21,5 +21,7 @@ namespace Diligent.WebAPI.Data.Entities | |||
| public Category Category { get; set; } | |||
| public List<Tag> Tags { get; set; } | |||
| public bool Deleted { get; set; } = false; | |||
| } | |||
| } | |||
| @@ -0,0 +1,26 @@ | |||
| using Microsoft.EntityFrameworkCore.Migrations; | |||
| #nullable disable | |||
| namespace Diligent.WebAPI.Data.Migrations | |||
| { | |||
| public partial class AddedDeletedFieldToFileEntity : Migration | |||
| { | |||
| protected override void Up(MigrationBuilder migrationBuilder) | |||
| { | |||
| migrationBuilder.AddColumn<bool>( | |||
| name: "Deleted", | |||
| table: "Files", | |||
| type: "bit", | |||
| nullable: false, | |||
| defaultValue: false); | |||
| } | |||
| protected override void Down(MigrationBuilder migrationBuilder) | |||
| { | |||
| migrationBuilder.DropColumn( | |||
| name: "Deleted", | |||
| table: "Files"); | |||
| } | |||
| } | |||
| } | |||
| @@ -260,6 +260,9 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| b.Property<int>("CategoryId") | |||
| .HasColumnType("int"); | |||
| b.Property<bool>("Deleted") | |||
| .HasColumnType("bit"); | |||
| b.Property<Guid>("DocumentId") | |||
| .HasColumnType("uniqueidentifier"); | |||
| @@ -15,8 +15,11 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| } | |||
| [HttpGet("names")] | |||
| public async Task<IActionResult> GetCategoriesNames() => | |||
| Ok(await _categoryService.GetCategoriesNamesAsync()); | |||
| public async Task<IActionResult> GetCategoriesNames() | |||
| { | |||
| User? user = (User?)HttpContext.Items["User"]; | |||
| return Ok(await _categoryService.GetCategoriesNamesAsync(user.Id)); | |||
| } | |||
| [HttpGet("granted-categories")] | |||
| public async Task<IActionResult> GetCategories(int userId) => | |||
| @@ -52,5 +52,12 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title}); | |||
| return Ok(); | |||
| } | |||
| [HttpDelete("delete-file/{id}")] | |||
| public async Task<IActionResult> DeleteFile([FromRoute]Guid id) | |||
| { | |||
| await _fileEntityService.DeleteFileAsync(id); | |||
| return Ok(); | |||
| } | |||
| } | |||
| } | |||