This commit is contained in:
2021-02-04 15:14:58 +00:00
parent 9f0c111d3d
commit 38f6cdf421
2 changed files with 34 additions and 49 deletions

View File

@@ -99,10 +99,10 @@ namespace AyaNova.Api.Controllers
/// <summary>
/// Put (update) DataListSavedFilter
/// </summary>
/// <param name="inObj"></param>
/// <param name="updatedObject"></param>
/// <returns></returns>
[HttpPut]
public async Task<IActionResult> PutDataListSavedFilter([FromBody] DataListSavedFilter inObj)
public async Task<IActionResult> PutDataListSavedFilter([FromBody] DataListSavedFilter updatedObject)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
@@ -113,26 +113,16 @@ namespace AyaNova.Api.Controllers
//Instantiate the business object handler
DataListSavedFilterBiz biz = DataListSavedFilterBiz.GetBiz(ct, HttpContext);
var o = await biz.GetAsync(inObj.Id);
var o = await biz.PutAsync(updatedObject);
if (o == null)
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
// if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
// return StatusCode(403, new ApiNotAuthorizedResponse());
try
{
if (!await biz.PutAsync(o, inObj))
if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
return StatusCode(409, new ApiErrorResponse(biz.Errors));
else
return BadRequest(new ApiErrorResponse(biz.Errors));
}
catch (DbUpdateConcurrencyException)
{
if (!await biz.ExistsAsync(inObj.Id))
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
else
return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT));
}
return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
}

View File

@@ -57,18 +57,8 @@ namespace AyaNova.Biz
//do stuff with datafilter
DataListSavedFilter outObj = inObj;
outObj.UserId = UserId;
await ct.DataListSavedFilter.AddAsync(outObj);
await ct.SaveChangesAsync();
//Handle child and associated items:
//EVENT LOG
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
return outObj;
}
@@ -85,11 +75,7 @@ namespace AyaNova.Biz
{
//This is simple so nothing more here, but often will be copying to a different output object or some other ops
var ret = await ct.DataListSavedFilter.AsNoTracking().SingleOrDefaultAsync(z => z.Id == fetchId && (z.Public == true || z.UserId == UserId));
// if (logTheGetEvent && ret != null)
// {
// //Log
// await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
// }
return ret;
}
@@ -139,29 +125,38 @@ namespace AyaNova.Biz
//
//put
internal async Task<bool> PutAsync(DataListSavedFilter dbObject, DataListSavedFilter inObj)
internal async Task<DataListSavedFilter> PutAsync(DataListSavedFilter putObject)
{
//preserve the owner ID if none was specified
if (inObj.UserId == 0)
inObj.UserId = dbObject.UserId;
var dbObject = await GetAsync(putObject.Id);
if (dbObject == null)
{
AddError(ApiErrorCode.NOT_FOUND, "id");
return null;
}
//Replace the db object with the PUT object
CopyObject.Copy(inObj, dbObject, "Id");
//Set "original" value of concurrency token to input token
//this will allow EF to check it out
ct.Entry(dbObject).OriginalValues["Concurrency"] = inObj.Concurrency;
//preserve the owner ID if none was specified
if (putObject.UserId == 0)
putObject.UserId = dbObject.UserId;
Validate(dbObject, false);
if (HasErrors)
return false;
await ct.SaveChangesAsync();
return null;
//Log modification and save context
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
ct.Replace(dbObject, putObject);
try
{
await ct.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!await ExistsAsync(putObject.Id))
AddError(ApiErrorCode.NOT_FOUND);
else
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
return null;
}
return true;
return dbObject;
}