| public static List<FileFilterReturnDto> FilterFiles(this List<FileFilterReturnDto> query, FileFilter filters) | public static List<FileFilterReturnDto> FilterFiles(this List<FileFilterReturnDto> query, FileFilter filters) | ||||
| { | { | ||||
| return query | return query | ||||
| .FilterByCategory(filters.Categories) | |||||
| //.FilterByCategory(filters.Categories) | |||||
| .FilterByExtension(filters.Extensions) | .FilterByExtension(filters.Extensions) | ||||
| .FilterByTags(filters.Tags); | .FilterByTags(filters.Tags); | ||||
| } | } |
| _configuration = configuration; | _configuration = configuration; | ||||
| } | } | ||||
| public async Task<List<CategoriesNamesResponse>> GetRootCategories(int userId) | |||||
| public async Task<List<CategoriesNamesResponse>> GetRootCategories(int userId,int parentCategoryId) | |||||
| { | { | ||||
| var user = await _userManager.FindByIdAsync(userId.ToString()); | var user = await _userManager.FindByIdAsync(userId.ToString()); | ||||
| var role = (await _userManager.GetRolesAsync(user))[0]; | var role = (await _userManager.GetRolesAsync(user))[0]; | ||||
| if(role == "SuperAdmin") | |||||
| return _mapper.Map<List<CategoriesNamesResponse>> | |||||
| (await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync()); | |||||
| var userCategories = await _context.UserCategories | |||||
| .Where(k => k.UserId == userId && k.Category.ParentCategory == null) | |||||
| .Include(t => t.Category) | |||||
| .ToListAsync(); | |||||
| return GetCategoriesFromUserCategories(userCategories); | |||||
| } | |||||
| public async Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId,int userId) | |||||
| { | |||||
| var user = await _userManager.FindByIdAsync(userId.ToString()); | |||||
| var role = (await _userManager.GetRolesAsync(user))[0]; | |||||
| if (role == "SuperAdmin") | if (role == "SuperAdmin") | ||||
| return _mapper.Map<List<CategoriesNamesResponse>>( | |||||
| await _context.Categories | |||||
| .Where(k => k.ParentCategory != null && k.ParentCategory.Id == parentCategoryId) | |||||
| .ToListAsync()); | |||||
| if (parentCategoryId == -1) | |||||
| return _mapper.Map<List<CategoriesNamesResponse>> | |||||
| (await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync()); | |||||
| else | |||||
| return await GetChildCategories(parentCategoryId, userId,role); | |||||
| var userCategories = await _context.UserCategories | |||||
| .Where(k => k.UserId == userId && k.Category != null && k.Category.ParentCategory != null && k.Category.ParentCategory.Id == parentCategoryId) | |||||
| .Include(t => t.Category) | |||||
| .ToListAsync(); | |||||
| var userCategories = new List<UserCategories>(); | |||||
| if(parentCategoryId == -1) | |||||
| userCategories = await _context.UserCategories | |||||
| .Where(k => k.UserId == userId && k.Category.ParentCategory == null) | |||||
| .Include(t => t.Category) | |||||
| .ToListAsync(); | |||||
| else | |||||
| return await GetChildCategories(parentCategoryId, userId,role); | |||||
| return GetCategoriesFromUserCategories(userCategories); | return GetCategoriesFromUserCategories(userCategories); | ||||
| } | } | ||||
| 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(); | ||||
| return res; | return res; | ||||
| } | } | ||||
| private async Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId, int userId, string role) | |||||
| { | |||||
| if (role == "SuperAdmin") | |||||
| return _mapper.Map<List<CategoriesNamesResponse>>( | |||||
| await _context.Categories | |||||
| .Where(k => k.ParentCategory != null && k.ParentCategory.Id == parentCategoryId) | |||||
| .ToListAsync()); | |||||
| var userCategories = await _context.UserCategories | |||||
| .Where(k => k.UserId == userId && k.Category != null && k.Category.ParentCategory != null && k.Category.ParentCategory.Id == parentCategoryId) | |||||
| .Include(t => t.Category) | |||||
| .ToListAsync(); | |||||
| return GetCategoriesFromUserCategories(userCategories); | |||||
| } | |||||
| } | } | ||||
| } | } |
| public async Task<object> GetAllFiltered(FileFilter filters) | public async Task<object> GetAllFiltered(FileFilter filters) | ||||
| { | { | ||||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | ||||
| var sql = @"SELECT Files.Id as FileId, Note, 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 | |||||
| var sql = ""; | |||||
| if(filters.CategoryId == 0) | |||||
| { | |||||
| sql = @"SELECT Files.Id as FileId, Note, Files.Deleted, stream_id, DocumentOrganizerDocStore.name as FileName, file_stream, file_type, cached_file_size, Title, Tags.Id as TagId, Tags.Name as TagName | |||||
| 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 DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0" + | |||||
| (filters.Content is null ? "" : " and contains(DocumentOrganizerDocStore.file_stream," + "'\"" + filters.Content + "*\"')"); | |||||
| var files = await connection.QueryAsync<FileFilterReturn, TagResponse, CategoryResponse, FileFilterReturn>(sql,(file, tag, category) => | |||||
| inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0 and Files.CategoryId is NULL" + | |||||
| (filters.Content is null ? "" : " and contains(DocumentOrganizerDocStore.file_stream," + "'\"" + filters.Content + "*\"')"); | |||||
| } | |||||
| else | |||||
| { | |||||
| sql = @"SELECT Files.Id as FileId, Note, Files.Deleted, stream_id, DocumentOrganizerDocStore.name as FileName, file_stream, file_type, cached_file_size, Title, Tags.Id as TagId, Tags.Name as TagName | |||||
| FROM Files inner join FileEntityTag on Files.Id = FileEntityTag.FilesId | |||||
| inner join Tags on FileEntityTag.TagsId = Tags.Id | |||||
| inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0 and Files.CategoryId=@categoryId" + | |||||
| (filters.Content is null ? "" : " and contains(DocumentOrganizerDocStore.file_stream," + "'\"" + filters.Content + "*\"')"); | |||||
| } | |||||
| var files = await connection.QueryAsync<FileFilterReturn, TagResponse, FileFilterReturn>(sql,(file, tag) => | |||||
| { | { | ||||
| file.Tags.Add(tag); | file.Tags.Add(tag); | ||||
| file.Category = category; | |||||
| return file; | return file; | ||||
| }, splitOn: "TagId, CategoryId"); | |||||
| }, splitOn: "TagId", | |||||
| param: new { categoryId = filters.CategoryId }); | |||||
| var filesList = new List<FileFilterReturnDto>(); | var filesList = new List<FileFilterReturnDto>(); | ||||
| foreach (var fileItem in files.ToList()) | foreach (var fileItem in files.ToList()) |
| { | { | ||||
| public interface ICategoryService | public interface ICategoryService | ||||
| { | { | ||||
| Task<List<CategoriesNamesResponse>> GetRootCategories(int userId); | |||||
| Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId,int userId); | |||||
| Task<List<CategoriesNamesResponse>> GetRootCategories(int userId,int categoryId); | |||||
| Task<Category> GetCategoryEntityById(int? id); | Task<Category> GetCategoryEntityById(int? id); | ||||
| Task<List<IsGrantedCategory>> GetCategories(int userId); | Task<List<IsGrantedCategory>> GetCategories(int userId); | ||||
| } | } |
| public class FileFilter : Pagination | public class FileFilter : Pagination | ||||
| { | { | ||||
| public string[]? Extensions { get; set; } | public string[]? Extensions { get; set; } | ||||
| public string[]? Categories { get; set; } = null; | |||||
| public string[]? Tags { get; set; } | public string[]? Tags { get; set; } | ||||
| public int CategoryId { get; set; } | |||||
| public string? Content { get; set; } | public string? Content { get; set; } | ||||
| } | } | ||||
| } | } |
| _categoryService = categoryService; | _categoryService = categoryService; | ||||
| } | } | ||||
| [HttpGet("root-categories")] | |||||
| public async Task<IActionResult> GetRootCategories() | |||||
| [HttpGet("root-categories/{parentCategoryId:int?}")] | |||||
| 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)); | |||||
| } | |||||
| [HttpGet("child-categories")] | |||||
| public async Task<IActionResult> GetChildCategories(int parentCategoryId) | |||||
| { | |||||
| User? user = (User?)HttpContext.Items["User"]; | |||||
| return Ok(await _categoryService.GetChildCategories(parentCategoryId, user.Id)); | |||||
| return Ok(await _categoryService.GetRootCategories(8,parentCategoryId)); | |||||
| } | } | ||||
| [HttpGet("granted-categories")] | [HttpGet("granted-categories")] |