public int MoveWorkItemToPrevious(WorkItemDto workItemDto)
        {
            if (workItemDto == null)
            {
                throw new ArgumentNullException(nameof(workItemDto), "WorkItem cannot be null");
            }

            int workItemWeekId = workItemDto.WeekId;
            int previousWeekId = -1;

            using (WeekInfoRepository weekInfoRepository = new WeekInfoRepository())
            {
                WeekInfo weekInfo = weekInfoRepository.Find(x => x.Id == workItemWeekId);
                if (weekInfo == null)
                {
                    throw new Exception("Week not found.");
                }

                DateTime startDate = weekInfo.StartDate;
                DateTime nextDate  = startDate.AddDays(-7);
                weekInfo = weekInfoRepository
                           .Find(x => DbFunctions.TruncateTime(x.StartDate) == nextDate);
                if (weekInfo == null)
                {
                    throw new Exception("Next Week not found.");
                }
                previousWeekId = weekInfo.Id;
            }

            using (WorkItemRepository workItemRepository = new WorkItemRepository())
            {
                UserWorkItem workItem = workItemRepository.Find(x => x.Id == workItemDto.Id);
                if (workItem == null)
                {
                    throw new Exception("Work Item not found");
                }

                // Validate if work item already exists in previous week.
                UserWorkItem previousWeekWorkItem = workItemRepository
                                                    .Find(x => x.TaskId == workItemDto.TaskId &&
                                                          x.WeekId == previousWeekId &&
                                                          x.ServerId == workItemDto.ServerId);
                if (previousWeekWorkItem != null)
                {
                    workItemRepository.Delete(previousWeekWorkItem);
                }

                workItem.WeekId = previousWeekId;
                workItem.State  = WorkItemState.New;
                int nextWorkItemId = workItemRepository.Insert(workItem);

                UserWorkItem currentWorkItem = workItemRepository.Find(x => x.Id == workItemDto.Id);
                // update the current work item.
                currentWorkItem.State = WorkItemState.Moved;
                workItemRepository.Update(currentWorkItem);

                return(nextWorkItemId);
            }
        }
        public int CopyWorkItemToNext(WorkItemDto workItemDto)
        {
            if (workItemDto == null)
            {
                throw new ArgumentNullException(nameof(workItemDto), "WorkItem cannot be null");
            }

            int workItemWeekId = workItemDto.WeekId;
            int nextWeekId     = -1;

            using (WeekInfoRepository weekInfoRepository = new WeekInfoRepository())
            {
                WeekInfo weekInfo = weekInfoRepository.Find(x => x.Id == workItemWeekId);
                if (weekInfo == null)
                {
                    throw new Exception("Week not found.");
                }

                DateTime startDate = weekInfo.StartDate;
                DateTime nextDate  = startDate.AddDays(7);
                weekInfo = weekInfoRepository
                           .Find(x => DbFunctions.TruncateTime(x.StartDate) == nextDate);
                if (weekInfo == null)
                {
                    throw new Exception("Next Week not found.");
                }
                nextWeekId = weekInfo.Id;
            }

            using (WorkItemRepository workItemRepository = new WorkItemRepository())
            {
                UserWorkItem workItem = workItemRepository.Find(x => x.Id == workItemDto.Id);
                if (workItem == null)
                {
                    throw new Exception("Work Item not found");
                }

                // Validate if work item already exists in next week.
                UserWorkItem nextWeekWorkItem = workItemRepository
                                                .Find(x => x.TaskId == workItemDto.TaskId &&
                                                      x.WeekId == nextWeekId &&
                                                      x.ServerId == workItemDto.ServerId);
                if (nextWeekWorkItem != null)
                {
                    workItemRepository.Delete(nextWeekWorkItem);
                }

                // Clone the work item.
                workItem.WeekId = nextWeekId;
                workItem.State  = WorkItemState.New;
                int nextWorkItemId = workItemRepository.Insert(workItem);

                return(nextWorkItemId);
            }
        }
 public bool DeleteWorkItem(WorkItemDto dto, string userId)
 {
     using (WorkItemRepository workItemRepository = new WorkItemRepository())
     {
         UserWorkItem workItem = workItemRepository.Find(x => x.Id == dto.Id);
         if (workItem != null)
         {
             workItemRepository.Delete(workItem);
             return(true);
         }
     }
     return(false);
 }