DrillingProgram replace idPart by idFileCategory.

This commit is contained in:
Фролов 2022-02-18 14:16:35 +05:00
parent 7a77ff7904
commit 4863b69cfe
3 changed files with 43 additions and 37 deletions

View File

@ -8,16 +8,16 @@ namespace AsbCloudApp.Services
{ {
public interface IDrillingProgramService public interface IDrillingProgramService
{ {
Task<IEnumerable<UserDto>> GetAvailableUsers(int idWell, CancellationToken token = default);
Task<IEnumerable<FileCategoryDto>> GetCategoriesAsync(CancellationToken token = default); Task<IEnumerable<FileCategoryDto>> GetCategoriesAsync(CancellationToken token = default);
Task<DrillingProgramStateDto> GetStateAsync(int idWell, int fileChangerId, Task<DrillingProgramStateDto> GetStateAsync(int idWell, int idUser,
CancellationToken token = default); CancellationToken token = default);
Task<int> AddOrReplaceFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token); Task<int> AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default);
Task<int> MarkAsDeletedFileMarkAsync(int idFileMark, CancellationToken token);
Task<int> AddPartsAsync(int idWell, IEnumerable<int> idFileCategories, CancellationToken token = default); Task<int> AddPartsAsync(int idWell, IEnumerable<int> idFileCategories, CancellationToken token = default);
Task<int> RemovePartsAsync(int idWell, IEnumerable<int> idFileCategories, CancellationToken token = default); Task<int> RemovePartsAsync(int idWell, IEnumerable<int> idFileCategories, CancellationToken token = default);
Task<int> AddUserAsync(int idUser, int idPart, int idUserRole, CancellationToken token = default); Task<int> AddUserAsync(int idWell, int idFileCategory, int idUser, int idUserRole, CancellationToken token = default);
Task<int> RemoveUserAsync(int idUser, int idPart, int idUserRole, CancellationToken token = default); Task<int> RemoveUserAsync(int idWell, int idFileCategory, int idUser, int idUserRole, CancellationToken token = default);
Task<int> AddFile(int idPart, int idUser, string fileFullName, Stream fileStream, CancellationToken token = default); Task<int> AddOrReplaceFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token);
Task<IEnumerable<UserDto>> GetAvailableUsers(int idWell, CancellationToken token = default); Task<int> MarkAsDeletedFileMarkAsync(int idFileMark, CancellationToken token);
} }
} }

View File

@ -138,14 +138,14 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
return state; return state;
} }
public async Task<int> AddFile(int idPart, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default) public async Task<int> AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default)
{ {
var part = await context.DrillingProgramParts var part = await context.DrillingProgramParts
.Include(p => p.RelatedUsers) .Include(p => p.RelatedUsers)
.FirstOrDefaultAsync(p => p.Id == idPart, token); .FirstOrDefaultAsync(p => p.IdWell == idWell && p.IdFileCategory == idFileCategory, token);
if (part == null) if (part == null)
throw new ArgumentInvalidException($"DrillingProgramPart id == {idPart} does not exist", nameof(idPart)); throw new ArgumentInvalidException($"DrillingProgramPart id == {idFileCategory} does not exist", nameof(idFileCategory));
if (! part.RelatedUsers.Any(r => r.IdUser == idUser && r.IdUserRole == idUserRolePublisher)) if (! part.RelatedUsers.Any(r => r.IdUser == idUser && r.IdUserRole == idUserRolePublisher))
throw new ForbidException($"User {idUser} is not in the publisher list."); throw new ForbidException($"User {idUser} is not in the publisher list.");
@ -198,45 +198,51 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
return await context.SaveChangesAsync(token); return await context.SaveChangesAsync(token);
} }
public async Task<int> AddUserAsync(int idUser, int idPart, int idUserRole, CancellationToken token = default) public async Task<int> AddUserAsync(int idWell, int idFileCategory, int idUser, int idUserRole, CancellationToken token = default)
{ {
var user = await userService.GetAsync(idUser, token); var user = await userService.GetAsync(idUser, token);
if (user is null) if (user is null)
throw new ArgumentInvalidException($"User id == {idUser} does not exist", nameof(idUser)); throw new ArgumentInvalidException($"User id == {idUser} does not exist", nameof(idUser));
var drillingProgramPart = await context.DrillingProgramParts.FirstOrDefaultAsync(p => p.Id == idPart, token); var part = await context.DrillingProgramParts
if (drillingProgramPart is null) .FirstOrDefaultAsync(p => p.IdWell == idWell && p.IdFileCategory == idFileCategory, token);
throw new ArgumentInvalidException($"DrillingProgramPart id == {idPart} does not exist", nameof(idPart));
if (part is null)
throw new ArgumentInvalidException($"DrillingProgramPart idFileCategory == {idFileCategory} does not exist", nameof(idFileCategory));
if (idUserRole != idUserRoleApprover && idUserRole != idUserRolePublisher) if (idUserRole != idUserRoleApprover && idUserRole != idUserRolePublisher)
throw new ArgumentInvalidException($"idUserRole ({idPart}), should be approver ({idUserRoleApprover}) or publisher ({idUserRolePublisher})", nameof(idPart)); throw new ArgumentInvalidException($"idUserRole ({idUserRole}), should be approver ({idUserRoleApprover}) or publisher ({idUserRolePublisher})", nameof(idUserRole));
var oldRelation = await context.RelationDrillingProgramPartUsers
.FirstOrDefaultAsync(r => r.IdUser == idUser && r.IdDrillingProgramPart == part.Id, token);
if(oldRelation is not null)
context.RelationDrillingProgramPartUsers.Remove(oldRelation);
var newRelation = new RelationUserDrillingProgramPart var newRelation = new RelationUserDrillingProgramPart
{ {
IdUser = idUser, IdUser = idUser,
IdDrillingProgramPart = idPart, IdDrillingProgramPart = part.Id,
IdUserRole = idUserRole, IdUserRole = idUserRole,
}; };
context.RelationDrillingProgramPartUsers.Add(newRelation); context.RelationDrillingProgramPartUsers.Add(newRelation);
if(idUserRole == idUserRoleApprover) if(idUserRole == idUserRoleApprover)
await RemoveDrillingProgramAsync(drillingProgramPart.IdWell, token); await RemoveDrillingProgramAsync(part.IdWell, token);
return await context.SaveChangesAsync(token); return await context.SaveChangesAsync(token);
} }
public async Task<int> RemoveUserAsync(int idUser, int idPart, int idUserRole, CancellationToken token = default) public async Task<int> RemoveUserAsync(int idWell, int idFileCategory, int idUser, int idUserRole, CancellationToken token = default)
{ {
var whereQuery = context.RelationDrillingProgramPartUsers var whereQuery = context.RelationDrillingProgramPartUsers
.Include(r => r.DrillingProgramPart)
.Where(r => r.IdUser == idUser && .Where(r => r.IdUser == idUser &&
r.IdDrillingProgramPart == idPart && r.IdUserRole == idUserRole &&
r.IdUserRole == idUserRole); r.DrillingProgramPart.IdWell == idWell &&
r.DrillingProgramPart.IdFileCategory == idFileCategory);
context.RelationDrillingProgramPartUsers.RemoveRange(whereQuery); context.RelationDrillingProgramPartUsers.RemoveRange(whereQuery);
if (idUserRole == idUserRoleApprover)
{
var part = await context.DrillingProgramParts.FirstOrDefaultAsync(p => p.Id == idPart, token);
}
return await context.SaveChangesAsync(token); return await context.SaveChangesAsync(token);
} }

View File

@ -82,17 +82,17 @@ namespace AsbCloudWebApi.Controllers
/// <summary> /// <summary>
/// Загрузка файла для части программы бурения /// Загрузка файла для части программы бурения
/// </summary> /// </summary>
/// <param name="idPart">ID части программы. Не путать с категорией файла</param>
/// <param name="idWell"></param> /// <param name="idWell"></param>
/// <param name="idFileCategory">ID части программы. Не путать с категорией файла</param>
/// <param name="files"></param> /// <param name="files"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
[HttpPost("part/{idPart}")] [HttpPost("part/{idFileCategory}")]
[Permission] [Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddFile( public async Task<IActionResult> AddFile(
int idPart,
int idWell, int idWell,
int idFileCategory,
[FromForm] IFormFileCollection files, [FromForm] IFormFileCollection files,
CancellationToken token) CancellationToken token)
{ {
@ -118,7 +118,7 @@ namespace AsbCloudWebApi.Controllers
return BadRequest(ArgumentInvalidException.MakeValidationError("file", "Файл должен быть xlsx")); return BadRequest(ArgumentInvalidException.MakeValidationError("file", "Файл должен быть xlsx"));
var fileStream = files[0].OpenReadStream(); var fileStream = files[0].OpenReadStream();
var result = await drillingProgramService.AddFile(idPart, (int)idUser, fileName, fileStream, token); var result = await drillingProgramService.AddFile(idWell, idFileCategory, (int)idUser, fileName, fileStream, token);
return Ok(result); return Ok(result);
} }
@ -180,14 +180,14 @@ namespace AsbCloudWebApi.Controllers
/// </summary> /// </summary>
/// <param name="idWell"></param> /// <param name="idWell"></param>
/// <param name="idUser"></param> /// <param name="idUser"></param>
/// <param name="idPart"></param> /// <param name="idFileCategory"></param>
/// <param name="idUserRole"></param> /// <param name="idUserRole"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
[HttpPost("part/{idPart}/user")] [HttpPost("part/{idFileCategory}/user")]
[Permission] [Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddUserAsync(int idWell, [Required] int idUser, int idPart, [Required] int idUserRole, CancellationToken token = default) public async Task<IActionResult> AddUserAsync(int idWell, [Required] int idUser, int idFileCategory, [Required] int idUserRole, CancellationToken token = default)
{ {
int? idCompany = User.GetCompanyId(); int? idCompany = User.GetCompanyId();
int? idUserEditor = User.GetUserId(); int? idUserEditor = User.GetUserId();
@ -199,7 +199,7 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false)) idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var result = await drillingProgramService.AddUserAsync(idUser, idPart, idUserRole, token); var result = await drillingProgramService.AddUserAsync(idWell, idFileCategory, idUser, idUserRole, token);
return Ok(result); return Ok(result);
} }
@ -208,14 +208,14 @@ namespace AsbCloudWebApi.Controllers
/// </summary> /// </summary>
/// <param name="idWell"></param> /// <param name="idWell"></param>
/// <param name="idUser"></param> /// <param name="idUser"></param>
/// <param name="idPart"></param> /// <param name="idFileCategory"></param>
/// <param name="idUserRole"></param> /// <param name="idUserRole"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
[HttpDelete("part/{idPart}/user/{idUser}")] [HttpDelete("part/{idFileCategory}/user/{idUser}")]
[Permission] [Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> RemoveUserAsync(int idWell, int idUser, int idPart, [Required]int idUserRole, CancellationToken token = default) public async Task<IActionResult> RemoveUserAsync(int idWell, int idUser, int idFileCategory, [Required]int idUserRole, CancellationToken token = default)
{ {
int? idCompany = User.GetCompanyId(); int? idCompany = User.GetCompanyId();
int? idUserEditor = User.GetUserId(); int? idUserEditor = User.GetUserId();
@ -227,7 +227,7 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false)) idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var result = await drillingProgramService.RemoveUserAsync(idUser, idPart, idUserRole, token); var result = await drillingProgramService.RemoveUserAsync(idWell, idFileCategory,idUser, idUserRole, token);
return Ok(result); return Ok(result);
} }