This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.JsonPatch;
|
||||
using System.Collections.Generic;
|
||||
using AyaNova.Util;
|
||||
using AyaNova.Api.ControllerHelpers;
|
||||
using AyaNova.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace AyaNova.Biz
|
||||
{
|
||||
@@ -378,10 +379,37 @@ namespace AyaNova.Biz
|
||||
//
|
||||
internal async Task<bool> ItemDeleteAsync(long id)
|
||||
{
|
||||
WorkOrderItem dbObject = await ct.WorkOrderItem.SingleOrDefaultAsync(m => m.Id == id);
|
||||
WorkOrderItem dbObject = await ct.WorkOrderItem
|
||||
.SingleOrDefaultAsync(m => m.Id == id);
|
||||
ItemValidateCanDelete(dbObject);
|
||||
if (HasErrors)
|
||||
return false;
|
||||
|
||||
//Traverse and delete tree
|
||||
//maybe need to get tree list of id's then work with that
|
||||
//a static function to get all id's from object and below?
|
||||
//https://docs.microsoft.com/en-us/ef/core/saving/transactions
|
||||
|
||||
WorkOrderItem dbObject = await ct.WorkOrderItem
|
||||
.Include(wi => wi.Labors)
|
||||
.Include(wi => wi.Parts)
|
||||
.SingleOrDefaultAsync(m => m.Id == id);
|
||||
var LaborIds = dbObject.Labors.Select(m => m.Id).ToList();
|
||||
var PartIds = dbObject.Parts.Select(m => m.Id).ToList();
|
||||
using (var transaction = ct.Database.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
//Delete children
|
||||
foreach(long l in LaborIds){
|
||||
if(!await LaborDeleteAsync(id)) return false;
|
||||
}
|
||||
foreach(long l in PartIds){
|
||||
if(!await LaborDeleteAsync(id)) return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ct.WorkOrderItem.Remove(dbObject);
|
||||
await ct.SaveChangesAsync();
|
||||
|
||||
@@ -389,6 +417,16 @@ namespace AyaNova.Biz
|
||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, "wo:" + dbObject.WorkOrderId.ToString(), ct);
|
||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType);
|
||||
await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||||
|
||||
//all good do the commit
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
//Just re-throw for now, but in future may want to deal with this more here
|
||||
throw;
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user