diff --git a/AsbCloudInfrastructure/Repository/IFileRepository.cs b/AsbCloudApp/Services/IFileRepository.cs
similarity index 79%
rename from AsbCloudInfrastructure/Repository/IFileRepository.cs
rename to AsbCloudApp/Services/IFileRepository.cs
index 73ae9b86..e01891cc 100644
--- a/AsbCloudInfrastructure/Repository/IFileRepository.cs
+++ b/AsbCloudApp/Services/IFileRepository.cs
@@ -1,31 +1,16 @@
using AsbCloudApp.Data;
using System;
using System.Collections.Generic;
-using System.Linq;
-using System.Text;
using System.Threading;
using System.Threading.Tasks;
-namespace AsbCloudInfrastructure.Repository
+namespace AsbCloudApp.Services
{
///
/// Сервис доступа к файлам
///
- public interface IFileRepository
+ public interface IFileRepository : ICrudService
{
- ///
- /// Добавление, в БД, информации о файле
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- Task AddAsync(int idWell, int? idUser, int idCategory,
- string destinationFileName, long fileSize, CancellationToken token = default);
-
///
/// Получить файлы определенной категории
///
@@ -52,14 +37,6 @@ namespace AsbCloudInfrastructure.Repository
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default);
- ///
- /// Инфо о файле
- ///
- ///
- ///
- ///
- Task GetInfoAsync(int idFile, CancellationToken token);
-
///
/// Пометить файл как удаленный
///
diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs
index 6b1a171f..89b89f00 100644
--- a/AsbCloudApp/Services/IFileService.cs
+++ b/AsbCloudApp/Services/IFileService.cs
@@ -107,5 +107,47 @@ namespace AsbCloudApp.Services
///
///
Task> GetInfoByIdsAsync(IEnumerable idsFile, CancellationToken token);
+
+ ///
+ /// Получить список файлов в контейнере
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ Task> 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);
+
+ ///
+ /// Пометить файл как удаленный
+ ///
+ ///
+ ///
+ ///
+ Task MarkAsDeletedAsync(int idFile, CancellationToken token = default);
+
+ ///
+ /// добавить метку на файл
+ ///
+ ///
+ ///
+ ///
+ ///
+ Task CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token);
+
+ ///
+ /// Получить запись по id
+ ///
+ ///
+ ///
+ ///
+ Task GetOrDefaultAsync(int id, CancellationToken token);
}
}
diff --git a/AsbCloudInfrastructure/Repository/FileRepository.cs b/AsbCloudInfrastructure/Repository/FileRepository.cs
index efa6296a..2726c2b7 100644
--- a/AsbCloudInfrastructure/Repository/FileRepository.cs
+++ b/AsbCloudInfrastructure/Repository/FileRepository.cs
@@ -1,4 +1,5 @@
using AsbCloudApp.Data;
+using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
@@ -28,25 +29,6 @@ namespace AsbCloudInfrastructure.Repository
.Include(f => f.Well);
}
- public async Task 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> GetInfosByCategoryAsync(int idWell, int idCategory, CancellationToken token)
{
var entities = await dbSetConfigured
@@ -126,22 +108,6 @@ namespace AsbCloudInfrastructure.Repository
return result;
}
- public async Task 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> GetInfoByIdsAsync(IEnumerable idsFile, CancellationToken token)
{
var result = new List();
@@ -178,26 +144,20 @@ namespace AsbCloudInfrastructure.Repository
public async Task> DeleteAsync(IEnumerable ids, CancellationToken token)
{
- var filesQuery = db.Files
- .Where(f => ids.Contains(f.Id));
+ var query = dbSetConfigured
+ .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 {
- Id = x.Id,
- IdWell = x.Id,
- IdCategory = x.IdCategory,
- Name = x.Name
- });
+ var filesDtos = files.Select(x => Convert(x));
- db.Files.RemoveRange(filesQuery);
+ db.Files.RemoveRange(query);
await db.SaveChangesAsync(token).ConfigureAwait(false);
return filesDtos;
}
- public async Task GetByMarkId(int idMark,
- CancellationToken token)
+ public async Task GetByMarkId(int idMark, CancellationToken token)
{
var entity = await dbSetConfigured
.FirstOrDefaultAsync(f => f.FileMarks.Any(m => m.Id == idMark), token)
@@ -271,5 +231,75 @@ namespace AsbCloudInfrastructure.Repository
});
return dto;
}
+
+ public async Task> GetAllAsync(CancellationToken token)
+ => await dbSetConfigured.AsNoTracking()
+ .Select(x => Convert(x))
+ .ToListAsync(token)
+ .ConfigureAwait(false);
+
+ public async Task 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 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 InsertRangeAsync(IEnumerable newItems, CancellationToken token)
+ {
+ throw new NotImplementedException();
+ }
+
+ public Task UpdateAsync(FileInfoDto item, CancellationToken token)
+ {
+ throw new NotImplementedException();
+ }
+
+ public Task DeleteAsync(int id, CancellationToken token)
+ {
+ throw new NotImplementedException();
+ }
}
}
diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs
index c30fd23b..6970d9b9 100644
--- a/AsbCloudInfrastructure/Services/FileService.cs
+++ b/AsbCloudInfrastructure/Services/FileService.cs
@@ -36,7 +36,14 @@ namespace AsbCloudInfrastructure.Services
var sysFileInfo = new System.IO.FileInfo(srcFilePath);
//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);
string filePath = MakeFilePath(idWell, idCategory, destinationFileName, fileId);
@@ -50,7 +57,16 @@ namespace AsbCloudInfrastructure.Services
string fileFullName, Stream fileStream, CancellationToken token)
{
//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);
//save stream to disk
@@ -73,7 +89,7 @@ namespace AsbCloudInfrastructure.Services
public async Task GetInfoAsync(int idFile,
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);
@@ -88,7 +104,7 @@ namespace AsbCloudInfrastructure.Services
}
public Task DeleteAsync(int idFile, CancellationToken token)
- => DeleteAsync(new int[] { idFile }, token);
+ => DeleteAsync(new int[] { idFile }, token);
public async Task DeleteAsync(IEnumerable ids, CancellationToken token)
{
@@ -112,7 +128,7 @@ namespace AsbCloudInfrastructure.Services
public async Task 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));
}
@@ -145,5 +161,23 @@ namespace AsbCloudInfrastructure.Services
return result;
}
+
+ public async Task> 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 MarkAsDeletedAsync(int idFile, CancellationToken token = default)
+ => await fileRepository.MarkAsDeletedAsync(idFile, token)
+ .ConfigureAwait(false);
+
+ public async Task CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
+ => await fileRepository.CreateFileMarkAsync(fileMarkDto, idUser, token)
+ .ConfigureAwait(false);
+
+ public async Task GetOrDefaultAsync(int id, CancellationToken token)
+ => await fileRepository.GetOrDefaultAsync(id, token)
+ .ConfigureAwait(false);
}
}
diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs
index b75f71f4..f598bc34 100644
--- a/AsbCloudWebApi/Controllers/FileController.cs
+++ b/AsbCloudWebApi/Controllers/FileController.cs
@@ -1,10 +1,11 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
-using AsbCloudInfrastructure.Repository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
+using System.Collections;
+using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@@ -21,13 +22,11 @@ namespace AsbCloudWebApi.Controllers
{
private readonly IFileService fileService;
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.wellService = wellService;
- this.fileRepository = fileRepository;
}
///
@@ -101,7 +100,7 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false))
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);
return Ok(filesInfo);
@@ -171,7 +170,7 @@ namespace AsbCloudWebApi.Controllers
if (!userService.HasPermission((int)idUser, $"File.edit{file.IdCategory}"))
return Forbid();
- var result = await fileRepository.MarkAsDeletedAsync(idFile, token);
+ var result = await fileService.MarkAsDeletedAsync(idFile, token);
return Ok(result);
}
@@ -196,7 +195,7 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false))
return Forbid();
- var result = await fileRepository.CreateFileMarkAsync(markDto, (int)idUser, token)
+ var result = await fileService.CreateFileMarkAsync(markDto, (int)idUser, token)
.ConfigureAwait(false);
return Ok(result);
@@ -246,7 +245,7 @@ namespace AsbCloudWebApi.Controllers
try
{
- var fileInfo = await fileRepository.GetInfoAsync(idFile, token).ConfigureAwait(false);
+ var fileInfo = await fileService.GetOrDefaultAsync(idFile, token).ConfigureAwait(false);
return Ok(fileInfo);
}
catch (FileNotFoundException ex)