Parcourir la source

infinity depth search of folders and files

BE_dev
Dzenis Hadzifejzovic il y a 2 ans
Parent
révision
e4ae7b844c

+ 1
- 1
Diligent.WebAPI.Business/Extensions/FileExtensions.cs Voir le fichier

@@ -28,7 +28,7 @@ namespace Diligent.WebAPI.Business.Extensions
public static List<FileFilterReturnDto> FilterFiles(this List<FileFilterReturnDto> query, FileFilter filters)
{
return query
.FilterByCategory(filters.Categories)
//.FilterByCategory(filters.Categories)
.FilterByExtension(filters.Extensions)
.FilterByTags(filters.Tags);
}

+ 30
- 26
Diligent.WebAPI.Business/Services/CategoryService.cs Voir le fichier

@@ -25,41 +25,30 @@ namespace Diligent.WebAPI.Business.Services
_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 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")
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);
}

public async Task<Category> GetCategoryEntityById(int? id) =>
await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();

@@ -102,5 +91,20 @@ namespace Diligent.WebAPI.Business.Services

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);
}
}
}

+ 19
- 9
Diligent.WebAPI.Business/Services/FileEntityService.cs Voir le fichier

@@ -41,20 +41,30 @@ namespace Diligent.WebAPI.Business.Services
public async Task<object> GetAllFiltered(FileFilter filters)
{
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
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.Category = category;
return file;
}, splitOn: "TagId, CategoryId");
}, splitOn: "TagId",
param: new { categoryId = filters.CategoryId });

var filesList = new List<FileFilterReturnDto>();
foreach (var fileItem in files.ToList())

+ 1
- 2
Diligent.WebAPI.Business/Services/Interfaces/ICategoryService.cs Voir le fichier

@@ -9,8 +9,7 @@ namespace Diligent.WebAPI.Business.Services.Interfaces
{
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<List<IsGrantedCategory>> GetCategories(int userId);
}

+ 1
- 1
Diligent.WebAPI.Contracts/DTOs/Files/FileFilter.cs Voir le fichier

@@ -5,8 +5,8 @@ namespace Diligent.WebAPI.Contracts.DTOs.Files
public class FileFilter : Pagination
{
public string[]? Extensions { get; set; }
public string[]? Categories { get; set; } = null;
public string[]? Tags { get; set; }
public int CategoryId { get; set; }
public string? Content { get; set; }
}
}

+ 3
- 10
Diligent.WebAPI.Host/Controllers/V1/CategoriesController.cs Voir le fichier

@@ -14,18 +14,11 @@ namespace Diligent.WebAPI.Host.Controllers.V1
_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"];
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")]

Chargement…
Annuler
Enregistrer