This commit is contained in:
2020-12-09 00:46:29 +00:00
parent cb8ab52327
commit 7e0fa69d4a
2 changed files with 32 additions and 7 deletions

View File

@@ -156,7 +156,7 @@ namespace AyaNova.Biz
AddError(ApiErrorCode.NOT_FOUND);
return false;
}
await ValidateCanDelete(dbObject);
await ValidateCanDeleteAsync(dbObject);
if (HasErrors)
return false;
@@ -286,7 +286,7 @@ namespace AyaNova.Biz
}
private async Task ValidateCanDelete(Customer inObj)
private async Task ValidateCanDeleteAsync(Customer inObj)
{
//## NOTE: contact isn't so important, this could be changed to check only more important things like workorders etc
//and just attempt to delete all the contacts if possible, but for now....

View File

@@ -151,15 +151,31 @@ namespace AyaNova.Biz
try
{
HeadOffice dbObject = await ct.HeadOffice.SingleOrDefaultAsync(z => z.Id == id);
if (dbObject == null){
if (dbObject == null)
{
AddError(ApiErrorCode.NOT_FOUND);
return false;
}
ValidateCanDelete(dbObject);
if (HasErrors)
return false;
if (HasErrors)
return false;
//DELETE DIRECT CHILD OBJECTS
{
var ContactIds = await ct.User.AsNoTracking().Where(z => z.HeadOfficeId == id).Select(z => z.Id).ToListAsync();
if (ContactIds.Count() > 0)
{
UserBiz b = new UserBiz(ct, UserId, UserTranslationId, CurrentUserRoles);
foreach (long ItemId in ContactIds)
if (!await b.DeleteAsync(ItemId, transaction))
{
AddError(ApiErrorCode.CHILD_OBJECT_ERROR, null, $"HeadOfficeContact [{ItemId}]: {b.GetErrorsAsString()}");
return false;
}
}
}
ct.HeadOffice.Remove(dbObject);
await ct.SaveChangesAsync();
@@ -173,6 +189,8 @@ namespace AyaNova.Biz
}
catch
{
//NOTE: no need to rollback the transaction, it will auto-rollback if not committed and it is disposed when it goes out of scope either way
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
throw;
}
@@ -246,9 +264,16 @@ namespace AyaNova.Biz
}
private void ValidateCanDelete(HeadOffice inObj)
private async Task ValidateCanDeleteAsync(HeadOffice inObj)
{
//whatever needs to be check to delete this object
//Referential integrity
if (await ct.Customer.AnyAsync(z => z.HeadOfficeId == inObj.Id) == true)
{
//Note: errorbox will ensure it appears in the general errror box and not field specific
//the translation key is to indicate what the linked object is that is causing the error
AddError(ApiErrorCode.VALIDATION_REFERENTIAL_INTEGRITY, "errorbox", "LT:Customer");
}
}