CS2-59: Added Delete() to File controller

This commit is contained in:
KharchenkoVV 2021-08-19 16:58:26 +05:00
parent e8d98a0887
commit 8b5af8d32e
3 changed files with 48 additions and 9 deletions

View File

@ -23,5 +23,8 @@ namespace AsbCloudApp.Services
Task<FileInfoDto> GetFileInfoAsync(int fileId, Task<FileInfoDto> GetFileInfoAsync(int fileId,
CancellationToken token); CancellationToken token);
Task<int> DeleteFileAsync(int idFile,
CancellationToken token = default);
} }
} }

View File

@ -125,5 +125,18 @@ namespace AsbCloudInfrastructure.Services
dto.AuthorName = entity.Author.Name; dto.AuthorName = entity.Author.Name;
return dto; return dto;
} }
public async Task<int> 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);
}
} }
} }

View File

@ -35,7 +35,7 @@ namespace AsbCloudWebApi.Controllers
/// <returns></returns> /// <returns></returns>
[HttpPost] [HttpPost]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> SaveFilesAsync(int idWell, int idCategory, public async Task<IActionResult> SaveFilesAsync(int idWell, int idCategory,
[FromForm] IFormFileCollection files, CancellationToken token = default) [FromForm] IFormFileCollection files, CancellationToken token = default)
{ {
int? idCompany = User.GetCompanyId(); int? idCompany = User.GetCompanyId();
@ -44,9 +44,9 @@ namespace AsbCloudWebApi.Controllers
if (idCompany is null || idUser is null) if (idCompany is null || idUser is null)
return Forbid(); return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false)) idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var fileInfoCollection = files.Select(f => var fileInfoCollection = files.Select(f =>
(f.FileName, idWell, idCategory, DateTime.Now, (int)idUser)); (f.FileName, idWell, idCategory, DateTime.Now, (int)idUser));
@ -63,7 +63,7 @@ namespace AsbCloudWebApi.Controllers
var fileStream = file.OpenReadStream(); var fileStream = file.OpenReadStream();
await fileService.SaveFile(idWell, idCategory, fileId, await fileService.SaveFile(idWell, idCategory, fileId,
fileExtension, fileStream); fileExtension, fileStream);
} }
@ -89,9 +89,9 @@ namespace AsbCloudWebApi.Controllers
{ {
int? idCompany = User.GetCompanyId(); 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)) idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var filesInfo = await fileService.GetFilesInfoAsync(idWell, idCategory, var filesInfo = await fileService.GetFilesInfoAsync(idWell, idCategory,
begin, end, skip, take, token).ConfigureAwait(false); begin, end, skip, take, token).ConfigureAwait(false);
@ -112,7 +112,7 @@ namespace AsbCloudWebApi.Controllers
[HttpGet] [HttpGet]
[Route("{fileId}")] [Route("{fileId}")]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFileAsync([FromRoute] int idWell, public async Task<IActionResult> GetFileAsync([FromRoute] int idWell,
int fileId, CancellationToken token = default) int fileId, CancellationToken token = default)
{ {
try try
@ -122,9 +122,9 @@ namespace AsbCloudWebApi.Controllers
if (idCompany is null) if (idCompany is null)
return Forbid(); return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false)) idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var fileInfo = await fileService.GetFileInfoAsync(fileId, token); var fileInfo = await fileService.GetFileInfoAsync(fileId, token);
@ -141,5 +141,28 @@ namespace AsbCloudWebApi.Controllers
return NotFound($"Файл не найден. Текст ошибки: {ex.Message}"); return NotFound($"Файл не найден. Текст ошибки: {ex.Message}");
} }
} }
/// <summary>
/// Удаляет файл с диска на сервере
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="idFile">id запрашиваемого файла</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns></returns>
[HttpDelete("{idFile}")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> 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);
}
} }
} }