This commit is contained in:
2021-02-25 22:43:16 +00:00
parent e48b96f60a
commit 8c31bd19a0
2 changed files with 35 additions and 7 deletions

View File

@@ -265,7 +265,7 @@ namespace AyaNova.Api.Controllers
///<param name="id">PartId</param>
/// <returns></returns>
[HttpPut("part-cost/{id}")]
public async Task<IActionResult> PutPartSerials([FromRoute] long id, [FromBody] decimal newCost)
public async Task<IActionResult> PutPartCost([FromRoute] long id, [FromBody] decimal newCost)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
@@ -274,18 +274,15 @@ namespace AyaNova.Api.Controllers
PartBiz biz = PartBiz.GetBiz(ct, HttpContext);
if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse());
var o = await biz.PutPartCostAsync(id, newCost);
if (o == null)
if (await biz.PutPartCostAsync(id, newCost) == false)
{
if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
return StatusCode(409, new ApiErrorResponse(biz.Errors));
else
return BadRequest(new ApiErrorResponse(biz.Errors));
}
return Ok(ApiOkResponse.Response(o));
return NoContent();
}

View File

@@ -348,6 +348,37 @@ namespace AyaNova.Biz
return ret;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//UPDATE PART COST
//
internal async Task<bool> PutPartCostAsync(long id, decimal newCost)
{
var dbObject = await ct.Part.FirstOrDefaultAsync(x => x.Id == id);
if (dbObject == null)
{
AddError(ApiErrorCode.NOT_FOUND, "id");
return false;
}
dbObject.Cost = newCost;
try
{
await ct.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!await ExistsAsync(id))
AddError(ApiErrorCode.NOT_FOUND);
else
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
return false;
}
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//SEARCH
//