#6539681 Исправления

This commit is contained in:
ai.astrakhantsev 2022-09-30 10:49:40 +05:00
parent ead3e860ba
commit f9e566cfef
5 changed files with 167 additions and 85 deletions

View File

@ -1,31 +1,16 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository namespace AsbCloudApp.Services
{ {
/// <summary> /// <summary>
/// Сервис доступа к файлам /// Сервис доступа к файлам
/// </summary> /// </summary>
public interface IFileRepository public interface IFileRepository : ICrudService<FileInfoDto>
{ {
/// <summary>
/// Добавление, в БД, информации о файле
/// </summary>
/// <param name="idWell"></param>
/// <param name="idUser"></param>
/// <param name="idCategory"></param>
/// <param name="destinationFileName"></param>
/// <param name="fileSize"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> AddAsync(int idWell, int? idUser, int idCategory,
string destinationFileName, long fileSize, CancellationToken token = default);
/// <summary> /// <summary>
/// Получить файлы определенной категории /// Получить файлы определенной категории
/// </summary> /// </summary>
@ -52,14 +37,6 @@ namespace AsbCloudInfrastructure.Repository
int idCategory, string companyName = 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); DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default);
/// <summary>
/// Инфо о файле
/// </summary>
/// <param name="idFile"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<FileInfoDto> GetInfoAsync(int idFile, CancellationToken token);
/// <summary> /// <summary>
/// Пометить файл как удаленный /// Пометить файл как удаленный
/// </summary> /// </summary>

View File

@ -107,5 +107,47 @@ namespace AsbCloudApp.Services
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token); Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token);
/// <summary>
/// Получить список файлов в контейнере
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="companyName"></param>
/// <param name="fileName"></param>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default);
/// <summary>
/// Пометить файл как удаленный
/// </summary>
/// <param name="idFile"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> MarkAsDeletedAsync(int idFile, CancellationToken token = default);
/// <summary>
/// добавить метку на файл
/// </summary>
/// <param name="fileMarkDto"></param>
/// <param name="idUser"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token);
/// <summary>
/// Получить запись по id
/// </summary>
/// <param name="id"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<FileInfoDto> GetOrDefaultAsync(int id, CancellationToken token);
} }
} }

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -28,25 +29,6 @@ namespace AsbCloudInfrastructure.Repository
.Include(f => f.Well); .Include(f => f.Well);
} }
public async Task<int> AddAsync(int idWell, int? idUser, int idCategory,
string destinationFileName, long fileSize, CancellationToken token = default)
{
var fileInfo = new AsbCloudDb.Model.FileInfo()
{
IdWell = idWell,
IdAuthor = idUser,
IdCategory = idCategory,
Name = destinationFileName,
UploadDate = DateTime.UtcNow,
IsDeleted = false,
Size = fileSize,
};
var entry = db.Files.Add(fileInfo);
await db.SaveChangesAsync(token).ConfigureAwait(false);
return entry.Entity.Id;
}
public async Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell, int idCategory, CancellationToken token) public async Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell, int idCategory, CancellationToken token)
{ {
var entities = await dbSetConfigured var entities = await dbSetConfigured
@ -126,22 +108,6 @@ namespace AsbCloudInfrastructure.Repository
return result; return result;
} }
public async Task<FileInfoDto> GetInfoAsync(int idFile, CancellationToken token)
{
var entity = await dbSetConfigured
.AsNoTracking()
.FirstOrDefaultAsync(f => f.Id == idFile, token)
.ConfigureAwait(false);
if (entity is null)
{
throw new FileNotFoundException($"fileId:{idFile} not found");
}
var dto = Convert(entity);
return dto;
}
public async Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token) public async Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token)
{ {
var result = new List<FileInfoDto>(); var result = new List<FileInfoDto>();
@ -178,26 +144,20 @@ namespace AsbCloudInfrastructure.Repository
public async Task<IEnumerable<FileInfoDto>> DeleteAsync(IEnumerable<int> ids, CancellationToken token) public async Task<IEnumerable<FileInfoDto>> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
{ {
var filesQuery = db.Files var query = dbSetConfigured
.Where(f => ids.Contains(f.Id)); .Where(f => ids.Contains(f.Id) && f.IsDeleted);
var files = await filesQuery.ToListAsync(token); var files = await query.ToListAsync(token);
var filesDtos = files.Select(x => new FileInfoDto { var filesDtos = files.Select(x => Convert(x));
Id = x.Id,
IdWell = x.Id,
IdCategory = x.IdCategory,
Name = x.Name
});
db.Files.RemoveRange(filesQuery); db.Files.RemoveRange(query);
await db.SaveChangesAsync(token).ConfigureAwait(false); await db.SaveChangesAsync(token).ConfigureAwait(false);
return filesDtos; return filesDtos;
} }
public async Task<FileInfoDto> GetByMarkId(int idMark, public async Task<FileInfoDto> GetByMarkId(int idMark, CancellationToken token)
CancellationToken token)
{ {
var entity = await dbSetConfigured var entity = await dbSetConfigured
.FirstOrDefaultAsync(f => f.FileMarks.Any(m => m.Id == idMark), token) .FirstOrDefaultAsync(f => f.FileMarks.Any(m => m.Id == idMark), token)
@ -271,5 +231,75 @@ namespace AsbCloudInfrastructure.Repository
}); });
return dto; return dto;
} }
public async Task<IEnumerable<FileInfoDto>> GetAllAsync(CancellationToken token)
=> await dbSetConfigured.AsNoTracking()
.Select(x => Convert(x))
.ToListAsync(token)
.ConfigureAwait(false);
public async Task<FileInfoDto> GetOrDefaultAsync(int id, CancellationToken token)
{
var entity = await dbSetConfigured
.AsNoTracking()
.FirstOrDefaultAsync(f => f.Id == id, token)
.ConfigureAwait(false);
if (entity is null)
{
throw new FileNotFoundException($"fileId:{id} not found");
}
var dto = Convert(entity);
return dto;
}
public FileInfoDto GetOrDefault(int id)
{
var entity = dbSetConfigured
.AsNoTracking()
.FirstOrDefault(f => f.Id == id);
if (entity is null)
{
throw new FileNotFoundException($"fileId:{id} not found");
}
var dto = Convert(entity);
return dto;
}
public async Task<int> InsertAsync(FileInfoDto newItem, CancellationToken token)
{
var fileInfo = new AsbCloudDb.Model.FileInfo()
{
IdWell = newItem.IdWell,
IdAuthor = newItem.IdAuthor,
IdCategory = newItem.IdCategory,
Name = newItem.Name,
UploadDate = DateTime.UtcNow,
IsDeleted = false,
Size = newItem.Size,
};
var entry = db.Files.Add(fileInfo);
await db.SaveChangesAsync(token).ConfigureAwait(false);
return entry.Entity.Id;
}
public Task<int> InsertRangeAsync(IEnumerable<FileInfoDto> newItems, CancellationToken token)
{
throw new NotImplementedException();
}
public Task<int> UpdateAsync(FileInfoDto item, CancellationToken token)
{
throw new NotImplementedException();
}
public Task<int> DeleteAsync(int id, CancellationToken token)
{
throw new NotImplementedException();
}
} }
} }

View File

@ -36,7 +36,14 @@ namespace AsbCloudInfrastructure.Services
var sysFileInfo = new System.IO.FileInfo(srcFilePath); var sysFileInfo = new System.IO.FileInfo(srcFilePath);
//save info to db //save info to db
var fileId = await fileRepository.AddAsync(idWell, idUser, idCategory, destinationFileName, sysFileInfo.Length, token) var dto = new FileInfoDto {
IdWell = idWell,
IdAuthor = idUser,
IdCategory = idCategory,
Name = destinationFileName,
Size = sysFileInfo.Length
};
var fileId = await fileRepository.InsertAsync(dto, token)
.ConfigureAwait(false); .ConfigureAwait(false);
string filePath = MakeFilePath(idWell, idCategory, destinationFileName, fileId); string filePath = MakeFilePath(idWell, idCategory, destinationFileName, fileId);
@ -50,7 +57,16 @@ namespace AsbCloudInfrastructure.Services
string fileFullName, Stream fileStream, CancellationToken token) string fileFullName, Stream fileStream, CancellationToken token)
{ {
//save info to db //save info to db
var fileId = await fileRepository.AddAsync(idWell, idUser, idCategory, Path.GetFileName(fileFullName), fileStream?.Length ?? 0, token) var dto = new FileInfoDto
{
IdWell = idWell,
IdAuthor = idUser,
IdCategory = idCategory,
Name = Path.GetFileName(fileFullName),
Size = fileStream?.Length ?? 0
};
var fileId = await fileRepository.InsertAsync(dto, token)
.ConfigureAwait(false); .ConfigureAwait(false);
//save stream to disk //save stream to disk
@ -73,7 +89,7 @@ namespace AsbCloudInfrastructure.Services
public async Task<FileInfoDto> GetInfoAsync(int idFile, public async Task<FileInfoDto> GetInfoAsync(int idFile,
CancellationToken token) CancellationToken token)
{ {
var dto = await fileRepository.GetInfoAsync(idFile, token).ConfigureAwait(false); var dto = await fileRepository.GetOrDefaultAsync(idFile, token).ConfigureAwait(false);
var ext = Path.GetExtension(dto.Name); var ext = Path.GetExtension(dto.Name);
@ -88,7 +104,7 @@ namespace AsbCloudInfrastructure.Services
} }
public Task<int> DeleteAsync(int idFile, CancellationToken token) public Task<int> DeleteAsync(int idFile, CancellationToken token)
=> DeleteAsync(new int[] { idFile }, token); => DeleteAsync(new int[] { idFile }, token);
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token) public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
{ {
@ -112,7 +128,7 @@ namespace AsbCloudInfrastructure.Services
public async Task<string> GetUrl(int idFile) public async Task<string> GetUrl(int idFile)
{ {
var fileInfo = await fileRepository.GetInfoAsync(idFile, CancellationToken.None).ConfigureAwait(false); var fileInfo = await fileRepository.GetOrDefaultAsync(idFile, CancellationToken.None).ConfigureAwait(false);
return GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name)); return GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
} }
@ -145,5 +161,23 @@ namespace AsbCloudInfrastructure.Services
return result; return result;
} }
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
=> await fileRepository.GetInfosAsync(idWell, idCategory, companyName, fileName, begin, end, skip, take, token)
.ConfigureAwait(false);
public async Task<int> MarkAsDeletedAsync(int idFile, CancellationToken token = default)
=> await fileRepository.MarkAsDeletedAsync(idFile, token)
.ConfigureAwait(false);
public async Task<int> CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
=> await fileRepository.CreateFileMarkAsync(fileMarkDto, idUser, token)
.ConfigureAwait(false);
public async Task<FileInfoDto> GetOrDefaultAsync(int id, CancellationToken token)
=> await fileRepository.GetOrDefaultAsync(id, token)
.ConfigureAwait(false);
} }
} }

View File

@ -1,10 +1,11 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudInfrastructure.Repository;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Collections;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -21,13 +22,11 @@ namespace AsbCloudWebApi.Controllers
{ {
private readonly IFileService fileService; private readonly IFileService fileService;
private readonly IWellService wellService; private readonly IWellService wellService;
private readonly IFileRepository fileRepository;
public FileController(IFileService fileService, IWellService wellService, IFileRepository fileRepository) public FileController(IFileService fileService, IWellService wellService)
{ {
this.fileService = fileService; this.fileService = fileService;
this.wellService = wellService; this.wellService = wellService;
this.fileRepository = fileRepository;
} }
/// <summary> /// <summary>
@ -101,7 +100,7 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false)) idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var filesInfo = await fileRepository.GetInfosAsync(idWell, idCategory, var filesInfo = await fileService.GetInfosAsync(idWell, idCategory,
companyName, fileName, begin, end, skip, take, token).ConfigureAwait(false); companyName, fileName, begin, end, skip, take, token).ConfigureAwait(false);
return Ok(filesInfo); return Ok(filesInfo);
@ -171,7 +170,7 @@ namespace AsbCloudWebApi.Controllers
if (!userService.HasPermission((int)idUser, $"File.edit{file.IdCategory}")) if (!userService.HasPermission((int)idUser, $"File.edit{file.IdCategory}"))
return Forbid(); return Forbid();
var result = await fileRepository.MarkAsDeletedAsync(idFile, token); var result = await fileService.MarkAsDeletedAsync(idFile, token);
return Ok(result); return Ok(result);
} }
@ -196,7 +195,7 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false)) idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var result = await fileRepository.CreateFileMarkAsync(markDto, (int)idUser, token) var result = await fileService.CreateFileMarkAsync(markDto, (int)idUser, token)
.ConfigureAwait(false); .ConfigureAwait(false);
return Ok(result); return Ok(result);
@ -246,7 +245,7 @@ namespace AsbCloudWebApi.Controllers
try try
{ {
var fileInfo = await fileRepository.GetInfoAsync(idFile, token).ConfigureAwait(false); var fileInfo = await fileService.GetOrDefaultAsync(idFile, token).ConfigureAwait(false);
return Ok(fileInfo); return Ok(fileInfo);
} }
catch (FileNotFoundException ex) catch (FileNotFoundException ex)