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> /// <summary>
/// Put (update) DataListSavedFilter /// Put (update) DataListSavedFilter
/// </summary> /// </summary>
/// <param name="inObj"></param> /// <param name="updatedObject"></param>
/// <returns></returns> /// <returns></returns>
[HttpPut] [HttpPut]
public async Task<IActionResult> PutDataListSavedFilter([FromBody] DataListSavedFilter inObj) public async Task<IActionResult> PutDataListSavedFilter([FromBody] DataListSavedFilter updatedObject)
{ {
if (!serverState.IsOpen) if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
@@ -113,26 +113,16 @@ namespace AyaNova.Api.Controllers
//Instantiate the business object handler //Instantiate the business object handler
DataListSavedFilterBiz biz = DataListSavedFilterBiz.GetBiz(ct, HttpContext); DataListSavedFilterBiz biz = DataListSavedFilterBiz.GetBiz(ct, HttpContext);
var o = await biz.GetAsync(inObj.Id); var o = await biz.PutAsync(updatedObject);
if (o == null) 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)); 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 //do stuff with datafilter
DataListSavedFilter outObj = inObj; DataListSavedFilter outObj = inObj;
outObj.UserId = UserId; outObj.UserId = UserId;
await ct.DataListSavedFilter.AddAsync(outObj); await ct.DataListSavedFilter.AddAsync(outObj);
await ct.SaveChangesAsync(); await ct.SaveChangesAsync();
//Handle child and associated items:
//EVENT LOG
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
return outObj; 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 //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)); 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; return ret;
} }
@@ -139,29 +125,38 @@ namespace AyaNova.Biz
// //
//put //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 var dbObject = await GetAsync(putObject.Id);
if (inObj.UserId == 0) if (dbObject == null)
inObj.UserId = dbObject.UserId; {
AddError(ApiErrorCode.NOT_FOUND, "id");
return null;
}
//Replace the db object with the PUT object //preserve the owner ID if none was specified
CopyObject.Copy(inObj, dbObject, "Id"); if (putObject.UserId == 0)
//Set "original" value of concurrency token to input token putObject.UserId = dbObject.UserId;
//this will allow EF to check it out
ct.Entry(dbObject).OriginalValues["Concurrency"] = inObj.Concurrency;
Validate(dbObject, false); Validate(dbObject, false);
if (HasErrors) if (HasErrors)
return false; return null;
await ct.SaveChangesAsync();
//Log modification and save context ct.Replace(dbObject, putObject);
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct); try
{
await ct.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!await ExistsAsync(putObject.Id))
AddError(ApiErrorCode.NOT_FOUND);
else
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
return null;
}
return dbObject;
return true;
} }