예제 #1
0
        public async Task <ActionResult <List <ShiftViewModel> > > SaveShiftsAsync(List <ShiftViewModel> shiftViewModels)
        {
            if (shiftViewModels == null || shiftViewModels.Count == 0)
            {
                return(BadRequest("No valid shifts received"));
            }

            try
            {
                //filter duplicate dates
                shiftViewModels = shiftViewModels.GroupBy(s => s.Date.Date)
                                  .Select(g => g.OrderByDescending(x => x.Date.Date).First()).ToList();

                List <Shift> shifts = shiftViewModels.Select(ShiftViewModel.CreateShift)
                                      .ToList();
                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);

                if (shifts[0] != null)
                {
                    //get project and get task from db
                    Project project = (await projectService.GetProjectDetailsAsync(shifts[0]
                                                                                   .ProjectId)).Data;
                    Task task = null;
                    if (shifts[0]
                        .TaskId != null)
                    {
                        task = (await taskService.GetTaskAsync((Guid)shifts[0]
                                                               .TaskId)).Data;
                    }

                    foreach (Shift shift in shifts)
                    {
                        if (shift.Id != Guid.Empty || shift.TaskId == null || shift.ProjectId == Guid.Empty)
                        {
                            shifts.Remove(shift);
                        }

                        // check if projectId and taskId differs from above? getproject/task => add project and task to shift
                        if (project != null && project.Id != shift.ProjectId)
                        {
                            project = (await projectService.GetProjectDetailsAsync(shift.ProjectId)).Data;
                        }

                        if (task != null && shift.TaskId != null && task.Id != shift.TaskId)
                        {
                            task = (await taskService.GetTaskAsync((Guid)shift.TaskId)).Data;
                        }

                        if (project == null || task == null)
                        {
                            shifts.Remove(shift);
                        }
                        shift.Project    = project;
                        shift.Task       = task;
                        shift.LastEditBy = oid;
                    }
                }

                if (shifts.Count != shiftViewModels.Count)
                {
                    return(UnprocessableEntity(new ErrorViewModel
                    {
                        Type = Type.Error, Message = "Could not covert al viewmodels to shifts"
                    }));
                }

                TaskListResult <Shift> result = await shiftService.CreateShiftsAsync(shifts);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }

                List <ShiftViewModel> createdVm = result.Data.Select(ShiftViewModel.CreateVm)
                                                  .ToList();
                return(Ok(createdVm));
            }
            catch (Exception ex)
            {
                string message = GetType()
                                 .Name + "Error in " + nameof(SaveShiftsAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }