/// <summary> /// Creates the entity in the database /// </summary> /// <param name="performingUserId">User creating the entity</param> /// <param name="entity">Entity to be created</param> /// <returns>Created entity DTO</returns> protected override IScriptedSelectionInputDto Create(int performingUserId, IScriptedSelectionInputDto entity) { using (var context = new PrometheusContext()) { var input = context.ScriptedSelectionInputs.Find(entity.Id); if (input != null) { throw new InvalidOperationException(string.Format("Scripted Selection with ID {0} already exists.", entity.Id)); } var saveInput = context.ScriptedSelectionInputs.Add(ManualMapper.MapDtoToScriptedSelectionInput(entity)); context.SaveChanges(performingUserId); return(ManualMapper.MapScriptedSelectionInputToDto(saveInput)); } }
/// <summary> /// Updates the entity in the database /// </summary> /// <param name="performingUserId">User updating the entity</param> /// <param name="entity">Entity to be updated</param> /// <returns>Updated entity DTO</returns> protected override IScriptedSelectionInputDto Update(int performingUserId, IScriptedSelectionInputDto entity) { using (var context = new PrometheusContext()) { if (!context.ScriptedSelectionInputs.Any(x => x.Id == entity.Id)) { throw new InvalidOperationException( string.Format("Service Measure with ID {0} cannot be updated since it does not exist.", entity.Id)); } var updatedScriptedSelection = ManualMapper.MapDtoToScriptedSelectionInput(entity); context.ScriptedSelectionInputs.Attach(updatedScriptedSelection); context.Entry(updatedScriptedSelection).State = EntityState.Modified; context.SaveChanges(performingUserId); return(ManualMapper.MapScriptedSelectionInputToDto(updatedScriptedSelection)); } }