private EntityOperationResult UpdateInternal(EntityUpdate update) { if (!RunInspection(update)) { return(EntityOperationResult.FailResult(new EntityOperationError("Insufficient rights to perform this operation.", EntityOperationError.ACCESS_VIOLATION))); } EntityOperationContext ctx = new EntityOperationContext(); this.AppyLogicBefore(update, ctx); var entity = update.ToEntity(); EntityOperationResult result = null; bool created = false; try { if (update.Id.HasValue) { if (update.PropertyUpdates.Count > 0) { _repository.Update(entity); } } else { update.Id = _repository.Create(entity); created = true; } //Order by operation - process detach first foreach (var relUpdate in update.RelationUpdates.OrderByDescending(ru => ru.Operation)) { if (relUpdate.Operation == RelationOperation.Attach) { _repository.Attach(entity, relUpdate.ToRelation()); } else if (relUpdate.Operation == RelationOperation.Detach) { _repository.Detach(entity, relUpdate.ToRelation()); } else if (relUpdate.PropertyUpdates.Count > 0) { _repository.UpdateRelation(entity, relUpdate.ToRelation()); } } result = EntityOperationResult.SuccessResult(); if (created) { result.Data.Add("Created", update.Id.Value); } } catch (UniqueRuleViolationException rex) { result = EntityOperationResult.FailResult(new EntityOperationError(rex.Message, EntityOperationError.UNIQUE_RULE_VIOLATION)); } this.AppyLogicAfter(update, ctx, result); return(result); }
public EntityOperationResult Delete(Services.tmp.EntityDelete delete) { using (var dbContext = _dbService.GetDatabaseContext(true)) { if (!RunInspection(delete)) { return(EntityOperationResult.FailResult(new EntityOperationError("Insufficient rights to perform this operation.", EntityOperationError.ACCESS_VIOLATION))); } var ctx = new EntityOperationContext(); this.AppyLogicBefore(delete, ctx); EntityOperationResult result = null; try { _repository.Delete(new Entity(delete.Entity, delete.Id.Value), delete.Recursive); result = EntityOperationResult.SuccessResult(); } catch (RelationExistsException ree) { result = EntityOperationResult.FailResult(new EntityOperationError(ree.Message, EntityOperationError.RELATION_EXISTS)); } catch (Exception ex) { result = EntityOperationResult.FailResult(new EntityOperationError(ex.Message)); } this.AppyLogicAfter(delete, ctx, result); if (result.Success) { dbContext.Complete(); } return(result); } }