This commit is contained in:
2020-05-13 21:18:18 +00:00
parent 53d1456e6f
commit a45abb7467
4 changed files with 284 additions and 160 deletions

View File

@@ -11,7 +11,6 @@ using AyaNova.Biz;
namespace AyaNova.Api.Controllers namespace AyaNova.Api.Controllers
{ {
[ApiController] [ApiController]
[ApiVersion("8.0")] [ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/[controller]")] [Route("api/v{version:apiVersion}/[controller]")]
@@ -23,7 +22,6 @@ namespace AyaNova.Api.Controllers
private readonly ILogger<CustomerController> log; private readonly ILogger<CustomerController> log;
private readonly ApiServerState serverState; private readonly ApiServerState serverState;
/// <summary> /// <summary>
/// ctor /// ctor
/// </summary> /// </summary>
@@ -84,7 +82,6 @@ namespace AyaNova.Api.Controllers
return CreatedAtAction(nameof(CustomerController.GetCustomer), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); return CreatedAtAction(nameof(CustomerController.GetCustomer), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
} }
/// <summary> /// <summary>
/// Get Customer /// Get Customer
/// </summary> /// </summary>
@@ -105,8 +102,6 @@ namespace AyaNova.Api.Controllers
return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType)));
} }
/// <summary> /// <summary>
/// Put (update) Customer /// Put (update) Customer
/// </summary> /// </summary>

View File

@@ -46,8 +46,38 @@ namespace AyaNova.Api.Controllers
serverState = apiServerState; serverState = apiServerState;
} }
// /// <summary>
// /// Create widget
// /// </summary>
// /// <param name="newObject"></param>
// /// <param name="apiVersion">From route path</param>
// /// <returns></returns>
// [HttpPost]
// public async Task<IActionResult> PostWidget([FromBody] Widget newObject, ApiVersion apiVersion)
// {
// if (!serverState.IsOpen)
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// //Instantiate the business object handler
// WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
// //If a user has change roles
// if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
// return StatusCode(403, new ApiNotAuthorizedResponse());
// if (!ModelState.IsValid)
// return BadRequest(new ApiErrorResponse(ModelState));
// //Create and validate
// Widget o = await biz.CreateAsync(newObject);
// if (o == null)
// return BadRequest(new ApiErrorResponse(biz.Errors));
// else
// return CreatedAtAction(nameof(WidgetController.GetWidget), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
// }
/// <summary> /// <summary>
/// Create widget /// Create Widget
/// </summary> /// </summary>
/// <param name="newObject"></param> /// <param name="newObject"></param>
/// <param name="apiVersion">From route path</param> /// <param name="apiVersion">From route path</param>
@@ -57,27 +87,79 @@ namespace AyaNova.Api.Controllers
{ {
if (!serverState.IsOpen) if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
//Instantiate the business object handler
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
//If a user has change roles
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse()); return StatusCode(403, new ApiNotAuthorizedResponse());
if (!ModelState.IsValid) if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState)); return BadRequest(new ApiErrorResponse(ModelState));
//Create and validate
Widget o = await biz.CreateAsync(newObject); Widget o = await biz.CreateAsync(newObject);
if (o == null) if (o == null)
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
else else
return CreatedAtAction(nameof(WidgetController.GetWidget), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); return CreatedAtAction(nameof(WidgetController.GetWidget), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
} }
// /// <summary>
// /// Duplicate widget
// /// </summary>
// /// <param name="id">Create a duplicate of this items id</param>
// /// <param name="apiVersion">From route path</param>
// /// <returns></returns>
// [HttpPost("duplicate/{id}")]
// public async Task<IActionResult> DuplicateWidget([FromRoute] long id, ApiVersion apiVersion)
// {
// if (!serverState.IsOpen)
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// //Instantiate the business object handler
// WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
// //If a user has change roles
// if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
// return StatusCode(403, new ApiNotAuthorizedResponse());
// if (!ModelState.IsValid)
// return BadRequest(new ApiErrorResponse(ModelState));
// var oSrc = await biz.GetAsync(id, false);
// if (oSrc == null)
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
// //Create and validate
// Widget o = await biz.DuplicateAsync(oSrc);
// if (o == null)
// return BadRequest(new ApiErrorResponse(biz.Errors));
// else
// return CreatedAtAction(nameof(WidgetController.GetWidget), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
// }
/// <summary>
/// Duplicate Widget
/// (Wiki and Attachments are not duplicated)
/// </summary>
/// <param name="id">Source object id</param>
/// <param name="apiVersion">From route path</param>
/// <returns>Widget</returns>
[HttpPost("duplicate/{id}")]
public async Task<IActionResult> DuplicateWidget([FromRoute] long id, ApiVersion apiVersion)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse());
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
Widget o = await biz.DuplicateAsync(id);
if (o == null)
return BadRequest(new ApiErrorResponse(biz.Errors));
else
return CreatedAtAction(nameof(WidgetController.GetWidget), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
}
/// <summary> /// <summary>
/// Get full widget object /// Get full widget object
/// </summary> /// </summary>
@@ -154,40 +236,7 @@ namespace AyaNova.Api.Controllers
/// <summary>
/// Duplicate widget
/// </summary>
/// <param name="id">Create a duplicate of this items id</param>
/// <param name="apiVersion">From route path</param>
/// <returns></returns>
[HttpPost("duplicate/{id}")]
public async Task<IActionResult> DuplicateWidget([FromRoute] long id, ApiVersion apiVersion)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
//Instantiate the business object handler
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
//If a user has change roles
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse());
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
var oSrc = await biz.GetAsync(id, false);
if (oSrc == null)
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
//Create and validate
Widget o = await biz.DuplicateAsync(oSrc);
if (o == null)
return BadRequest(new ApiErrorResponse(biz.Errors));
else
return CreatedAtAction(nameof(WidgetController.GetWidget), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
}

View File

@@ -6,10 +6,8 @@ using AyaNova.Models;
namespace AyaNova.Biz namespace AyaNova.Biz
{ {
internal class CustomerBiz : BizObject, ISearchAbleObject internal class CustomerBiz : BizObject, ISearchAbleObject
{ {
internal CustomerBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) internal CustomerBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
{ {
ct = dbcontext; ct = dbcontext;
@@ -27,8 +25,6 @@ namespace AyaNova.Biz
return new CustomerBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); return new CustomerBiz(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)
@@ -90,8 +86,7 @@ namespace AyaNova.Biz
} }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
// GET //GET
//
// //
internal async Task<Customer> GetAsync(long id, bool logTheGetEvent = true) internal async Task<Customer> GetAsync(long id, bool logTheGetEvent = true)
{ {
@@ -138,7 +133,6 @@ namespace AyaNova.Biz
return dbObject; return dbObject;
} }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//DELETE //DELETE
// //
@@ -174,7 +168,7 @@ namespace AyaNova.Biz
} }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
// SEARCH //SEARCH
// //
private async Task SearchIndexAsync(Customer obj, bool isNew) private async Task SearchIndexAsync(Customer obj, bool isNew)
{ {

View File

@@ -1,6 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using EnumsNET; using EnumsNET;
using AyaNova.Util; using AyaNova.Util;
using AyaNova.Api.ControllerHelpers; using AyaNova.Api.ControllerHelpers;
@@ -8,10 +7,8 @@ using AyaNova.Models;
namespace AyaNova.Biz namespace AyaNova.Biz
{ {
internal class WidgetBiz : BizObject, IJobObject, ISearchAbleObject internal class WidgetBiz : BizObject, IJobObject, ISearchAbleObject
{ {
internal WidgetBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) internal WidgetBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
{ {
ct = dbcontext; ct = dbcontext;
@@ -23,15 +20,12 @@ namespace AyaNova.Biz
internal static WidgetBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) internal static WidgetBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
{ {
if (httpContext != null) if (httpContext != null)
return new WidgetBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); return new WidgetBiz(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 WidgetBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); return new WidgetBiz(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)
@@ -39,40 +33,21 @@ namespace AyaNova.Biz
return await ct.Widget.AnyAsync(e => e.Id == id); return await ct.Widget.AnyAsync(e => e.Id == id);
} }
////////////////////////////////////////////////////////////////////////////////////////////////
/// GET
///
///
internal async Task<Widget> 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.Widget.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<Widget> CreateAsync(Widget newObject) internal async Task<Widget> CreateAsync(Widget newObject)
{ {
await ValidateAsync(newObject, null); await ValidateAsync(newObject, null);
if (HasErrors) if (HasErrors)
return null; return null;
else else
{ {
newObject.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext(); newObject.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext();
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags); newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields); newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
await ct.Widget.AddAsync(newObject); await ct.Widget.AddAsync(newObject);
await ct.SaveChangesAsync(); await ct.SaveChangesAsync();
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct); await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
await SearchIndexAsync(newObject, true); await SearchIndexAsync(newObject, true);
await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null); await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
@@ -80,84 +55,215 @@ namespace AyaNova.Biz
} }
} }
// ////////////////////////////////////////////////////////////////////////////////////////////////
// //DUPLICATE
// //
// internal async Task<Widget> DuplicateAsync(Widget dbObj)
// {
// Widget outObj = new Widget();
// CopyObject.Copy(dbObj, outObj, "Wiki");
// // outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
// //generate unique name
// string newUniqueName = string.Empty;
// bool NotUnique = true;
// long l = 1;
// do
// {
// newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255);
// NotUnique = await ct.Widget.AnyAsync(m => m.Name == newUniqueName);
// } while (NotUnique);
// outObj.Name = newUniqueName;
// outObj.Id = 0;
// outObj.Concurrency = 0;
// //Test get serial id visible id number from generator
// outObj.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext();
// await ct.Widget.AddAsync(outObj);
// 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;
// }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//DUPLICATE //DUPLICATE
// //
internal async Task<Widget> DuplicateAsync(long id)
internal async Task<Widget> DuplicateAsync(Widget dbObj)
{ {
Widget dbObject = await GetAsync(id, false);
Widget outObj = new Widget(); 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;
}
Widget newObject = new Widget();
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.Widget.AnyAsync(m => m.Name == newUniqueName); NotUnique = await ct.Widget.AnyAsync(m => m.Name == newUniqueName);
} while (NotUnique); } while (NotUnique);
newObject.Name = newUniqueName;
outObj.Name = newUniqueName; newObject.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext();
newObject.Id = 0;
newObject.Concurrency = 0;
outObj.Id = 0; await ct.Widget.AddAsync(newObject);
outObj.Concurrency = 0;
//Test get serial id visible id number from generator
outObj.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext();
await ct.Widget.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
//
internal async Task<Widget> GetAsync(long id, bool logTheGetEvent = true)
{
var ret = await ct.Widget.SingleOrDefaultAsync(m => m.Id == id);
if (logTheGetEvent && ret != null)
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
return ret;
}
// ////////////////////////////////////////////////////////////////////////////////////////////////
// //UPDATE
// //
// //put
// internal async Task<bool> PutAsync(Widget dbObj, Widget inObj)
// {
// //make a snapshot of the original for validation but update the original to preserve workflow
// Widget SnapshotOfOriginalDBObj = new Widget();
// CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj);
// //Replace the db object with the PUT object
// CopyObject.Copy(inObj, dbObj, "Id,Serial");
// 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;
// }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//UPDATE //UPDATE
// //
internal async Task<Widget> PutAsync(Widget putObject)
//put
internal async Task<bool> PutAsync(Widget dbObj, Widget inObj)
{ {
Widget dbObject = await ct.Widget.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;
}
Widget SnapshotOfOriginalDBObj = new Widget(); Widget SnapshotOfOriginalDBObj = new Widget();
CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
CopyObject.Copy(putObject, dbObject, "Id,Serial");
//Replace the db object with the PUT object dbObject.Tags = TagUtil.NormalizeTags(dbObject.Tags);
CopyObject.Copy(inObj, dbObj, "Id,Serial"); 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(Widget 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.Widget.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;
// }
////////////////////////////////////////////////////////////////////////////////////////////////
//DELETE
//
internal async Task<bool> DeleteAsync(long id)
{
using (var transaction = await ct.Database.BeginTransactionAsync())
{
try
{
Widget dbObject = await ct.Widget.SingleOrDefaultAsync(m => m.Id == id);
ValidateCanDelete(dbObject);
if (HasErrors)
return false;
if (HasErrors)
return false;
ct.Widget.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;
}
}
private async Task SearchIndexAsync(Widget obj, bool isNew) private async Task SearchIndexAsync(Widget obj, bool isNew)
{ {
@@ -181,26 +287,6 @@ namespace AyaNova.Biz
return SearchParams; return SearchParams;
} }
////////////////////////////////////////////////////////////////////////////////////////////////
//DELETE
//
internal async Task<bool> DeleteAsync(Widget 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.Widget.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;
}