This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using AyaNova.Util;
|
||||
using AyaNova.Api.ControllerHelpers;
|
||||
using AyaNova.Models;
|
||||
|
||||
namespace AyaNova.Biz
|
||||
{
|
||||
|
||||
internal class ContractBiz : BizObject, ISearchAbleObject
|
||||
{
|
||||
|
||||
internal ContractBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||
{
|
||||
ct = dbcontext;
|
||||
@@ -22,15 +19,12 @@ namespace AyaNova.Biz
|
||||
|
||||
internal static ContractBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||
{
|
||||
|
||||
if (httpContext != null)
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//EXISTS
|
||||
internal async Task<bool> ExistsAsync(long id)
|
||||
@@ -38,12 +32,9 @@ namespace AyaNova.Biz
|
||||
return await ct.Contract.AnyAsync(e => e.Id == id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//CREATE
|
||||
|
||||
//Called from route and also seeder
|
||||
//
|
||||
internal async Task<Contract> CreateAsync(Contract newObject)
|
||||
{
|
||||
await ValidateAsync(newObject, null);
|
||||
@@ -51,122 +42,138 @@ namespace AyaNova.Biz
|
||||
return null;
|
||||
else
|
||||
{
|
||||
//do stuff with Contract
|
||||
Contract outObj = newObject;
|
||||
|
||||
outObj.Tags = TagUtil.NormalizeTags(outObj.Tags);
|
||||
outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields);
|
||||
//Save to db
|
||||
await ct.Contract.AddAsync(outObj);
|
||||
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||
await ct.Contract.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
//Handle child and associated items:
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
||||
await SearchIndexAsync(outObj, true);
|
||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
||||
|
||||
return outObj;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//DUPLICATE
|
||||
//
|
||||
|
||||
internal async Task<Contract> DuplicateAsync(Contract dbObj)
|
||||
internal async Task<Contract> DuplicateAsync(long id)
|
||||
{
|
||||
|
||||
Contract outObj = new Contract();
|
||||
CopyObject.Copy(dbObj, outObj, "Wiki");
|
||||
// outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
|
||||
//generate unique name
|
||||
Contract dbObject = await GetAsync(id, false);
|
||||
if (dbObject == null)
|
||||
{
|
||||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||
return null;
|
||||
}
|
||||
Contract newObject = new Contract();
|
||||
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||
string newUniqueName = string.Empty;
|
||||
bool NotUnique = true;
|
||||
long l = 1;
|
||||
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);
|
||||
} while (NotUnique);
|
||||
|
||||
outObj.Name = newUniqueName;
|
||||
|
||||
|
||||
outObj.Id = 0;
|
||||
outObj.Concurrency = 0;
|
||||
|
||||
await ct.Contract.AddAsync(outObj);
|
||||
newObject.Name = newUniqueName;
|
||||
newObject.Id = 0;
|
||||
newObject.Concurrency = 0;
|
||||
await ct.Contract.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
|
||||
//Handle child and associated items:
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
|
||||
await SearchIndexAsync(outObj, true);
|
||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null);
|
||||
return outObj;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// GET
|
||||
///
|
||||
///
|
||||
|
||||
internal async Task<Contract> GetAsync(long fetchId, bool logTheGetEvent = true)
|
||||
//GET
|
||||
//
|
||||
internal async Task<Contract> GetAsync(long id, 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 == fetchId);
|
||||
var ret = await ct.Contract.SingleOrDefaultAsync(m => m.Id == id);
|
||||
if (logTheGetEvent && ret != null)
|
||||
{
|
||||
//Log
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
|
||||
}
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//UPDATE
|
||||
//
|
||||
|
||||
//put
|
||||
internal async Task<bool> PutAsync(Contract dbObj, Contract inObj)
|
||||
internal async Task<Contract> PutAsync(Contract putObject)
|
||||
{
|
||||
|
||||
//make a snapshot of the original for validation but update the original to preserve workflow
|
||||
Contract dbObject = await ct.Contract.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||||
if (dbObject == null)
|
||||
{
|
||||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||
return null;
|
||||
}
|
||||
Contract SnapshotOfOriginalDBObj = new Contract();
|
||||
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
|
||||
|
||||
//Replace the db object with the PUT object
|
||||
CopyObject.Copy(inObj, dbObj, "Id");
|
||||
|
||||
dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags);
|
||||
dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields);
|
||||
|
||||
//Set "original" value of concurrency token to input token
|
||||
//this will allow EF to check it out
|
||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
||||
|
||||
|
||||
await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
|
||||
if (HasErrors)
|
||||
return false;
|
||||
|
||||
//Log event and save context
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
|
||||
await SearchIndexAsync(dbObj, false);
|
||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||
|
||||
return true;
|
||||
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||
CopyObject.Copy(putObject, dbObject, "Id");
|
||||
dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
|
||||
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||
if (HasErrors) return null;
|
||||
try
|
||||
{
|
||||
await ct.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!await ExistsAsync(putObject.Id))
|
||||
AddError(ApiErrorCode.NOT_FOUND);
|
||||
else
|
||||
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||
return null;
|
||||
}
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||
await SearchIndexAsync(dbObject, false);
|
||||
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||
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)
|
||||
{
|
||||
//SEARCH INDEXING
|
||||
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);
|
||||
|
||||
if (isNew)
|
||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||
else
|
||||
@@ -182,38 +189,14 @@ namespace AyaNova.Biz
|
||||
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
|
||||
//
|
||||
|
||||
//Can save or update?
|
||||
private async Task ValidateAsync(Contract proposedObj, Contract currentObj)
|
||||
{
|
||||
|
||||
//run validation and biz rules
|
||||
bool isNew = currentObj == null;
|
||||
|
||||
//Name required
|
||||
@@ -250,13 +233,10 @@ namespace AyaNova.Biz
|
||||
|
||||
}
|
||||
|
||||
|
||||
//Can delete?
|
||||
// private void ValidateCanDelete(Contract inObj)
|
||||
// {
|
||||
// //whatever needs to be check to delete this object
|
||||
// }
|
||||
|
||||
private void ValidateCanDelete(Contract inObj)
|
||||
{
|
||||
//whatever needs to be check to delete this object
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user