feature/files_filters_be vers BE_dev il y a 3 ans
| 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); | |||||
| } | |||||
| } | |||||
| } |
| using Azure.Core; | using Azure.Core; | ||||
| using Diligent.WebAPI.Contracts.DTOs.File; | using Diligent.WebAPI.Contracts.DTOs.File; | ||||
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Linq; | using System.Linq; | ||||
| { | { | ||||
| return await _context.Files.ToListAsync(); | 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 | |||||
| }; | |||||
| } | |||||
| } | } | ||||
| } | } |
| using Diligent.WebAPI.Contracts.DTOs.File; | using Diligent.WebAPI.Contracts.DTOs.File; | ||||
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Linq; | using System.Linq; | ||||
| Task<List<FileEntityResponse>> GetAllAsync(); | Task<List<FileEntityResponse>> GetAllAsync(); | ||||
| Task UploadPdfAsync(FileEntity file); | Task UploadPdfAsync(FileEntity file); | ||||
| Task<IEnumerable<FileEntity>> GetAll(); | Task<IEnumerable<FileEntity>> GetAll(); | ||||
| Task<object> GetAllFiltered(FileFilter filters); | |||||
| } | } | ||||
| } | } |
| 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; } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.File; | using Diligent.WebAPI.Contracts.DTOs.File; | ||||
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||||
| using Diligent.WebAPI.Host.Methods; | using Diligent.WebAPI.Host.Methods; | ||||
| namespace Diligent.WebAPI.Host.Controllers.V1 | namespace Diligent.WebAPI.Host.Controllers.V1 | ||||
| [HttpGet] | [HttpGet] | ||||
| public async Task<IActionResult> GetAll() => Ok(await _fileEntityService.GetAll()); | 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] | [HttpPost] | ||||
| public async Task<IActionResult> UploadPdf([FromForm] CreateFileRequest request) | public async Task<IActionResult> UploadPdf([FromForm] CreateFileRequest request) | ||||
| { | { |