diff --git a/server/AyaNova/Controllers/DataListSavedFilterController.cs b/server/AyaNova/Controllers/DataListSavedFilterController.cs index 67008813..cba00404 100644 --- a/server/AyaNova/Controllers/DataListSavedFilterController.cs +++ b/server/AyaNova/Controllers/DataListSavedFilterController.cs @@ -234,31 +234,30 @@ namespace AyaNova.Api.Controllers } /// - /// Get default ListView for DataList + /// Get default Columns and Sort for DataList /// /// Key of an existing DataList - /// A single DataListSavedFilter + /// Columns and Sort [HttpGet("default/{dataListKey}")] public ActionResult GetDefaultDataListSavedFilter([FromRoute] string dataListKey) { - throw new System.NotSupportedException("NEEDS REPLACEMENT WITH NEW SYSTEM"); - // if (!serverState.IsOpen) - // return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + if (!serverState.IsOpen) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - // //Instantiate the business object handler - // DataListSavedFilterBiz biz = DataListSavedFilterBiz.GetBiz(ct, HttpContext); + //Instantiate the business object handler + DataListSavedFilterBiz biz = DataListSavedFilterBiz.GetBiz(ct, HttpContext); - // if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) - // return StatusCode(403, new ApiNotAuthorizedResponse()); + if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) + return StatusCode(403, new ApiNotAuthorizedResponse()); - // if (!ModelState.IsValid) - // return BadRequest(new ApiErrorResponse(ModelState)); + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); - // var o = AyaNova.DataList.DataListFactory.GetAyaDataList(dataListKey); ; - // if (o == null) - // return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); + var o = AyaNova.DataList.DataListFactory.GetAyaDataList(dataListKey); ; + if (o == null) + return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - // return Ok(ApiOkResponse.Response(o.DefaultListView)); + return Ok(ApiOkResponse.Response(new { Columns = o.DefaultColumns, SortBy = o.DefaultSortBy })); } diff --git a/server/AyaNova/biz/DataListViewBiz.cs b/server/AyaNova/biz/DataListViewBiz.cs index 741a7a8f..be9f7dfc 100644 --- a/server/AyaNova/biz/DataListViewBiz.cs +++ b/server/AyaNova/biz/DataListViewBiz.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using System.Collections.Generic; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Linq; +using Newtonsoft.Json; using AyaNova.Util; using AyaNova.Api.ControllerHelpers; using AyaNova.Models; @@ -233,7 +234,7 @@ namespace AyaNova.Biz if (string.IsNullOrWhiteSpace(inObj.Name)) AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name"); - + //If name is otherwise OK, check that name is unique if (!PropertyHasErrors("Name")) { @@ -244,6 +245,9 @@ namespace AyaNova.Biz } } + if (string.IsNullOrWhiteSpace(inObj.Filter)) + AddError(ApiErrorCode.VALIDATION_REQUIRED, "Filter"); + if (string.IsNullOrWhiteSpace(inObj.ListKey)) AddError(ApiErrorCode.VALIDATION_REQUIRED, "ListKey"); @@ -255,76 +259,63 @@ namespace AyaNova.Biz AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "ListKey", $"ListKey \"{inObj.ListKey}\" DataListKey is not valid"); } - - if (inObj.ListKey.Length > 255) AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "ListKey", "255 max"); - //Filter json must parse - //this is all automated normally so not going to do too much parsing here - //just ensure it's basically there - if (!string.IsNullOrWhiteSpace(inObj.ListView)) + //check if filter can be reconstructed into a C# filter object + try { - try - { - var v = JArray.Parse(inObj.ListView); - for (int i = 0; i < v.Count; i++) - { - var filterItem = v[i]; - if (filterItem["fld"] == null) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "ListView", $"ListView array item {i}, object is missing required \"fld\" property "); - else - { - var fld = filterItem["fld"].Value(); - if (string.IsNullOrWhiteSpace(fld)) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "ListView", $"ListView array item {i}, \"fld\" property is empty and required"); - - //validate the field name if we can - if (DataList != null) - { - - var TheField = DataList.FieldDefinitions.SingleOrDefault(z => z.FieldKey.ToLowerInvariant() == fld.ToLowerInvariant()); - - if (TheField == null) - { - AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "ListView", $"ListView array item {i}, fld property value \"{fld}\" is not a valid value for ListKey specified"); - } - - } - } - //This is the old filter validation code but at this point only going to validate that the fields are present and valid as the bare minimum - // if (filterItem["op"] == null) - // AddError(ApiErrorCode.VALIDATION_REQUIRED, "Filter", $"Filter array item {i}, object is missing required \"op\" property "); - // else - // { - // var opType = filterItem["op"].Value(); - // if (!DataListFilterComparisonOperator.Operators.Contains(opType)) - // AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Filter", $"Filter array item {i}, \"op\" property value of \"{opType}\" is not a valid FilterComparisonOperator type"); - // } - - // if (filterItem["value"] == null) - // AddError(ApiErrorCode.VALIDATION_REQUIRED, "Filter", $"Filter array item {i}, object is missing or is empty the required \"value\" property "); - // else - // { - // //check if the value is present, not what it is exactly, just that it's present - // //value also could contain relative date tokens, not that it checks them anyway but just noting it here - // if (filterItem["value"].Type == JTokenType.String && string.IsNullOrWhiteSpace(filterItem["value"].Value())) - // AddError(ApiErrorCode.VALIDATION_REQUIRED, "Filter", $"Filter array item {i}, object is missing or is empty the required \"value\" property "); - - // if (filterItem["value"].Type == JTokenType.Array && filterItem["value"].Count() == 0) - // AddError(ApiErrorCode.VALIDATION_REQUIRED, "Filter", $"Filter array item {i}, object is missing or is empty the required \"value\" property ARRAY "); - // } - - - //NOTE: value of nothing, null or empty is a valid value so no checking for it here - } - } - catch (Newtonsoft.Json.JsonReaderException ex) - { - AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "ListView", "ListView is not valid JSON string: " + ex.Message); - - } + List dataListFilterOptions = JsonConvert.DeserializeObject>(inObj.Filter); } + catch (System.Exception ex) + { + AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Filter", "Filter is not valid JSON string, can't convert to List, error: " + ex.Message); + } + + + // //Filter json must parse + // //this is all automated normally so not going to do too much parsing here + // //just ensure it's basically there + // if (!string.IsNullOrWhiteSpace(inObj.ListView)) + // { + // try + // { + // var v = JArray.Parse(inObj.ListView); + // for (int i = 0; i < v.Count; i++) + // { + // var filterItem = v[i]; + // if (filterItem["fld"] == null) + // AddError(ApiErrorCode.VALIDATION_REQUIRED, "ListView", $"ListView array item {i}, object is missing required \"fld\" property "); + // else + // { + // var fld = filterItem["fld"].Value(); + // if (string.IsNullOrWhiteSpace(fld)) + // AddError(ApiErrorCode.VALIDATION_REQUIRED, "ListView", $"ListView array item {i}, \"fld\" property is empty and required"); + + // //validate the field name if we can + // if (DataList != null) + // { + + // var TheField = DataList.FieldDefinitions.SingleOrDefault(z => z.FieldKey.ToLowerInvariant() == fld.ToLowerInvariant()); + + // if (TheField == null) + // { + // AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "ListView", $"ListView array item {i}, fld property value \"{fld}\" is not a valid value for ListKey specified"); + // } + + // } + // } + + + // //NOTE: value of nothing, null or empty is a valid value so no checking for it here + // } + // } + // catch (Newtonsoft.Json.JsonReaderException ex) + // { + // AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "ListView", "ListView is not valid JSON string: " + ex.Message); + + // } + // }