| @@ -0,0 +1,36 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||
| namespace Diligent.WebAPI.Business.Extensions | |||
| { | |||
| [ExcludeFromCodeCoverage] | |||
| public static class FileExtensions | |||
| { | |||
| public static IQueryable<FileEntity> FilterByExtension(this IQueryable<FileEntity> query, string[]? values) | |||
| { | |||
| if (values == null || values.Length == 0) | |||
| return query; | |||
| return query.Where(n => values.Contains(n.Extension)); | |||
| } | |||
| public static IQueryable<FileEntity> FilterByCategory(this IQueryable<FileEntity> query, string[]? values) | |||
| { | |||
| if (values == null || values.Length == 0) | |||
| return query; | |||
| return query.Where(n => values.Contains(n.Category.Name)); | |||
| } | |||
| public static IQueryable<FileEntity> FilterByTags(this IQueryable<FileEntity> query, string[]? values) | |||
| { | |||
| if (values == null || values.Length == 0) | |||
| return query; | |||
| //return query.Where(n => values.Any(x => n.Tags.Any(y => y.Name == x))); | |||
| return query.Where(n => n.Tags.Any(x => values.Contains(x.Name))); | |||
| } | |||
| public static IQueryable<FileEntity> FilterFiles(this IQueryable<FileEntity> query, FileFilter filters) | |||
| { | |||
| return query | |||
| .FilterByCategory(filters.Categories) | |||
| .FilterByExtension(filters.Extensions) | |||
| .FilterByTags(filters.Tags); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| using Azure.Core; | |||
| using Diligent.WebAPI.Contracts.DTOs.File; | |||
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| @@ -35,5 +36,21 @@ namespace Diligent.WebAPI.Business.Services | |||
| { | |||
| return await _context.Files.ToListAsync(); | |||
| } | |||
| public async Task<object> GetAllFiltered(FileFilter filters) | |||
| { | |||
| var filtered = await _context.Files | |||
| .Include(n => n.Tags) | |||
| .Include(n => n.Category) | |||
| .FilterFiles(filters) | |||
| .ToListAsync(); | |||
| return new | |||
| { | |||
| Data = filtered.ApplyPagging(filters) | |||
| .Select(n => new { n.Id, n.Name, n.Size, n.Extension}), | |||
| Total = filtered.Count | |||
| }; | |||
| } | |||
| } | |||
| } | |||
| @@ -1,4 +1,5 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.File; | |||
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| @@ -12,5 +13,6 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| Task<List<FileEntityResponse>> GetAllAsync(); | |||
| Task UploadPdfAsync(FileEntity file); | |||
| Task<IEnumerable<FileEntity>> GetAll(); | |||
| Task<object> GetAllFiltered(FileFilter filters); | |||
| } | |||
| } | |||
| @@ -0,0 +1,11 @@ | |||
| using Diligent.WebAPI.Contracts.Models; | |||
| namespace Diligent.WebAPI.Contracts.DTOs.Files | |||
| { | |||
| public class FileFilter : Pagination | |||
| { | |||
| public string[]? Extensions { get; set; } | |||
| public string[]? Categories { get; set; } | |||
| public string[]? Tags { get; set; } | |||
| } | |||
| } | |||
| @@ -1,4 +1,5 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.File; | |||
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||
| using Diligent.WebAPI.Host.Methods; | |||
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| @@ -24,6 +25,9 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| [HttpGet] | |||
| public async Task<IActionResult> GetAll() => Ok(await _fileEntityService.GetAll()); | |||
| [HttpGet("filtered")] | |||
| public async Task<IActionResult> GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters)); | |||
| [HttpPost] | |||
| public async Task<IActionResult> UploadPdf([FromForm] CreateFileRequest request) | |||
| { | |||