public async Task <ActionResult <TaskViewModel> > RemoveTaskAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                return(BadRequest("No valid id."));
            }
            try
            {
                Task task = (await taskService.GetTaskAsync(id)).Data;
                if (task == null)
                {
                    return(NotFound("Task not found"));
                }
                TaskResult <Task> result = await taskService.RemoveTaskAsync(task);

                return(!result.Succeeded
                    ? UnprocessableEntity(new ErrorViewModel
                {
                    Type = Type.Error, Message = result.Message
                })
                    : Ok(TaskViewModel.CreateVm(result.Data)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(RemoveTaskAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
        public async Task <ActionResult <TaskViewModel> > SaveTaskAsync(TaskViewModel taskViewModel)
        {
            if (taskViewModel == null)
            {
                return(BadRequest("No valid task received"));
            }
            if (string.IsNullOrEmpty(taskViewModel.Name))
            {
                return(BadRequest("Name of a Task cannot be empty"));
            }
            if (taskViewModel.Category == null)
            {
                return(BadRequest("Category of a Task cannot be empty"));
            }

            try
            {
                Task task = TaskViewModel.CreateTask(taskViewModel);
                if (task == null)
                {
                    return(BadRequest("Unable to convert TaskViewModel to Task"));
                }

                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                task.LastEditBy = oid;

                task.Category    = null;
                task.Instruction = null;

                TaskResult <Task> result;
                if (task.Id == Guid.Empty)
                {
                    result = await taskService.CreateTaskAsync(task);
                }
                else
                {
                    return(BadRequest("Cannot update existing Task with post method"));
                }

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                return(Ok(TaskViewModel.CreateVm(result.Data)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(SaveTaskAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
示例#3
0
        public async Task <ActionResult <ShiftViewModel> > UpdateShiftAsync(ShiftViewModel shiftViewModel)
        {
            if (shiftViewModel == null || shiftViewModel.Id == Guid.Empty || shiftViewModel.Project == null ||
                shiftViewModel.Task == null)
            {
                return(BadRequest("No valid Shift received"));
            }
            if (shiftViewModel.Task.Id == Guid.Empty || shiftViewModel.Project.Id == Guid.Empty)
            {
                return(BadRequest("ProjectId and or TaskId cannot be empty"));
            }
            try
            {
                Shift oldShift = (await shiftService.GetShiftAsync(shiftViewModel.Id)).Data;
                if (oldShift == null)
                {
                    return(NotFound("Shift not found"));
                }

                if (oldShift.ProjectId != shiftViewModel.Project.Id)
                {
                    return(BadRequest("Cannot change the project of a shift"));
                }

                if (!oldShift.RowVersion.SequenceEqual(shiftViewModel.RowVersion))
                {
                    return(BadRequest("Outdated entity received"));
                }

                Shift updatedShift = ShiftViewModel.CreateShift(shiftViewModel);
                if (updatedShift == null)
                {
                    return(BadRequest("Unable to convert ShiftViewModel to Shift"));
                }

                Project project = (await projectService.GetProjectDetailsAsync(updatedShift.ProjectId)).Data;
                Task    task    = null;
                if (updatedShift.TaskId != null)
                {
                    task = (await taskService.GetTaskAsync((Guid)updatedShift.TaskId)).Data;
                }

                if (project == null || task == null)
                {
                    return(NotFound("Project and or Task Not found"));
                }

                oldShift.StartTime            = updatedShift.StartTime;
                oldShift.EndTime              = updatedShift.EndTime;
                oldShift.ParticipantsRequired = updatedShift.ParticipantsRequired;
                oldShift.Task    = task;
                oldShift.Project = project;
                //cannot update Project, Id or Date by design
                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                oldShift.LastEditBy = oid;

                TaskResult <Shift> result = await shiftService.UpdateShiftAsync(oldShift);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                result.Data.Task.Requirements = null;
                return(Ok(ShiftViewModel.CreateVm(result.Data)));
            }
            catch (Exception ex)
            {
                string message = GetType()
                                 .Name + "Error in " + nameof(UpdateShiftAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
示例#4
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
                }));
            }
        }
        public async Task <ActionResult <TaskViewModel> > UpdateTaskAsync(TaskViewModel taskViewModel)
        {
            if (taskViewModel == null || taskViewModel.Id == Guid.Empty)
            {
                return(BadRequest("No valid project received"));
            }
            if (string.IsNullOrEmpty(taskViewModel.Name))
            {
                return(BadRequest("Name of a Task cannot be empty"));
            }
            if (taskViewModel.Category == null)
            {
                return(BadRequest("Category of a Task cannot be empty"));
            }
            try
            {
                Task oldTask = (await taskService.GetTaskAsync(taskViewModel.Id)).Data;
                if (oldTask == null)
                {
                    return(NotFound("Task not found"));
                }
                if (!oldTask.RowVersion.SequenceEqual(taskViewModel.RowVersion))
                {
                    return(BadRequest("Outdated entity received"));
                }
                Task updatedTask = TaskViewModel.CreateTask(taskViewModel);
                if (updatedTask == null)
                {
                    return(BadRequest("Unable to convert TaskViewModel to Task"));
                }

                oldTask.Color        = updatedTask.Color;
                oldTask.Description  = updatedTask.Description;
                oldTask.Name         = updatedTask.Name;
                oldTask.ProjectTasks = updatedTask.ProjectTasks;
                if (updatedTask.Instruction != null)
                {
                    oldTask.Instruction =
                        (await documentService.GetDocumentAsync(updatedTask.InstructionId ?? Guid.Empty))
                        .Data;
                    oldTask.InstructionId = oldTask.Instruction.Id;
                }

                if (updatedTask.CategoryId != Guid.Empty && oldTask.CategoryId != updatedTask.CategoryId)
                {
                    oldTask.Category   = (await taskService.GetCategoryAsync(updatedTask.CategoryId ?? Guid.Empty)).Data;
                    oldTask.CategoryId = oldTask.Category.Id;
                }

                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                oldTask.LastEditBy = oid;

                TaskResult <Task> result = await taskService.UpdateTaskAsync(oldTask);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                return(Ok(TaskViewModel.CreateVm(result.Data)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(UpdateTaskAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }