/// <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 ILifecycleStatusDto Create(int performingUserId, ILifecycleStatusDto lifecycleStatus) { using (var context = new PrometheusContext()) { var existingStatus = context.LifecycleStatuses.Find(lifecycleStatus.Id); if (existingStatus == null) { //Insert at correct Position foreach (var status in context.LifecycleStatuses) { if (status.Position >= lifecycleStatus.Position) { status.Position++; context.LifecycleStatuses.Attach(status); context.Entry(status).State = EntityState.Modified; } } var savedStatus = context.LifecycleStatuses.Add(ManualMapper.MapDtoToLifecycleStatus(lifecycleStatus)); context.SaveChanges(performingUserId); return(ManualMapper.MapLifecycleStatusToDto(savedStatus)); } else { throw new InvalidOperationException(string.Format("Lifecycle Status with ID {0} already exists.", lifecycleStatus.Id)); } } }
private int CreateFakeLifecycleStatus(string name) { var fakeStatus = A.Fake <ILifecycleStatusDto>(); A.CallTo(() => fakeStatus.Name).Returns(name); using (var context = new PrometheusContext()) { var status = ManualMapper.MapDtoToLifecycleStatus(fakeStatus); context.LifecycleStatuses.Add(status); context.SaveChanges(); return(status.Id); } }
/// <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 ILifecycleStatusDto Update(int performingUserId, ILifecycleStatusDto lifecycleStatusDto) { using (var context = new PrometheusContext()) { var statusEntity = context.LifecycleStatuses.FirstOrDefault(x => x.Id == lifecycleStatusDto.Id); if (statusEntity == null) { throw new InvalidOperationException("Lifecycle Status record must exist in order to be updated."); } var currentPosition = statusEntity.Position; var newPosition = lifecycleStatusDto.Position; foreach (var status in context.LifecycleStatuses) { //Update positions to allow for position change backwards if (newPosition < currentPosition && status.Position >= newPosition && status.Position < currentPosition) { status.Position++; context.LifecycleStatuses.Attach(status); context.Entry(status).State = EntityState.Modified; } //Update positions to allow for position change forwards if (newPosition > currentPosition && status.Position <= newPosition && status.Position > currentPosition) { status.Position--; context.LifecycleStatuses.Attach(status); context.Entry(status).State = EntityState.Modified; } } var updatedStatus = ManualMapper.MapDtoToLifecycleStatus(lifecycleStatusDto); context.LifecycleStatuses.Attach(updatedStatus); context.Entry(updatedStatus).State = EntityState.Modified; context.SaveChanges(performingUserId); return(ManualMapper.MapLifecycleStatusToDto(updatedStatus)); } }