This commit is contained in:
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class ContractBiz : BizObject, ISearchAbleObject
|
internal class ContractBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal ContractBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal ContractBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static ContractBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static ContractBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new ContractBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new ContractBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new ContractBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new ContractBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,12 +32,9 @@ namespace AyaNova.Biz
|
|||||||
return await ct.Contract.AnyAsync(e => e.Id == id);
|
return await ct.Contract.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
|
||||||
internal async Task<Contract> CreateAsync(Contract newObject)
|
internal async Task<Contract> CreateAsync(Contract newObject)
|
||||||
{
|
{
|
||||||
await ValidateAsync(newObject, null);
|
await ValidateAsync(newObject, null);
|
||||||
@@ -51,122 +42,138 @@ namespace AyaNova.Biz
|
|||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with Contract
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
Contract outObj = newObject;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.Contract.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.Contract.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Contract> DuplicateAsync(long id)
|
||||||
internal async Task<Contract> DuplicateAsync(Contract dbObj)
|
|
||||||
{
|
{
|
||||||
|
Contract dbObject = await GetAsync(id, false);
|
||||||
Contract outObj = new Contract();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
Contract newObject = new Contract();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.Contract.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.Contract.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.Contract.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.Contract.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
//Handle child and associated items:
|
await SearchIndexAsync(newObject, true);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await SearchIndexAsync(outObj, true);
|
return newObject;
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
|
||||||
return outObj;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
/// GET
|
//GET
|
||||||
///
|
//
|
||||||
///
|
internal async Task<Contract> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
|
|
||||||
internal async Task<Contract> 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.Contract.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
var ret = await ct.Contract.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
if (logTheGetEvent && ret != null)
|
||||||
{
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Contract> PutAsync(Contract putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(Contract dbObj, Contract inObj)
|
|
||||||
{
|
{
|
||||||
|
Contract dbObject = await ct.Contract.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Contract SnapshotOfOriginalDBObj = new Contract();
|
Contract SnapshotOfOriginalDBObj = new Contract();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Contract dbObject = await ct.Contract.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.Contract.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(Contract obj, bool isNew)
|
private async Task SearchIndexAsync(Contract obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -182,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(Contract dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.Contract.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(Contract proposedObj, Contract currentObj)
|
private async Task ValidateAsync(Contract proposedObj, Contract currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -250,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(Contract inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(Contract inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class HeadOfficeBiz : BizObject, ISearchAbleObject
|
internal class HeadOfficeBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal HeadOfficeBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal HeadOfficeBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static HeadOfficeBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static HeadOfficeBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new HeadOfficeBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new HeadOfficeBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new HeadOfficeBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new HeadOfficeBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,133 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.HeadOffice.AnyAsync(e => e.Id == id);
|
return await ct.HeadOffice.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<HeadOffice> 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.HeadOffice.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<HeadOffice> CreateAsync(HeadOffice newObject)
|
||||||
internal async Task<HeadOffice> CreateAsync(HeadOffice inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with HeadOffice
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
HeadOffice outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.HeadOffice.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.HeadOffice.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<HeadOffice> DuplicateAsync(long id)
|
||||||
internal async Task<HeadOffice> DuplicateAsync(HeadOffice dbObj)
|
|
||||||
{
|
{
|
||||||
|
HeadOffice dbObject = await GetAsync(id, false);
|
||||||
HeadOffice outObj = new HeadOffice();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
HeadOffice newObject = new HeadOffice();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.HeadOffice.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.HeadOffice.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.HeadOffice.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.HeadOffice.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<HeadOffice> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.HeadOffice.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<HeadOffice> PutAsync(HeadOffice putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(HeadOffice dbObj, HeadOffice inObj)
|
|
||||||
{
|
{
|
||||||
|
HeadOffice dbObject = await ct.HeadOffice.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
HeadOffice SnapshotOfOriginalDBObj = new HeadOffice();
|
HeadOffice SnapshotOfOriginalDBObj = new HeadOffice();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
HeadOffice dbObject = await ct.HeadOffice.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.HeadOffice.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(HeadOffice obj, bool isNew)
|
private async Task SearchIndexAsync(HeadOffice obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -180,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(HeadOffice dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.HeadOffice.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(HeadOffice proposedObj, HeadOffice currentObj)
|
private async Task ValidateAsync(HeadOffice proposedObj, HeadOffice currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -248,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(HeadOffice inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(HeadOffice inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class LoanUnitBiz : BizObject, ISearchAbleObject
|
internal class LoanUnitBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal LoanUnitBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal LoanUnitBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static LoanUnitBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static LoanUnitBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new LoanUnitBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new LoanUnitBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new LoanUnitBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new LoanUnitBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,133 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.LoanUnit.AnyAsync(e => e.Id == id);
|
return await ct.LoanUnit.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<LoanUnit> 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.LoanUnit.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<LoanUnit> CreateAsync(LoanUnit newObject)
|
||||||
internal async Task<LoanUnit> CreateAsync(LoanUnit inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with LoanUnit
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
LoanUnit outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.LoanUnit.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.LoanUnit.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<LoanUnit> DuplicateAsync(long id)
|
||||||
internal async Task<LoanUnit> DuplicateAsync(LoanUnit dbObj)
|
|
||||||
{
|
{
|
||||||
|
LoanUnit dbObject = await GetAsync(id, false);
|
||||||
LoanUnit outObj = new LoanUnit();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
LoanUnit newObject = new LoanUnit();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.LoanUnit.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.LoanUnit.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.LoanUnit.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.LoanUnit.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<LoanUnit> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.LoanUnit.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<LoanUnit> PutAsync(LoanUnit putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(LoanUnit dbObj, LoanUnit inObj)
|
|
||||||
{
|
{
|
||||||
|
LoanUnit dbObject = await ct.LoanUnit.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
LoanUnit SnapshotOfOriginalDBObj = new LoanUnit();
|
LoanUnit SnapshotOfOriginalDBObj = new LoanUnit();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
LoanUnit dbObject = await ct.LoanUnit.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.LoanUnit.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(LoanUnit obj, bool isNew)
|
private async Task SearchIndexAsync(LoanUnit obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -180,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(LoanUnit dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.LoanUnit.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(LoanUnit proposedObj, LoanUnit currentObj)
|
private async Task ValidateAsync(LoanUnit proposedObj, LoanUnit currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -248,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(LoanUnit inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(LoanUnit inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class PMTemplateBiz : BizObject, ISearchAbleObject
|
internal class PMTemplateBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal PMTemplateBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal PMTemplateBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static PMTemplateBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static PMTemplateBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new PMTemplateBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new PMTemplateBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new PMTemplateBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new PMTemplateBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,133 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.PMTemplate.AnyAsync(e => e.Id == id);
|
return await ct.PMTemplate.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<PMTemplate> 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.PMTemplate.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<PMTemplate> CreateAsync(PMTemplate newObject)
|
||||||
internal async Task<PMTemplate> CreateAsync(PMTemplate inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with PMTemplate
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
PMTemplate outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.PMTemplate.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.PMTemplate.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<PMTemplate> DuplicateAsync(long id)
|
||||||
internal async Task<PMTemplate> DuplicateAsync(PMTemplate dbObj)
|
|
||||||
{
|
{
|
||||||
|
PMTemplate dbObject = await GetAsync(id, false);
|
||||||
PMTemplate outObj = new PMTemplate();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
PMTemplate newObject = new PMTemplate();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.PMTemplate.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.PMTemplate.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.PMTemplate.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.PMTemplate.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<PMTemplate> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.PMTemplate.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<PMTemplate> PutAsync(PMTemplate putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(PMTemplate dbObj, PMTemplate inObj)
|
|
||||||
{
|
{
|
||||||
|
PMTemplate dbObject = await ct.PMTemplate.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
PMTemplate SnapshotOfOriginalDBObj = new PMTemplate();
|
PMTemplate SnapshotOfOriginalDBObj = new PMTemplate();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
PMTemplate dbObject = await ct.PMTemplate.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.PMTemplate.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(PMTemplate obj, bool isNew)
|
private async Task SearchIndexAsync(PMTemplate obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -180,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(PMTemplate dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.PMTemplate.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(PMTemplate proposedObj, PMTemplate currentObj)
|
private async Task ValidateAsync(PMTemplate proposedObj, PMTemplate currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -248,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(PMTemplate inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(PMTemplate inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class PartBiz : BizObject, ISearchAbleObject
|
internal class PartBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal PartBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal PartBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static PartBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static PartBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new PartBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new PartBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new PartBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new PartBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,134 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.Part.AnyAsync(e => e.Id == id);
|
return await ct.Part.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<Part> 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.Part.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<Part> CreateAsync(Part newObject)
|
||||||
internal async Task<Part> CreateAsync(Part inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with Part
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
Part outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.Part.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.Part.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Part> DuplicateAsync(long id)
|
||||||
internal async Task<Part> DuplicateAsync(Part dbObj)
|
|
||||||
{
|
{
|
||||||
|
Part dbObject = await GetAsync(id, false);
|
||||||
Part outObj = new Part();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
Part newObject = new Part();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.Part.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.Part.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.Part.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.Part.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<Part> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.Part.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Part> PutAsync(Part putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(Part dbObj, Part inObj)
|
|
||||||
{
|
{
|
||||||
|
Part dbObject = await ct.Part.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Part SnapshotOfOriginalDBObj = new Part();
|
Part SnapshotOfOriginalDBObj = new Part();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Part dbObject = await ct.Part.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.Part.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(Part obj, bool isNew)
|
private async Task SearchIndexAsync(Part obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -181,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(Part dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.Part.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(Part proposedObj, Part currentObj)
|
private async Task ValidateAsync(Part proposedObj, Part currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -249,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(Part inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(Part inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class ProjectBiz : BizObject, ISearchAbleObject
|
internal class ProjectBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal ProjectBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal ProjectBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static ProjectBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static ProjectBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new ProjectBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new ProjectBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new ProjectBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new ProjectBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,133 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.Project.AnyAsync(e => e.Id == id);
|
return await ct.Project.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<Project> 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.Project.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<Project> CreateAsync(Project newObject)
|
||||||
internal async Task<Project> CreateAsync(Project inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with Project
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
Project outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.Project.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.Project.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Project> DuplicateAsync(long id)
|
||||||
internal async Task<Project> DuplicateAsync(Project dbObj)
|
|
||||||
{
|
{
|
||||||
|
Project dbObject = await GetAsync(id, false);
|
||||||
Project outObj = new Project();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
Project newObject = new Project();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.Project.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.Project.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.Project.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.Project.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<Project> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.Project.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Project> PutAsync(Project putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(Project dbObj, Project inObj)
|
|
||||||
{
|
{
|
||||||
|
Project dbObject = await ct.Project.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Project SnapshotOfOriginalDBObj = new Project();
|
Project SnapshotOfOriginalDBObj = new Project();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Project dbObject = await ct.Project.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.Project.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(Project obj, bool isNew)
|
private async Task SearchIndexAsync(Project obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -180,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(Project dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.Project.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(Project proposedObj, Project currentObj)
|
private async Task ValidateAsync(Project proposedObj, Project currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -248,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(Project inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(Project inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class PurchaseOrderBiz : BizObject, ISearchAbleObject
|
internal class PurchaseOrderBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal PurchaseOrderBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal PurchaseOrderBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static PurchaseOrderBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static PurchaseOrderBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new PurchaseOrderBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new PurchaseOrderBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new PurchaseOrderBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new PurchaseOrderBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,133 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.PurchaseOrder.AnyAsync(e => e.Id == id);
|
return await ct.PurchaseOrder.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<PurchaseOrder> 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.PurchaseOrder.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<PurchaseOrder> CreateAsync(PurchaseOrder newObject)
|
||||||
internal async Task<PurchaseOrder> CreateAsync(PurchaseOrder inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with PurchaseOrder
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
PurchaseOrder outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.PurchaseOrder.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.PurchaseOrder.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<PurchaseOrder> DuplicateAsync(long id)
|
||||||
internal async Task<PurchaseOrder> DuplicateAsync(PurchaseOrder dbObj)
|
|
||||||
{
|
{
|
||||||
|
PurchaseOrder dbObject = await GetAsync(id, false);
|
||||||
PurchaseOrder outObj = new PurchaseOrder();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
PurchaseOrder newObject = new PurchaseOrder();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.PurchaseOrder.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.PurchaseOrder.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.PurchaseOrder.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.PurchaseOrder.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<PurchaseOrder> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.PurchaseOrder.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<PurchaseOrder> PutAsync(PurchaseOrder putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(PurchaseOrder dbObj, PurchaseOrder inObj)
|
|
||||||
{
|
{
|
||||||
|
PurchaseOrder dbObject = await ct.PurchaseOrder.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
PurchaseOrder SnapshotOfOriginalDBObj = new PurchaseOrder();
|
PurchaseOrder SnapshotOfOriginalDBObj = new PurchaseOrder();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
PurchaseOrder dbObject = await ct.PurchaseOrder.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.PurchaseOrder.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(PurchaseOrder obj, bool isNew)
|
private async Task SearchIndexAsync(PurchaseOrder obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -180,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(PurchaseOrder dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.PurchaseOrder.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(PurchaseOrder proposedObj, PurchaseOrder currentObj)
|
private async Task ValidateAsync(PurchaseOrder proposedObj, PurchaseOrder currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -248,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(PurchaseOrder inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(PurchaseOrder inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class UnitBiz : BizObject, ISearchAbleObject
|
internal class UnitBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal UnitBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal UnitBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static UnitBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static UnitBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new UnitBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new UnitBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new UnitBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new UnitBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,134 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.Unit.AnyAsync(e => e.Id == id);
|
return await ct.Unit.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<Unit> 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.Unit.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<Unit> CreateAsync(Unit newObject)
|
||||||
internal async Task<Unit> CreateAsync(Unit inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with Unit
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
Unit outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.Unit.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.Unit.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Unit> DuplicateAsync(long id)
|
||||||
internal async Task<Unit> DuplicateAsync(Unit dbObj)
|
|
||||||
{
|
{
|
||||||
|
Unit dbObject = await GetAsync(id, false);
|
||||||
Unit outObj = new Unit();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
Unit newObject = new Unit();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.Unit.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.Unit.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.Unit.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.Unit.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<Unit> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.Unit.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Unit> PutAsync(Unit putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(Unit dbObj, Unit inObj)
|
|
||||||
{
|
{
|
||||||
|
Unit dbObject = await ct.Unit.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Unit SnapshotOfOriginalDBObj = new Unit();
|
Unit SnapshotOfOriginalDBObj = new Unit();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Unit dbObject = await ct.Unit.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.Unit.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(Unit obj, bool isNew)
|
private async Task SearchIndexAsync(Unit obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -181,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(Unit dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.Unit.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(Unit proposedObj, Unit currentObj)
|
private async Task ValidateAsync(Unit proposedObj, Unit currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -249,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(Unit inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(Unit inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class UnitModelBiz : BizObject, ISearchAbleObject
|
internal class UnitModelBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal UnitModelBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal UnitModelBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static UnitModelBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static UnitModelBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new UnitModelBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new UnitModelBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new UnitModelBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new UnitModelBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,133 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.UnitModel.AnyAsync(e => e.Id == id);
|
return await ct.UnitModel.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<UnitModel> 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.UnitModel.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<UnitModel> CreateAsync(UnitModel newObject)
|
||||||
internal async Task<UnitModel> CreateAsync(UnitModel inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with UnitModel
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
UnitModel outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.UnitModel.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.UnitModel.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<UnitModel> DuplicateAsync(long id)
|
||||||
internal async Task<UnitModel> DuplicateAsync(UnitModel dbObj)
|
|
||||||
{
|
{
|
||||||
|
UnitModel dbObject = await GetAsync(id, false);
|
||||||
UnitModel outObj = new UnitModel();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
UnitModel newObject = new UnitModel();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.UnitModel.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.UnitModel.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.UnitModel.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.UnitModel.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<UnitModel> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.UnitModel.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<UnitModel> PutAsync(UnitModel putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(UnitModel dbObj, UnitModel inObj)
|
|
||||||
{
|
{
|
||||||
|
UnitModel dbObject = await ct.UnitModel.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
UnitModel SnapshotOfOriginalDBObj = new UnitModel();
|
UnitModel SnapshotOfOriginalDBObj = new UnitModel();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
UnitModel dbObject = await ct.UnitModel.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.UnitModel.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(UnitModel obj, bool isNew)
|
private async Task SearchIndexAsync(UnitModel obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -180,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(UnitModel dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.UnitModel.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(UnitModel proposedObj, UnitModel currentObj)
|
private async Task ValidateAsync(UnitModel proposedObj, UnitModel currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -248,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(UnitModel inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(UnitModel inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using AyaNova.Util;
|
using AyaNova.Util;
|
||||||
using AyaNova.Api.ControllerHelpers;
|
using AyaNova.Api.ControllerHelpers;
|
||||||
using AyaNova.Models;
|
using AyaNova.Models;
|
||||||
|
|
||||||
namespace AyaNova.Biz
|
namespace AyaNova.Biz
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class VendorBiz : BizObject, ISearchAbleObject
|
internal class VendorBiz : BizObject, ISearchAbleObject
|
||||||
{
|
{
|
||||||
|
|
||||||
internal VendorBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
internal VendorBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||||
{
|
{
|
||||||
ct = dbcontext;
|
ct = dbcontext;
|
||||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
internal static VendorBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
internal static VendorBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (httpContext != null)
|
if (httpContext != null)
|
||||||
return new VendorBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
return new VendorBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||||
else//when called internally for internal ops there will be no context so need to set default values for that
|
else
|
||||||
return new VendorBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
return new VendorBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//EXISTS
|
//EXISTS
|
||||||
internal async Task<bool> ExistsAsync(long id)
|
internal async Task<bool> ExistsAsync(long id)
|
||||||
@@ -38,133 +32,148 @@ namespace AyaNova.Biz
|
|||||||
return await ct.Vendor.AnyAsync(e => e.Id == id);
|
return await ct.Vendor.AnyAsync(e => e.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// GET
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
internal async Task<Vendor> 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.Vendor.SingleOrDefaultAsync(m => m.Id == fetchId);
|
|
||||||
if (logTheGetEvent && ret != null)
|
|
||||||
{
|
|
||||||
//Log
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//CREATE
|
//CREATE
|
||||||
|
//
|
||||||
//Called from route and also seeder
|
internal async Task<Vendor> CreateAsync(Vendor newObject)
|
||||||
internal async Task<Vendor> CreateAsync(Vendor inObj)
|
|
||||||
{
|
{
|
||||||
await ValidateAsync(inObj, null);
|
await ValidateAsync(newObject, null);
|
||||||
if (HasErrors)
|
if (HasErrors)
|
||||||
return null;
|
return null;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//do stuff with Vendor
|
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||||
Vendor outObj = inObj;
|
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||||
|
await ct.Vendor.AddAsync(newObject);
|
||||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
|
||||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
|
||||||
//Save to db
|
|
||||||
await ct.Vendor.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
//Handle child and associated items:
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
await SearchIndexAsync(newObject, true);
|
||||||
await SearchIndexAsync(outObj, true);
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
return newObject;
|
||||||
|
|
||||||
return outObj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//DUPLICATE
|
//DUPLICATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Vendor> DuplicateAsync(long id)
|
||||||
internal async Task<Vendor> DuplicateAsync(Vendor dbObj)
|
|
||||||
{
|
{
|
||||||
|
Vendor dbObject = await GetAsync(id, false);
|
||||||
Vendor outObj = new Vendor();
|
if (dbObject == null)
|
||||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
{
|
||||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
//generate unique name
|
return null;
|
||||||
|
}
|
||||||
|
Vendor newObject = new Vendor();
|
||||||
|
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||||
string newUniqueName = string.Empty;
|
string newUniqueName = string.Empty;
|
||||||
bool NotUnique = true;
|
bool NotUnique = true;
|
||||||
long l = 1;
|
long l = 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||||
NotUnique = await ct.Vendor.AnyAsync(m => m.Name == newUniqueName);
|
NotUnique = await ct.Vendor.AnyAsync(m => m.Name == newUniqueName);
|
||||||
} while (NotUnique);
|
} while (NotUnique);
|
||||||
|
newObject.Name = newUniqueName;
|
||||||
outObj.Name = newUniqueName;
|
newObject.Id = 0;
|
||||||
|
newObject.Concurrency = 0;
|
||||||
|
await ct.Vendor.AddAsync(newObject);
|
||||||
outObj.Id = 0;
|
|
||||||
outObj.Concurrency = 0;
|
|
||||||
|
|
||||||
await ct.Vendor.AddAsync(outObj);
|
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||||
|
await SearchIndexAsync(newObject, true);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
//Handle child and associated items:
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
//GET
|
||||||
await SearchIndexAsync(outObj, true);
|
//
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
internal async Task<Vendor> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
return outObj;
|
{
|
||||||
|
var ret = await ct.Vendor.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
if (logTheGetEvent && ret != null)
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//UPDATE
|
//UPDATE
|
||||||
//
|
//
|
||||||
|
internal async Task<Vendor> PutAsync(Vendor putObject)
|
||||||
//put
|
|
||||||
internal async Task<bool> PutAsync(Vendor dbObj, Vendor inObj)
|
|
||||||
{
|
{
|
||||||
|
Vendor dbObject = await ct.Vendor.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
if (dbObject == null)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Vendor SnapshotOfOriginalDBObj = new Vendor();
|
Vendor SnapshotOfOriginalDBObj = new Vendor();
|
||||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||||
|
CopyObject.Copy(putObject, dbObject, "Id");
|
||||||
//Replace the db object with the PUT object
|
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||||
CopyObject.Copy(inObj, dbObj, "Id");
|
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||||
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
if (HasErrors) return null;
|
||||||
|
try
|
||||||
//Set "original" value of concurrency token to input token
|
{
|
||||||
//this will allow EF to check it out
|
await ct.SaveChangesAsync();
|
||||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
if (!await ExistsAsync(putObject.Id))
|
||||||
if (HasErrors)
|
AddError(ApiErrorCode.NOT_FOUND);
|
||||||
return false;
|
else
|
||||||
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||||
//Log event and save context
|
return null;
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
}
|
||||||
await SearchIndexAsync(dbObj, false);
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
await SearchIndexAsync(dbObject, false);
|
||||||
|
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||||
return true;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//DELETE
|
||||||
|
//
|
||||||
|
internal async Task<bool> DeleteAsync(long id)
|
||||||
|
{
|
||||||
|
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Vendor dbObject = await ct.Vendor.SingleOrDefaultAsync(m => m.Id == id);
|
||||||
|
ValidateCanDelete(dbObject);
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
if (HasErrors)
|
||||||
|
return false;
|
||||||
|
ct.Vendor.Remove(dbObject);
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//Log event
|
||||||
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||||
|
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||||
|
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||||
|
//all good do the commit
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//SEARCH
|
||||||
|
//
|
||||||
private async Task SearchIndexAsync(Vendor obj, bool isNew)
|
private async Task SearchIndexAsync(Vendor obj, bool isNew)
|
||||||
{
|
{
|
||||||
//SEARCH INDEXING
|
|
||||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||||
else
|
else
|
||||||
@@ -180,38 +189,14 @@ namespace AyaNova.Biz
|
|||||||
return SearchParams;
|
return SearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//DELETE
|
|
||||||
//
|
|
||||||
internal async Task<bool> DeleteAsync(Vendor dbObj)
|
|
||||||
{
|
|
||||||
//Determine if the object can be deleted, do the deletion tentatively
|
|
||||||
//Probably also in here deal with tags and associated search text etc
|
|
||||||
|
|
||||||
//NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj);
|
|
||||||
if (HasErrors)
|
|
||||||
return false;
|
|
||||||
ct.Vendor.Remove(dbObj);
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
|
|
||||||
//Log event
|
|
||||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
|
|
||||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
|
|
||||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//VALIDATION
|
//VALIDATION
|
||||||
//
|
//
|
||||||
|
|
||||||
//Can save or update?
|
|
||||||
private async Task ValidateAsync(Vendor proposedObj, Vendor currentObj)
|
private async Task ValidateAsync(Vendor proposedObj, Vendor currentObj)
|
||||||
{
|
{
|
||||||
|
|
||||||
//run validation and biz rules
|
|
||||||
bool isNew = currentObj == null;
|
bool isNew = currentObj == null;
|
||||||
|
|
||||||
//Name required
|
//Name required
|
||||||
@@ -248,13 +233,10 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ValidateCanDelete(Vendor inObj)
|
||||||
//Can delete?
|
{
|
||||||
// private void ValidateCanDelete(Vendor inObj)
|
//whatever needs to be check to delete this object
|
||||||
// {
|
}
|
||||||
// //whatever needs to be check to delete this object
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user