This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
using System.Linq;
|
||||
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;
|
||||
@@ -44,180 +42,109 @@ namespace AyaNova.Biz
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//CREATE
|
||||
//
|
||||
internal async Task<DataListSavedColumnView> CreateAsync(DataListSavedColumnView newObject)
|
||||
{
|
||||
ValidateAsync(newObject);
|
||||
if (HasErrors)
|
||||
return null;
|
||||
else
|
||||
{
|
||||
//delete the existing one in favor of this one
|
||||
if (!await DeleteAsync(newObject.UserId, newObject.ListKey))
|
||||
return null;
|
||||
await ct.DataListSavedColumnView.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// //CREATE
|
||||
// internal async Task<DataListSavedColumnView> CreateAsync(DataListSavedColumnView inObj)
|
||||
// {
|
||||
// await ValidateAsync(inObj, true);
|
||||
// if (HasErrors)
|
||||
// return null;
|
||||
// else
|
||||
// {
|
||||
// //do stuff with datafilter
|
||||
// DataListSavedColumnView outObj = inObj;
|
||||
// outObj.UserId = UserId;
|
||||
|
||||
|
||||
// await ct.DataListSavedColumnView.AddAsync(outObj);
|
||||
// await ct.SaveChangesAsync();
|
||||
|
||||
// //Handle child and associated items:
|
||||
|
||||
// //EVENT LOG
|
||||
// await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
||||
|
||||
|
||||
|
||||
// return outObj;
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// /// GET
|
||||
|
||||
// //Get one
|
||||
// internal async Task<DataListSavedColumnView> GetAsync(long fetchId, bool logTheGetEvent = true)
|
||||
// {
|
||||
// //This is simple so nothing more here, but often will be copying to a different output object or some other ops
|
||||
// var ret = await ct.DataListSavedColumnView.SingleOrDefaultAsync(z => z.Id == fetchId && (z.Public == true || z.UserId == UserId));
|
||||
// if (logTheGetEvent && ret != null)
|
||||
// {
|
||||
// //Log
|
||||
// await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
||||
// }
|
||||
// return ret;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
// //get ViewList (NOT PAGED)
|
||||
// internal async Task<List<NameIdItem>> GetViewListAsync(string listKey)
|
||||
// {
|
||||
// List<NameIdItem> items = new List<NameIdItem>();
|
||||
// if (!string.IsNullOrWhiteSpace(listKey))
|
||||
// {
|
||||
// items = await ct.DataListSavedColumnView
|
||||
// .AsNoTracking()
|
||||
// .Where(z => z.ListKey == listKey && (z.Public == true || z.UserId == UserId))
|
||||
// .OrderBy(z => z.Name)
|
||||
// .Select(z => new NameIdItem()
|
||||
// {
|
||||
// Id = z.Id,
|
||||
// Name = z.Name
|
||||
// }).ToListAsync();
|
||||
|
||||
// }
|
||||
// return items;
|
||||
// }
|
||||
return newObject;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//UPDATE
|
||||
//GET
|
||||
//
|
||||
|
||||
//put
|
||||
internal async Task<bool> PutAsync(DataListSavedColumnView inObj)
|
||||
//Internal, used by datalistfetcher via datalisttableprocessingoptions constructor
|
||||
//can be called without full biz instantiation as it doesn't rely on UserId in biz object or other shit
|
||||
//maybe should be static?
|
||||
internal async Task<DataListSavedColumnView> GetAsync(long userId, string listKey, bool createDefaultIfNecessary)
|
||||
{
|
||||
//preserve the owner ID if none was specified
|
||||
if (inObj.UserId == 0)
|
||||
inObj.UserId = dbObject.UserId;
|
||||
|
||||
//Replace the db object with the PUT object
|
||||
CopyObject.Copy(inObj, dbObject, "Id");
|
||||
//Set "original" value of concurrency token to input token
|
||||
//this will allow EF to check it out
|
||||
ct.Entry(dbObject).OriginalValues["Concurrency"] = inObj.Concurrency;
|
||||
|
||||
await ValidateAsync(dbObject);
|
||||
if (HasErrors)
|
||||
return false;
|
||||
await ct.SaveChangesAsync();
|
||||
|
||||
//Log modification and save context
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
var ret = await ct.DataListSavedColumnView.AsNoTracking().SingleOrDefaultAsync(z => z.UserId == userId && z.ListKey == listKey);
|
||||
if(ret==null && createDefaultIfNecessary){
|
||||
if(!DataListFactory.ListKeyIsValid(listKey)){
|
||||
throw new System.ArgumentOutOfRangeException($"ListKey '{listKey}' is not a valid DataListKey");
|
||||
}
|
||||
ret=new DataListSavedColumnView();
|
||||
ret.UserId=UserId;
|
||||
ret.ListKey=listKey;
|
||||
var dataList=DataListFactory.GetAyaDataList(listKey);
|
||||
ret.Columns=JsonConvert.SerializeObject(dataList.DefaultColumns);
|
||||
ret.Sort=JsonConvert.SerializeObject(dataList.DefaultSortBy);
|
||||
return await CreateAsync(ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// //DELETE
|
||||
// //
|
||||
// internal async Task<bool> DeleteAsync(DataListSavedColumnView dbObject)
|
||||
// {
|
||||
// //Determine if the object can be deleted, do the deletion tentatively
|
||||
// //Probably also in here deal with tags and associated search text etc
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//DELETE
|
||||
//
|
||||
internal async Task<bool> DeleteAsync(long userId, string listKey)
|
||||
{
|
||||
//this is effectively the RESET route handler
|
||||
//so it can be called any time even if there is no default list and it's a-ok
|
||||
//because a new default will be created if needed
|
||||
var dbObject = await GetAsync(userId, listKey, false);
|
||||
if (dbObject != null)
|
||||
{
|
||||
ct.DataListSavedColumnView.Remove(dbObject);
|
||||
await ct.SaveChangesAsync();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// //FUTURE POSSIBLE NEED
|
||||
// //ValidateCanDelete(dbObject);
|
||||
|
||||
// if (HasErrors)
|
||||
// return false;
|
||||
// ct.DataListSavedColumnView.Remove(dbObject);
|
||||
// await ct.SaveChangesAsync();
|
||||
|
||||
// //Delete sibling objects
|
||||
|
||||
// //Event log process delete
|
||||
// await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||
|
||||
|
||||
// //Delete search index
|
||||
// //Search.ProcessDeletedObjectKeywords(dbObject.Id, BizType);
|
||||
|
||||
|
||||
// return true;
|
||||
// }
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//VALIDATION
|
||||
//
|
||||
|
||||
//Can save or update?
|
||||
private async Task ValidateAsync(DataListSavedColumnView inObj)
|
||||
private void ValidateAsync(DataListSavedColumnView inObj)
|
||||
{
|
||||
|
||||
|
||||
if (inObj.UserId != UserId)
|
||||
AddError(ApiErrorCode.NOT_AUTHORIZED, "UserId", "Only own view can be modified");
|
||||
|
||||
|
||||
if (string.IsNullOrWhiteSpace(inObj.ListKey))
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "ListKey");
|
||||
|
||||
|
||||
//TODO TODO TODO
|
||||
//THIS SHOULD USE NAME LIST INSTEAD OR BE DONE AT OBJECT ABOVE FOR DEFAULTS, SAME FOR FILTER BIZ THIS IS BASED OFF OF NO NEED TO INSTANTIATE
|
||||
var DataList = DataListFactory.GetAyaDataList(inObj.ListKey);
|
||||
|
||||
if (DataList == null)
|
||||
{
|
||||
if (!DataListFactory.ListKeyIsValid(inObj.ListKey))
|
||||
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");
|
||||
|
||||
//check if filter can be reconstructed into a C# filter object
|
||||
|
||||
|
||||
//Validate Sort JSON
|
||||
try
|
||||
{
|
||||
List<DataListFilterOption> dataListFilterOptions = JsonConvert.DeserializeObject<List<DataListFilterOption>>(inObj.Filter);
|
||||
JsonConvert.DeserializeObject<Dictionary<string, string>>(inObj.Sort);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Filter", "Filter is not valid JSON string, can't convert to List<DataListFilterOption>, error: " + ex.Message);
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Sort", "Sort is not valid JSON string, can't convert to valid Dictionary<string, string> SortBy, error: " + ex.Message);
|
||||
}
|
||||
|
||||
//Validate Columns JSON
|
||||
try
|
||||
{
|
||||
|
||||
JsonConvert.DeserializeObject<List<string>>(inObj.Columns);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Sort", "Sort is not valid JSON string, can't convert to valid Dictionary<string, string> SortBy, error: " + ex.Message);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user