diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs index 2a345a37..fe96a6d6 100644 --- a/AsbCloudApp/Services/IFileService.cs +++ b/AsbCloudApp/Services/IFileService.cs @@ -23,5 +23,8 @@ namespace AsbCloudApp.Services Task GetFileInfoAsync(int fileId, CancellationToken token); + + Task DeleteFileAsync(int idFile, + CancellationToken token = default); } } diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs index 9b09ce02..d23b9fcb 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudInfrastructure/Services/FileService.cs @@ -125,5 +125,18 @@ namespace AsbCloudInfrastructure.Services dto.AuthorName = entity.Author.Name; return dto; } + + public async Task DeleteFileAsync(int idFile, + CancellationToken token = default) + { + var fileInfo = db.Files.FirstOrDefault(f => f.Id == idFile); + + if (fileInfo is null) + return 0; + + fileInfo.IsDeleted = true; + + return await db.SaveChangesAsync(token); + } } } diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs index 76cb5b45..30b83a1c 100644 --- a/AsbCloudWebApi/Controllers/FileController.cs +++ b/AsbCloudWebApi/Controllers/FileController.cs @@ -35,7 +35,7 @@ namespace AsbCloudWebApi.Controllers /// [HttpPost] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] - public async Task SaveFilesAsync(int idWell, int idCategory, + public async Task SaveFilesAsync(int idWell, int idCategory, [FromForm] IFormFileCollection files, CancellationToken token = default) { int? idCompany = User.GetCompanyId(); @@ -44,9 +44,9 @@ namespace AsbCloudWebApi.Controllers if (idCompany is null || idUser is null) return Forbid(); - if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, + if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false)) - return Forbid(); + return Forbid(); var fileInfoCollection = files.Select(f => (f.FileName, idWell, idCategory, DateTime.Now, (int)idUser)); @@ -63,7 +63,7 @@ namespace AsbCloudWebApi.Controllers var fileStream = file.OpenReadStream(); - await fileService.SaveFile(idWell, idCategory, fileId, + await fileService.SaveFile(idWell, idCategory, fileId, fileExtension, fileStream); } @@ -89,9 +89,9 @@ namespace AsbCloudWebApi.Controllers { int? idCompany = User.GetCompanyId(); - if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, + if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false)) - return Forbid(); + return Forbid(); var filesInfo = await fileService.GetFilesInfoAsync(idWell, idCategory, begin, end, skip, take, token).ConfigureAwait(false); @@ -112,7 +112,7 @@ namespace AsbCloudWebApi.Controllers [HttpGet] [Route("{fileId}")] [ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)] - public async Task GetFileAsync([FromRoute] int idWell, + public async Task GetFileAsync([FromRoute] int idWell, int fileId, CancellationToken token = default) { try @@ -122,9 +122,9 @@ namespace AsbCloudWebApi.Controllers if (idCompany is null) return Forbid(); - if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, + if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false)) - return Forbid(); + return Forbid(); var fileInfo = await fileService.GetFileInfoAsync(fileId, token); @@ -141,5 +141,28 @@ namespace AsbCloudWebApi.Controllers return NotFound($"Файл не найден. Текст ошибки: {ex.Message}"); } } + + /// + /// Удаляет файл с диска на сервере + /// + /// id скважины + /// id запрашиваемого файла + /// Токен отмены задачи + /// + [HttpDelete("{idFile}")] + [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] + public async Task DeleteAsync(int idWell, int idFile, + CancellationToken token = default) + { + int? idCompany = User.GetCompanyId(); + + if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, + idWell, token).ConfigureAwait(false)) + return Forbid(); + + var result = await fileService.DeleteFileAsync(idFile, token); + + return Ok(result); + } } }