162 lines
6.0 KiB
C#
162 lines
6.0 KiB
C#
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using AyaNova.Util;
|
|
using AyaNova.Api.ControllerHelpers;
|
|
using AyaNova.Models;
|
|
using AyaNova.DataList;
|
|
|
|
|
|
namespace AyaNova.Biz
|
|
{
|
|
|
|
|
|
internal class DataListSavedColumnViewBiz : BizObject
|
|
{
|
|
|
|
internal DataListSavedColumnViewBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
|
{
|
|
ct = dbcontext;
|
|
UserId = currentUserId;
|
|
UserTranslationId = userTranslationId;
|
|
CurrentUserRoles = UserRoles;
|
|
BizType = AyaType.DataListSavedColumnView;
|
|
}
|
|
|
|
internal static DataListSavedColumnViewBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
|
{
|
|
if (httpContext != null)
|
|
return new DataListSavedColumnViewBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
|
else
|
|
return new DataListSavedColumnViewBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//EXISTS
|
|
internal async Task<bool> ExistsAsync(long id)
|
|
{
|
|
return await ct.DataListSavedColumnView.AnyAsync(z => z.Id == id);
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//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();
|
|
|
|
return newObject;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//GET
|
|
//
|
|
//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)
|
|
{
|
|
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(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;
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//VALIDATION
|
|
//
|
|
|
|
//Can save or update?
|
|
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");
|
|
|
|
if (!DataListFactory.ListKeyIsValid(inObj.ListKey))
|
|
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "ListKey", $"ListKey \"{inObj.ListKey}\" DataListKey is not valid");
|
|
|
|
|
|
//Validate Sort JSON
|
|
try
|
|
{
|
|
JsonConvert.DeserializeObject<Dictionary<string, string>>(inObj.Sort);
|
|
}
|
|
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);
|
|
}
|
|
|
|
//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;
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
}//eoc
|
|
|
|
|
|
}//eons
|
|
|