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>
/// Create FormUserOptions
/// Create or Replace FormUserOptions
/// </summary>
/// <param name="newObject"></param>
/// <param name="apiVersion">From route path</param>
@@ -52,11 +52,11 @@ namespace AyaNova.Api.Controllers
return StatusCode(403, new ApiNotAuthorizedResponse());
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
FormUserOptions o = await biz.CreateAsync(newObject);
FormUserOptions o = await biz.UpsertAsync(newObject);
if (o == null)
return BadRequest(new ApiErrorResponse(biz.Errors));
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));
}
/// <summary>
/// Update FormUserOptions
/// </summary>
/// <param name="updatedObject"></param>
/// <returns></returns>
[HttpPut]
public async Task<IActionResult> PutFormUserOptions([FromBody] FormUserOptions updatedObject)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
FormUserOptionsBiz biz = FormUserOptionsBiz.GetBiz(ct, HttpContext);
if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse());
var o = await biz.PutAsync(updatedObject);
if (o == null)
{
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(new { Concurrency = o.Concurrency })); ;
}
// /// <summary>
// /// Update FormUserOptions
// /// </summary>
// /// <param name="updatedObject"></param>
// /// <returns></returns>
// [HttpPut]
// public async Task<IActionResult> PutFormUserOptions([FromBody] FormUserOptions updatedObject)
// {
// if (!serverState.IsOpen)
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// if (!ModelState.IsValid)
// return BadRequest(new ApiErrorResponse(ModelState));
// FormUserOptionsBiz biz = FormUserOptionsBiz.GetBiz(ct, HttpContext);
// if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
// return StatusCode(403, new ApiNotAuthorizedResponse());
// var o = await biz.PutAsync(updatedObject);
// if (o == null)
// {
// 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(new { Concurrency = o.Concurrency })); ;
// }
/// <summary>
/// Delete FormUserOptions

View File

@@ -38,13 +38,16 @@ namespace AyaNova.Biz
////////////////////////////////////////////////////////////////////////////////////////////////
//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)
return null;
else
{
//remove any prior version that might exist (or might not)
await DeleteAsync(newObject.FormKey);
newObject.Options = JsonUtil.CompactJson(newObject.Options);
await ct.FormUserOptions.AddAsync(newObject);
await ct.SaveChangesAsync();
@@ -63,64 +66,63 @@ namespace AyaNova.Biz
return ret;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//UPDATE
//
internal async Task<FormUserOptions> PutAsync(FormUserOptions putObject)
{
var dbObject = await GetAsync(putObject.FormKey);
if (dbObject == null)
{
AddError(ApiErrorCode.NOT_FOUND, "formKey");
return null;
}
if (dbObject.Concurrency != putObject.Concurrency)
{
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
return null;
}
// ////////////////////////////////////////////////////////////////////////////////////////////////
// //UPDATE
// //
// internal async Task<FormUserOptions> PutAsync(FormUserOptions putObject)
// {
// var dbObject = await GetAsync(putObject.FormKey);
// if (dbObject == null)
// {
// AddError(ApiErrorCode.NOT_FOUND, "formKey");
// return null;
// }
// if (dbObject.Concurrency != putObject.Concurrency)
// {
// AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
// return null;
// }
putObject.Options = JsonUtil.CompactJson(putObject.Options);
Validate(putObject, dbObject);
if (HasErrors) return null;
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;
}
// putObject.Options = JsonUtil.CompactJson(putObject.Options);
// Validate(putObject, dbObject);
// if (HasErrors) return null;
// 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 putObject;
}
// return putObject;
// }
////////////////////////////////////////////////////////////////////////////////////////////////
//DELETE
//
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;
}
// 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
//
private void Validate(FormUserOptions proposedObj, FormUserOptions currentObj)
{
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.");
}
}
// private void Validate(FormUserOptions proposedObj, FormUserOptions currentObj)
// {
// 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.");
// }
// }
private void ValidateCanDelete(FormUserOptions inObj)
{
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.");
}
// private void ValidateCanDelete(FormUserOptions inObj)
// {
// 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.");
// }
}
// }
/////////////////////////////////////////////////////////////////////

View File

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