/// <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 ISelectionInputDto Create(int performingUserId, ISelectionInputDto entity) { using (var context = new PrometheusContext()) { var measure = context.SelectionInputs.Find(entity.Id); if (measure != null) { throw new InvalidOperationException(string.Format("Service Measure with ID {0} already exists.", entity.Id)); } var savedMeasure = context.SelectionInputs.Add(ManualMapper.MapDtoToSelectionInput(entity)); context.SaveChanges(performingUserId); return(ManualMapper.MapSelectionInputToDto(savedMeasure)); } }
/// <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 ISelectionInputDto Update(int performingUserId, ISelectionInputDto entity) { using (var context = new PrometheusContext()) { if (!context.SelectionInputs.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 updatedSelectionInput = ManualMapper.MapDtoToSelectionInput(entity); context.SelectionInputs.Attach(updatedSelectionInput); context.Entry(updatedSelectionInput).State = EntityState.Modified; context.SaveChanges(performingUserId); return(ManualMapper.MapSelectionInputToDto(updatedSelectionInput)); } }