コード例 #1
0
        /// <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));
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Finds lifecycle status with identifier provided and returns its DTO
 /// </summary>
 /// <param name="lifecycleStatusId"></param>
 /// <returns></returns>
 public ILifecycleStatusDto GetLifecycleStatus(int lifecycleStatusId)
 {
     using (var context = new PrometheusContext())
     {
         var lifecycleStatus = context.LifecycleStatuses.Find(lifecycleStatusId);
         if (lifecycleStatus != null)
         {
             return(ManualMapper.MapLifecycleStatusToDto(lifecycleStatus));
         }
         return(null);
     }
 }
コード例 #3
0
        /// <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));
            }
        }
コード例 #4
0
        /// <summary>
        /// KVP of all lifecycle IDs and names in ascending order by name
        /// </summary>
        /// <returns></returns>
        public IEnumerable <Tuple <int, string> > GetLifecycleStatusNames()
        {
            using (var context = new PrometheusContext())
            {
                var lifecycleStatusRecords = context.LifecycleStatuses;

                //Empty list
                if (!lifecycleStatusRecords.Any())
                {
                    return(new List <Tuple <int, string> >());
                }

                var statuses = new List <LifecycleStatusDto>();
                foreach (var status in lifecycleStatusRecords)                   //mapping and linq don't seem to get along
                {
                    statuses.Add(ManualMapper.MapLifecycleStatusToDto(status));
                }

                var nameList = new List <Tuple <int, string> >();
                nameList.AddRange(statuses.OrderBy(x => x.Position).Select(x => new Tuple <int, string>(x.Id, x.Name)));
                return(nameList.OrderBy(x => x.Item1));
            }
        }