| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using Dapper; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using Microsoft.Extensions.Configuration; | |||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Data.SqlClient; | |||||
| using System.Linq; | using System.Linq; | ||||
| using System.Text; | using System.Text; | ||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| { | { | ||||
| private readonly DatabaseContext _context; | private readonly DatabaseContext _context; | ||||
| private readonly IMapper _mapper; | 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; | _context = context; | ||||
| _mapper = mapper; | _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) => | public async Task<Category> GetCategoryEntityById(int id) => | ||||
| await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); | await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); |
| } | } | ||||
| public async Task<List<FileEntityResponse>> GetAllAsync() => | 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) | public async Task UploadPdfAsync(FileEntity file) | ||||
| { | { | ||||
| public async Task<IEnumerable<FileEntity>> GetAll() | 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) | public async Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(String content) | ||||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | ||||
| var sql = @"select * from Files inner join DocumentOrganizerDocStore on Files.DocumentId = DocumentOrganizerDocStore.stream_id | 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}); | var files = await connection.QueryAsync<FileEntity>(sql,new {content=content}); | ||||
| return files.ToList(); | return files.ToList(); | ||||
| { | { | ||||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | 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 | FROM Files inner join FileEntityTag on Files.Id = FileEntityTag.FilesId | ||||
| inner join Tags on FileEntityTag.TagsId = Tags.Id | inner join Tags on FileEntityTag.TagsId = Tags.Id | ||||
| inner join Categories on Files.CategoryId = Categories.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);"); | (filters.Content is null ? "" : $" where contains(DocumentOrganizerDocStore.file_stream,@content);"); | ||||
| var files = await connection.QueryAsync<FileFilterReturnDto, TagResponse, CategoryResponse, FileFilterReturnDto>(sql,(file, tag, category) => | var files = await connection.QueryAsync<FileFilterReturnDto, TagResponse, CategoryResponse, FileFilterReturnDto>(sql,(file, tag, category) => | ||||
| Total = filtered.Count | 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(); | |||||
| } | |||||
| } | } | ||||
| } | } |
| { | { | ||||
| public interface ICategoryService | public interface ICategoryService | ||||
| { | { | ||||
| Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(); | |||||
| Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(int userId); | |||||
| Task<Category> GetCategoryEntityById(int id); | Task<Category> GetCategoryEntityById(int id); | ||||
| Task<List<IsGrantedCategory>> GetCategories(int userId); | Task<List<IsGrantedCategory>> GetCategories(int userId); | ||||
| } | } |
| Task<IEnumerable<FileEntity>> GetAll(); | Task<IEnumerable<FileEntity>> GetAll(); | ||||
| Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(string content); | Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(string content); | ||||
| Task<object> GetAllFiltered(FileFilter filters); | Task<object> GetAllFiltered(FileFilter filters); | ||||
| Task<FileEntity> GetFileEntityByIdAsync(Guid id); | |||||
| Task DeleteFileAsync(Guid id); | |||||
| } | } | ||||
| } | } |
| private readonly DatabaseContext _databaseContext; | private readonly DatabaseContext _databaseContext; | ||||
| private readonly IEmailer _emailer; | private readonly IEmailer _emailer; | ||||
| private readonly ILogger<UserService> _logger; | 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; | _frontEndSettings = frontEndSettings.Value; | ||||
| _userManager = userManager; | _userManager = userManager; | ||||
| _databaseContext = databaseContext; | _databaseContext = databaseContext; | ||||
| _emailer = emailer; | _emailer = emailer; | ||||
| _logger = logger; | _logger = logger; | ||||
| _categoryService = categoryService; | |||||
| } | } | ||||
| public async Task<IEnumerable<User?>> GetAll() | public async Task<IEnumerable<User?>> GetAll() |
| public string Extension { get; set; } | public string Extension { get; set; } | ||||
| public List<TagResponse> Tags { get; set; } | public List<TagResponse> Tags { get; set; } | ||||
| public bool Deleted { get; set; } | |||||
| } | } | ||||
| } | } |
| public int cached_file_size { get; set; } | public int cached_file_size { get; set; } | ||||
| public List<TagResponse> Tags { get; set; } = new(); | public List<TagResponse> Tags { get; set; } = new(); | ||||
| public CategoryResponse Category { get; set; } | public CategoryResponse Category { get; set; } | ||||
| public bool Deleted { get; set; } | |||||
| } | } | ||||
| } | } |
| public Category Category { get; set; } | public Category Category { get; set; } | ||||
| public List<Tag> Tags { get; set; } | public List<Tag> Tags { get; set; } | ||||
| public bool Deleted { get; set; } = false; | |||||
| } | } | ||||
| } | } |
| 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"); | |||||
| } | |||||
| } | |||||
| } |
| b.Property<int>("CategoryId") | b.Property<int>("CategoryId") | ||||
| .HasColumnType("int"); | .HasColumnType("int"); | ||||
| b.Property<bool>("Deleted") | |||||
| .HasColumnType("bit"); | |||||
| b.Property<Guid>("DocumentId") | b.Property<Guid>("DocumentId") | ||||
| .HasColumnType("uniqueidentifier"); | .HasColumnType("uniqueidentifier"); | ||||
| } | } | ||||
| [HttpGet("names")] | [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")] | [HttpGet("granted-categories")] | ||||
| public async Task<IActionResult> GetCategories(int userId) => | public async Task<IActionResult> GetCategories(int userId) => |
| await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title}); | await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title}); | ||||
| return Ok(); | return Ok(); | ||||
| } | } | ||||
| [HttpDelete("delete-file/{id}")] | |||||
| public async Task<IActionResult> DeleteFile([FromRoute]Guid id) | |||||
| { | |||||
| await _fileEntityService.DeleteFileAsync(id); | |||||
| return Ok(); | |||||
| } | |||||
| } | } | ||||
| } | } |