diff --git a/server/AyaNova/Controllers/FormUserOptionsController.cs b/server/AyaNova/Controllers/FormUserOptionsController.cs index 69fdb893..d138ce8c 100644 --- a/server/AyaNova/Controllers/FormUserOptionsController.cs +++ b/server/AyaNova/Controllers/FormUserOptionsController.cs @@ -37,7 +37,7 @@ namespace AyaNova.Api.Controllers } /// - /// Create FormUserOptions + /// Create or Replace FormUserOptions /// /// /// From route path @@ -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)); } - /// - /// Update FormUserOptions - /// - /// - /// - [HttpPut] - public async Task 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 })); ; - } + // /// + // /// Update FormUserOptions + // /// + // /// + // /// + // [HttpPut] + // public async Task 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 })); ; + // } /// /// Delete FormUserOptions diff --git a/server/AyaNova/biz/FormUserOptionsBiz.cs b/server/AyaNova/biz/FormUserOptionsBiz.cs index e878d59a..3152a770 100644 --- a/server/AyaNova/biz/FormUserOptionsBiz.cs +++ b/server/AyaNova/biz/FormUserOptionsBiz.cs @@ -38,13 +38,16 @@ namespace AyaNova.Biz //////////////////////////////////////////////////////////////////////////////////////////////// //CREATE // - internal async Task CreateAsync(FormUserOptions newObject) + internal async Task 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 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 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 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."); + // } - } + // } ///////////////////////////////////////////////////////////////////// diff --git a/server/AyaNova/models/FormUserOptions.cs b/server/AyaNova/models/FormUserOptions.cs index 13c5531d..0b73bd70 100644 --- a/server/AyaNova/models/FormUserOptions.cs +++ b/server/AyaNova/models/FormUserOptions.cs @@ -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; } } }