This commit is contained in:
2021-09-22 18:45:39 +00:00
parent eb45e14e1c
commit 0f9baa74ed
3 changed files with 96 additions and 94 deletions

View File

@@ -37,7 +37,7 @@ namespace AyaNova.Api.Controllers
} }
/// <summary> /// <summary>
/// Create FormUserOptions /// Create or Replace FormUserOptions
/// </summary> /// </summary>
/// <param name="newObject"></param> /// <param name="newObject"></param>
/// <param name="apiVersion">From route path</param> /// <param name="apiVersion">From route path</param>
@@ -52,11 +52,11 @@ namespace AyaNova.Api.Controllers
return StatusCode(403, new ApiNotAuthorizedResponse()); return StatusCode(403, new ApiNotAuthorizedResponse());
if (!ModelState.IsValid) if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState)); return BadRequest(new ApiErrorResponse(ModelState));
FormUserOptions o = await biz.CreateAsync(newObject); FormUserOptions o = await biz.UpsertAsync(newObject);
if (o == null) if (o == null)
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
else else
return CreatedAtAction(nameof(FormUserOptionsController.GetFormUserOptions), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); return CreatedAtAction(nameof(FormUserOptionsController.GetFormUserOptions), new { formKey = o.FormKey, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
} }
@@ -80,31 +80,31 @@ namespace AyaNova.Api.Controllers
return Ok(ApiOkResponse.Response(o)); return Ok(ApiOkResponse.Response(o));
} }
/// <summary> // /// <summary>
/// Update FormUserOptions // /// Update FormUserOptions
/// </summary> // /// </summary>
/// <param name="updatedObject"></param> // /// <param name="updatedObject"></param>
/// <returns></returns> // /// <returns></returns>
[HttpPut] // [HttpPut]
public async Task<IActionResult> PutFormUserOptions([FromBody] FormUserOptions updatedObject) // public async Task<IActionResult> PutFormUserOptions([FromBody] FormUserOptions 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));
if (!ModelState.IsValid) // if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState)); // return BadRequest(new ApiErrorResponse(ModelState));
FormUserOptionsBiz biz = FormUserOptionsBiz.GetBiz(ct, HttpContext); // FormUserOptionsBiz biz = FormUserOptionsBiz.GetBiz(ct, HttpContext);
if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) // if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse()); // return StatusCode(403, new ApiNotAuthorizedResponse());
var o = await biz.PutAsync(updatedObject); // var o = await biz.PutAsync(updatedObject);
if (o == null) // if (o == null)
{ // {
if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT)) // if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
return StatusCode(409, new ApiErrorResponse(biz.Errors)); // return StatusCode(409, new ApiErrorResponse(biz.Errors));
else // else
return BadRequest(new ApiErrorResponse(biz.Errors)); // return BadRequest(new ApiErrorResponse(biz.Errors));
} // }
return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency })); ; // return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency })); ;
} // }
/// <summary> /// <summary>
/// Delete FormUserOptions /// Delete FormUserOptions

View File

@@ -38,13 +38,16 @@ namespace AyaNova.Biz
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//CREATE //CREATE
// //
internal async Task<FormUserOptions> CreateAsync(FormUserOptions newObject) internal async Task<FormUserOptions> UpsertAsync(FormUserOptions newObject)
{ {
Validate(newObject, null); //Validate(newObject, null);
newObject.UserId=UserId;//always defaults to currently logged in user
if (HasErrors) if (HasErrors)
return null; return null;
else else
{ {
//remove any prior version that might exist (or might not)
await DeleteAsync(newObject.FormKey);
newObject.Options = JsonUtil.CompactJson(newObject.Options); newObject.Options = JsonUtil.CompactJson(newObject.Options);
await ct.FormUserOptions.AddAsync(newObject); await ct.FormUserOptions.AddAsync(newObject);
await ct.SaveChangesAsync(); await ct.SaveChangesAsync();
@@ -63,64 +66,63 @@ namespace AyaNova.Biz
return ret; return ret;
} }
//////////////////////////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////////////////////////
//UPDATE // //UPDATE
// // //
internal async Task<FormUserOptions> PutAsync(FormUserOptions putObject) // internal async Task<FormUserOptions> PutAsync(FormUserOptions putObject)
{ // {
var dbObject = await GetAsync(putObject.FormKey); // var dbObject = await GetAsync(putObject.FormKey);
if (dbObject == null) // if (dbObject == null)
{ // {
AddError(ApiErrorCode.NOT_FOUND, "formKey"); // AddError(ApiErrorCode.NOT_FOUND, "formKey");
return null; // return null;
} // }
if (dbObject.Concurrency != putObject.Concurrency) // if (dbObject.Concurrency != putObject.Concurrency)
{ // {
AddError(ApiErrorCode.CONCURRENCY_CONFLICT); // AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
return null; // return null;
} // }
putObject.Options = JsonUtil.CompactJson(putObject.Options); // putObject.Options = JsonUtil.CompactJson(putObject.Options);
Validate(putObject, dbObject); // Validate(putObject, dbObject);
if (HasErrors) return null; // if (HasErrors) return null;
ct.Replace(dbObject, putObject); // ct.Replace(dbObject, putObject);
try // try
{ // {
await ct.SaveChangesAsync(); // await ct.SaveChangesAsync();
} // }
catch (DbUpdateConcurrencyException) // catch (DbUpdateConcurrencyException)
{ // {
if (!await ExistsAsync(putObject.Id)) // if (!await ExistsAsync(putObject.Id))
AddError(ApiErrorCode.NOT_FOUND); // AddError(ApiErrorCode.NOT_FOUND);
else // else
AddError(ApiErrorCode.CONCURRENCY_CONFLICT); // AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
return null; // return null;
} // }
return putObject; // return putObject;
} // }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//DELETE //DELETE
// //
internal async Task<bool> DeleteAsync(string formKey) internal async Task<bool> DeleteAsync(string formKey)
{ {
using (var transaction = await ct.Database.BeginTransactionAsync()) // using (var transaction = await ct.Database.BeginTransactionAsync())
// {
var dbObject = await GetAsync(formKey);
if (dbObject == null)
{ {
var dbObject = await GetAsync(formKey);
if (dbObject == null)
{
AddError(ApiErrorCode.NOT_FOUND);
return false;
}
ValidateCanDelete(dbObject);
if (HasErrors)
return false;
ct.FormUserOptions.Remove(dbObject);
await ct.SaveChangesAsync();
await transaction.CommitAsync();
return true; return true;
} }
// ValidateCanDelete(dbObject);
if (HasErrors)
return false;
ct.FormUserOptions.Remove(dbObject);
await ct.SaveChangesAsync();
// await transaction.CommitAsync();
return true;
// }
} }
@@ -129,22 +131,22 @@ namespace AyaNova.Biz
//VALIDATION //VALIDATION
// //
private void Validate(FormUserOptions proposedObj, FormUserOptions currentObj) // private void Validate(FormUserOptions proposedObj, FormUserOptions currentObj)
{ // {
if (proposedObj.UserId != UserId) // if (proposedObj.UserId != UserId)
{ // {
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "A user can only modify their own personal form settings. UserId does not match current api user logged in."); // AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "A user can only modify their own personal form settings. UserId does not match current api user logged in.");
} // }
} // }
private void ValidateCanDelete(FormUserOptions inObj) // private void ValidateCanDelete(FormUserOptions inObj)
{ // {
if (inObj.UserId != UserId) // if (inObj.UserId != UserId)
{ // {
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "A user can only modify their own personal form settings. UserId does not match current api user logged in."); // AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "A user can only modify their own personal form settings. UserId does not match current api user logged in.");
} // }
} // }
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////

View File

@@ -12,13 +12,13 @@ namespace AyaNova.Models
{ {
public long Id { get; set; } public long Id { get; set; }
public uint Concurrency { get; set; } public uint Concurrency { get; set; }
[Required, MaxLength(255)] [Required, MaxLength(255)]
public string FormKey { get; set; }//max 255 characters ascii set public string FormKey { get; set; }//max 255 characters ascii set
[Required] [Required]
public string Options { get; set; }//JSON fragment of form customization template, top level is array. public string Options { get; set; }//JSON fragment of form customization template, top level is array.
[Required] //this is set from logged in user id, not provided
public long UserId {get;set;} public long UserId { get; set; }
} }
} }