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,
CancellationToken token);
Task<int> DeleteFileAsync(int idFile,
CancellationToken token = default);
}
}

View File

@ -125,5 +125,18 @@ namespace AsbCloudInfrastructure.Services
dto.AuthorName = entity.Author.Name;
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>
[HttpPost]
[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)
{
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<IActionResult> GetFileAsync([FromRoute] int idWell,
public async Task<IActionResult> 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}");
}
}
/// <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);
}
}
}