From 5aacb87a937616bb2b8d2a5fa2f75e43c15e73ac Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 13 Feb 2020 20:22:42 +0000 Subject: [PATCH] --- .../Controllers/DataListTemplateController.cs | 181 ------------------ server/AyaNova/DataList/DataListFetcher.cs | 8 - server/AyaNova/biz/AyaType.cs | 7 +- .../AyaNova/biz/BizObjectExistsInDatabase.cs | 3 +- server/AyaNova/biz/BizObjectFactory.cs | 3 +- .../AyaNova/biz/BizObjectNameFetcherDirect.cs | 5 +- server/AyaNova/biz/BizRoles.cs | 9 +- server/AyaNova/biz/DataListTemplateBiz.cs | 124 ------------ server/AyaNova/models/AyContext.cs | 2 +- server/AyaNova/models/DataListTemplate.cs | 16 -- server/AyaNova/util/AySchema.cs | 3 +- 11 files changed, 9 insertions(+), 352 deletions(-) delete mode 100644 server/AyaNova/Controllers/DataListTemplateController.cs delete mode 100644 server/AyaNova/biz/DataListTemplateBiz.cs delete mode 100644 server/AyaNova/models/DataListTemplate.cs diff --git a/server/AyaNova/Controllers/DataListTemplateController.cs b/server/AyaNova/Controllers/DataListTemplateController.cs deleted file mode 100644 index bcd69bf3..00000000 --- a/server/AyaNova/Controllers/DataListTemplateController.cs +++ /dev/null @@ -1,181 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Authorization; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; - -using AyaNova.Models; -using AyaNova.Api.ControllerHelpers; -using AyaNova.Biz; -using AyaNova.DataList; - - -namespace AyaNova.Api.Controllers -{ - - [ApiController] - [ApiVersion("8.0")] - [Route("api/v{version:apiVersion}/[controller]")] - [Produces("application/json")] - [Authorize] - public class DataListTemplateController : ControllerBase - { - private readonly AyContext ct; - private readonly ILogger log; - private readonly ApiServerState serverState; - - - /// - /// ctor - /// - /// - /// - /// - public DataListTemplateController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) - { - ct = dbcontext; - log = logger; - serverState = apiServerState; - } - - - /// - /// Get full DataListTemplate object - /// - /// - /// A single DataListTemplate - [HttpGet("{DataListKey}")] - public async Task GetDataListTemplate([FromRoute] string DataListKey) - { - if (serverState.IsClosed) - return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - - //Attempt to get the data list by key to see if key is valid and for the default template and valid field names - var DataList = DataListFactory.GetAyaDataList(DataListKey); - if (DataList == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - //Instantiate the business object handler - DataListTemplateBiz biz = DataListTemplateBiz.GetBiz(ct, HttpContext); - - if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - var o = await biz.GetAsync(DataListKey, true, DataList); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); - } - - - /// - /// List of all DataList keys available - /// - /// List of strings - [HttpGet("ListKeys")] - public ActionResult GetDataListKeys() - { - if (!serverState.IsOpen) - { - return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } - - return Ok(ApiOkResponse.Response(DataListFactory.GetListOfAllDataListKeyNames(), true)); - } - - - /// - /// Put (update) DataListTemplate - /// - /// - /// - /// - [HttpPut("{DataListKey}")] - public async Task PutDataListTemplate([FromRoute] string DataListKey, [FromBody] DataListTemplate inObj) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - //Attempt to get the data list by key to see if key is valid and for the default template and valid field names - var DataList = DataListFactory.GetAyaDataList(DataListKey); - if (DataList == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - //Instantiate the business object handler - DataListTemplateBiz biz = DataListTemplateBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(DataListKey, false, DataList); - 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, DataList)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - // if (!await biz.ExistsAsync(id)) - // return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - // else - //these always exist so can only be concurrency conflict - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - - /// - /// Delete DataListTemplate - /// (Reset DataListTemplate to default) - /// - /// - /// Ok - [HttpDelete("{DataListKey}")] - public async Task DeleteDataListTemplate([FromRoute] string DataListKey) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - //Attempt to get the data list by key to see if key is valid - var DataList = DataListFactory.GetAyaDataList(DataListKey); - if (DataList == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - //Instantiate the business object handler - DataListTemplateBiz biz = DataListTemplateBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(DataListKey, false, DataList); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasDeleteRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!await biz.DeleteAsync(o)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - - return NoContent(); - } - - //------------ - - - }//eoc -}//eons \ No newline at end of file diff --git a/server/AyaNova/DataList/DataListFetcher.cs b/server/AyaNova/DataList/DataListFetcher.cs index 04375ca7..7732652d 100644 --- a/server/AyaNova/DataList/DataListFetcher.cs +++ b/server/AyaNova/DataList/DataListFetcher.cs @@ -40,14 +40,6 @@ namespace AyaNova.DataList //start with default string JSONDataListTemplate = DataList.DefaultDataListDisplayTemplate; - var CustomDataListTemplate = await ct.DataListTemplate.FirstOrDefaultAsync(x => x.DataListKey == listOptions.DataListKey); - //Null is expected unless user has created a custom one - if (CustomDataListTemplate != null) - { - //Make sure it's valid, if not then don't use it - if (DataList.ValidateTemplate(CustomDataListTemplate.Template)) - JSONDataListTemplate = CustomDataListTemplate.Template; - } diff --git a/server/AyaNova/biz/AyaType.cs b/server/AyaNova/biz/AyaType.cs index 3681e6b0..81d22781 100644 --- a/server/AyaNova/biz/AyaType.cs +++ b/server/AyaNova/biz/AyaType.cs @@ -32,9 +32,8 @@ namespace AyaNova.Biz DEPRECATED_REUSELATER_15 = 15, DEPRECATED_REUSELATER_16 = 16, FileAttachment = 17, - DataListSortFilter = 18, - FormCustom = 19, - DataListTemplate = 20 + DataListSortFilter = 18, + FormCustom = 19 //NOTE: New objects added here need to also be added to the following classes: @@ -43,7 +42,7 @@ namespace AyaNova.Biz //AyaNova.Biz.BizRoles //AyaNova.Biz.BizObjectNameFetcherDIRECT - //and in the CLIENT in ayatype.js + //and in the CLIENT in ayatype.js } diff --git a/server/AyaNova/biz/BizObjectExistsInDatabase.cs b/server/AyaNova/biz/BizObjectExistsInDatabase.cs index f8d71cbc..ea1d1c1c 100644 --- a/server/AyaNova/biz/BizObjectExistsInDatabase.cs +++ b/server/AyaNova/biz/BizObjectExistsInDatabase.cs @@ -38,8 +38,7 @@ namespace AyaNova.Biz return await ct.FileAttachment.AnyAsync(m => m.Id == id); case AyaType.DataListSortFilter: return await ct.DataListSortFilter.AnyAsync(m => m.Id == id); - case AyaType.DataListTemplate: - return await ct.DataListTemplate.AnyAsync(m => m.Id == id); + case AyaType.FormCustom: return await ct.FormCustom.AnyAsync(m => m.Id == id); diff --git a/server/AyaNova/biz/BizObjectFactory.cs b/server/AyaNova/biz/BizObjectFactory.cs index 40b13d15..cd44307e 100644 --- a/server/AyaNova/biz/BizObjectFactory.cs +++ b/server/AyaNova/biz/BizObjectFactory.cs @@ -37,8 +37,7 @@ namespace AyaNova.Biz return new LocaleBiz(dbcontext, userId, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, roles); case AyaType.DataListSortFilter: return new DataListSortFilterBiz(dbcontext, userId, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, roles); - case AyaType.DataListTemplate: - return new DataListTemplateBiz(dbcontext, userId, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, roles); + case AyaType.FormCustom: return new FormCustomBiz(dbcontext, userId, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, roles); diff --git a/server/AyaNova/biz/BizObjectNameFetcherDirect.cs b/server/AyaNova/biz/BizObjectNameFetcherDirect.cs index 7b2c6827..7076c911 100644 --- a/server/AyaNova/biz/BizObjectNameFetcherDirect.cs +++ b/server/AyaNova/biz/BizObjectNameFetcherDirect.cs @@ -37,10 +37,7 @@ namespace AyaNova.Biz TABLE = "aformcustom"; COLUMN = "formkey"; break; - case AyaType.DataListTemplate: - TABLE = "adatalisttemplate"; - COLUMN = "datalistkey"; - break; + default: throw new System.NotSupportedException($"AyaNova.BLL.BizObjectNameFetcher::Name type {aytype.ToString()} is not supported"); } diff --git a/server/AyaNova/biz/BizRoles.cs b/server/AyaNova/biz/BizRoles.cs index 897fc49c..a7a8060c 100644 --- a/server/AyaNova/biz/BizRoles.cs +++ b/server/AyaNova/biz/BizRoles.cs @@ -139,14 +139,7 @@ namespace AyaNova.Biz ReadFullRecord = AuthorizationRoles.All }); - //////////////////////////////////////////////////////////// - //DATALISTTEMPLATE - // - roles.Add(AyaType.DataListTemplate, new BizRoleSet() - { - Change = AuthorizationRoles.BizAdminFull, - ReadFullRecord = AuthorizationRoles.All - }); + //////////////////////////////////////////////////////////// //FORMCUSTOM diff --git a/server/AyaNova/biz/DataListTemplateBiz.cs b/server/AyaNova/biz/DataListTemplateBiz.cs deleted file mode 100644 index 064fa71e..00000000 --- a/server/AyaNova/biz/DataListTemplateBiz.cs +++ /dev/null @@ -1,124 +0,0 @@ -using System.Linq; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Newtonsoft.Json.Linq; -using AyaNova.Util; -using AyaNova.Api.ControllerHelpers; -using AyaNova.Models; -using AyaNova.DataList; - - -namespace AyaNova.Biz -{ - - - internal class DataListTemplateBiz : BizObject - { - - internal DataListTemplateBiz(AyContext dbcontext, long currentUserId, long userLocaleId, AuthorizationRoles UserRoles) - { - ct = dbcontext; - UserId = currentUserId; - UserLocaleId = userLocaleId; - CurrentUserRoles = UserRoles; - BizType = AyaType.DataListSortFilter; - } - - internal static DataListTemplateBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext) - { - return new DataListTemplateBiz(ct, UserIdFromContext.Id(httpContext.Items), UserLocaleIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); - } - - // //Version for internal use - // internal static DataListTemplateBiz GetBizInternal(AyContext ct) - // { - // return new DataListTemplateBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, AuthorizationRoles.BizAdminFull); - // } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - /// GET - internal async Task GetAsync(string DataListKey, bool log, IAyaDataList dataList) - { - //DataListKey always exists, if not in db then in default form - var ret = await ct.DataListTemplate.SingleOrDefaultAsync(m => m.DataListKey == DataListKey); - //Not in db? then put it in the db with the default then return it - if (ret == null) - { - ret = new DataListTemplate(); - ret.DataListKey = dataList.ListKey; - ret.Template = dataList.DefaultDataListDisplayTemplate; - await ct.DataListTemplate.AddAsync(ret); - await ct.SaveChangesAsync(); - } - - if (log) - { - //Log - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, ret.Id, BizType, AyaEvent.Retrieved), ct); - } - return ret; - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //UPDATE - // - - //put - internal async Task PutAsync(DataListTemplate dbObj, DataListTemplate inObj, IAyaDataList dataList) - { - - //Replace the db object with the PUT object - CopyObject.Copy(inObj, dbObj, "Id"); - //Set "original" value of concurrency token to input token - //this will allow EF to check it out - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken; - - if (!dataList.ValidateTemplate(dbObj.Template)) - { - AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Template"); - } - if (HasErrors) - return false; - - await ct.SaveChangesAsync(); - - //Log modification and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - - return true; - } - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DELETE - // - internal async Task DeleteAsync(DataListTemplate dbObj) - { - //no validation required, we have defaults so this is ok anytime - ct.DataListTemplate.Remove(dbObj); - await ct.SaveChangesAsync(); - - //Event log process delete - await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.DataListKey, ct); - return true; - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //VALIDATION - // nothing special required here, it's all tightly controlled and datalist has validation code built in - - - - - - ///////////////////////////////////////////////////////////////////// - - }//eoc - - -}//eons - diff --git a/server/AyaNova/models/AyContext.cs b/server/AyaNova/models/AyContext.cs index 8abfd5ec..96f08def 100644 --- a/server/AyaNova/models/AyContext.cs +++ b/server/AyaNova/models/AyContext.cs @@ -20,7 +20,7 @@ namespace AyaNova.Models public virtual DbSet DataListSortFilter { get; set; } public virtual DbSet Tag { get; set; } public virtual DbSet FormCustom { get; set; } - public virtual DbSet DataListTemplate { get; set; } + //Note: had to add this constructor to work with the code in startup.cs that gets the connection string from the appsettings.json file //and commented out the above on configuring diff --git a/server/AyaNova/models/DataListTemplate.cs b/server/AyaNova/models/DataListTemplate.cs deleted file mode 100644 index 29607c9d..00000000 --- a/server/AyaNova/models/DataListTemplate.cs +++ /dev/null @@ -1,16 +0,0 @@ - -using System.ComponentModel.DataAnnotations; - -namespace AyaNova.Models -{ - - public partial class DataListTemplate - { - public long Id { get; set; } - public uint ConcurrencyToken { get; set; } - [Required] - public string DataListKey { get; set; } - public string Template { get; set; }//JSON fragment template - - } -} diff --git a/server/AyaNova/util/AySchema.cs b/server/AyaNova/util/AySchema.cs index 001503c0..eb6fb3f1 100644 --- a/server/AyaNova/util/AySchema.cs +++ b/server/AyaNova/util/AySchema.cs @@ -268,8 +268,7 @@ namespace AyaNova.Util await ExecQueryAsync("CREATE TABLE adatalistsortfilter (id BIGSERIAL PRIMARY KEY, userId bigint not null, name varchar(255) not null, public bool not null," + "listkey varchar(255) not null, filter text, sort text, UNIQUE(name))"); - - await ExecQueryAsync("CREATE TABLE adatalisttemplate (id BIGSERIAL PRIMARY KEY, datalistkey text not null, template text, UNIQUE(datalistkey))"); + await SetSchemaLevelAsync(++currentSchema); }