From a7962492af7fae7b3f1b914217beea2ff6116814 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?=
 <nikita.grigorevich@gmail.com>
Date: Tue, 31 Aug 2021 18:01:26 +0500
Subject: [PATCH] fileController search by companyName (not by companyId). Add
 UserDto into FileInfoDto. Add CompanyDto into UserDto.

---
 AsbCloudApp/Data/CompanyDto.cs                |  2 +-
 AsbCloudApp/Data/FileInfoDto.cs               |  1 -
 AsbCloudApp/Data/UserDto.cs                   |  2 +
 AsbCloudApp/Services/IFileService.cs          |  4 +-
 .../Services/FileService.cs                   | 49 +++++++------------
 AsbCloudWebApi/Controllers/FileController.cs  |  6 +--
 6 files changed, 27 insertions(+), 37 deletions(-)

diff --git a/AsbCloudApp/Data/CompanyDto.cs b/AsbCloudApp/Data/CompanyDto.cs
index 795d4358..f53d6483 100644
--- a/AsbCloudApp/Data/CompanyDto.cs
+++ b/AsbCloudApp/Data/CompanyDto.cs
@@ -4,6 +4,6 @@
     {
         public int Id { get; set; }
         public string Caption { get; set; }
-        public string CompanyType { get; set; }
+        public string CompanyTypeCaption { get; set; }
     }
 }
diff --git a/AsbCloudApp/Data/FileInfoDto.cs b/AsbCloudApp/Data/FileInfoDto.cs
index 2361e325..46c29e98 100644
--- a/AsbCloudApp/Data/FileInfoDto.cs
+++ b/AsbCloudApp/Data/FileInfoDto.cs
@@ -12,6 +12,5 @@ namespace AsbCloudApp.Data
         public DateTime UploadDate { get; set; }
         public long Size { get; set; }
         public UserDto Author { get; set; }
-        public string Company { get; set; }
     }
 }
diff --git a/AsbCloudApp/Data/UserDto.cs b/AsbCloudApp/Data/UserDto.cs
index dd9234c6..98abcbb9 100644
--- a/AsbCloudApp/Data/UserDto.cs
+++ b/AsbCloudApp/Data/UserDto.cs
@@ -9,5 +9,7 @@
         public int? IdRole { get; set; }
 
         public string Password { get; set; }
+
+        public CompanyDto Company { get; set; }
     }
 }
diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs
index 4b0ac894..77f9d3ca 100644
--- a/AsbCloudApp/Services/IFileService.cs
+++ b/AsbCloudApp/Services/IFileService.cs
@@ -14,8 +14,8 @@ namespace AsbCloudApp.Services
         Task<FileInfoDto> SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default);
 
         Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
-            int idCategory, IEnumerable<int> companies, string fileName, DateTime begin, DateTime end,
-            int skip, int take, CancellationToken token = default);
+            int idCategory, string companyName = default, string fileName = default, DateTime begin = default, DateTime end = default,
+            int skip = 0, int take = 32, CancellationToken token = default);
 
         Task<FileInfoDto> GetInfoAsync(int fileId,
             CancellationToken token);
diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs
index c8ae19ce..e758a828 100644
--- a/AsbCloudInfrastructure/Services/FileService.cs
+++ b/AsbCloudInfrastructure/Services/FileService.cs
@@ -1,7 +1,6 @@
 using AsbCloudApp.Data;
 using AsbCloudApp.Services;
 using AsbCloudDb.Model;
-using AsbCloudInfrastructure.Services.Cache;
 using Mapster;
 using Microsoft.EntityFrameworkCore;
 using System;
@@ -16,14 +15,18 @@ namespace AsbCloudInfrastructure.Services
     public class FileService : IFileService
     {
         public string RootPath { get; private set; }
-        private readonly IAsbCloudDbContext db;
-        private readonly CacheTable<Company> cacheCompanies;
 
-        public FileService(IAsbCloudDbContext db, Cache.CacheDb cacheDb)
+        private readonly IQueryable<AsbCloudDb.Model.FileInfo> dbSetConfigured;
+        private readonly IAsbCloudDbContext db;
+
+        public FileService(IAsbCloudDbContext db)
         {
             RootPath = "files";
             this.db = db;
-            cacheCompanies = cacheDb.GetCachedTable<AsbCloudDb.Model.Company>((DbContext)db);
+            dbSetConfigured = db.Files
+                .Include(f => f.Author)
+                    .ThenInclude(u => u.Company)
+                        .ThenInclude(c => c.CompanyType);
         }
 
         public async Task<FileInfoDto> SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default)
@@ -61,37 +64,31 @@ namespace AsbCloudInfrastructure.Services
         public async Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell,
             int idCategory, CancellationToken token = default)
         {
-            var entities = await db.Files
-                .Include(f => f.Author)
+            var entities = await dbSetConfigured
                 .Where(e => e.IdWell == idWell && e.IdCategory == idCategory)
                 .AsNoTracking()
                 .ToListAsync(token)
                 .ConfigureAwait(false);
 
-            var dtos = entities.Adapt<FileInfoDto, AsbCloudDb.Model.FileInfo>(async (d,s) => {
-                d.Author = s.Author?.Adapt<UserDto>();
-                var company = await cacheCompanies.FirstOrDefaultAsync(c => c.Id == s.Author?.IdCompany, token)
-                    .ConfigureAwait(false);
-                d.Company = company?.Caption;
-            });
-
+            var dtos = entities.Adapt<FileInfoDto>();
             return dtos;
         }
 
         public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
-            int idCategory, IEnumerable<int> companies = default, string fileName = default, DateTime begin = default,
+            int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
             DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
         {
-            var query = db.Files
-                .Include(f => f.Author)
+            var query = dbSetConfigured
                 .Where(e => e.IdWell == idWell &&
                     e.IdCategory == idCategory);
 
             query = query.Where(e => !e.IsDeleted);
 
-            if (companies.Any())
-                query.Include(file => file.Author).ThenInclude(a => a.Company)
-                    .Where(e => companies.Contains(e.Author.Company.Id));
+            if (!string.IsNullOrEmpty(companyName))
+                query = query
+                        .Include(file => file.Author)
+                            .ThenInclude(a => a.Company)
+                        .Where(e => (e.Author == null) || (e.Author.Company == null) || e.Author.Company.Caption.Contains(companyName));
 
             if (!string.IsNullOrEmpty(fileName))
                 query = query.Where(e => e.Name.ToLower().Contains(fileName.ToLower()));
@@ -124,13 +121,7 @@ namespace AsbCloudInfrastructure.Services
                 .Take(take).AsNoTracking().ToListAsync(token)
                 .ConfigureAwait(false);
 
-            var dtos = entities.Adapt<FileInfoDto, AsbCloudDb.Model.FileInfo>(async (d, s) =>
-            {
-                d.Author = s.Author?.Adapt<UserDto>();
-                var company = await cacheCompanies.FirstOrDefaultAsync(c => c.Id == s.Author?.IdCompany, token)
-                    .ConfigureAwait(false);
-                d.Company = company?.Caption;
-            });
+            var dtos = entities.Adapt<FileInfoDto>();
 
             result.Items.AddRange(dtos);
             return result;
@@ -139,8 +130,7 @@ namespace AsbCloudInfrastructure.Services
         public async Task<FileInfoDto> GetInfoAsync(int fileId,
             CancellationToken token = default)
         {
-            var entity = await db.Files
-                .Include(f => f.Author)
+            var entity = await dbSetConfigured
                 .AsNoTracking()
                 .FirstOrDefaultAsync(f => f.Id == fileId, token)
                 .ConfigureAwait(false);
@@ -149,7 +139,6 @@ namespace AsbCloudInfrastructure.Services
                 return null;
 
             var dto = entity.Adapt<FileInfoDto>();
-            dto.Author = entity.Author?.Adapt<UserDto>();
             return dto;
         }
 
diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs
index 09e8143e..42058db1 100644
--- a/AsbCloudWebApi/Controllers/FileController.cs
+++ b/AsbCloudWebApi/Controllers/FileController.cs
@@ -95,7 +95,7 @@ namespace AsbCloudWebApi.Controllers
         /// </summary>
         /// <param name="idWell">id скважины</param>
         /// <param name="idCategory">id категории файла</param>
-        /// <param name="companies">id компаний для фильтрации возвращаемых файлов</param>
+        /// <param name="companyName">id компаний для фильтрации возвращаемых файлов</param>
         /// <param name="fileName">часть имени файла для поиска</param>
         /// <param name="begin">дата начала</param>
         /// <param name="end">дата окончания</param>
@@ -108,7 +108,7 @@ namespace AsbCloudWebApi.Controllers
         public async Task<IActionResult> GetFilesInfoAsync(
             [FromRoute] int idWell,
             int idCategory = default,
-            [FromQuery] IEnumerable<int> companies = default,
+            string companyName = default,
             string fileName = default,
             DateTime begin = default, 
             DateTime end = default,
@@ -123,7 +123,7 @@ namespace AsbCloudWebApi.Controllers
                 return Forbid();
 
             var filesInfo = await fileService.GetInfosAsync(idWell, idCategory,
-                companies, fileName, begin, end, skip, take, token).ConfigureAwait(false);
+                companyName, fileName, begin, end, skip, take, token).ConfigureAwait(false);
 
             return Ok(filesInfo);
         }