Пример #1
0
        public async Task <ActionResult <TaskViewModel> > AddTaskToProjectAsync(ProjectTaskViewModel projectTaskViewModel)
        {
            if (projectTaskViewModel == null)
            {
                return(BadRequest("No valid ProjectTask received"));
            }
            if (projectTaskViewModel.ProjectId == Guid.Empty || projectTaskViewModel.TaskId == Guid.Empty)
            {
                return(BadRequest("Project and/or Task Ids cannot be empty"));
            }

            try
            {
                ProjectTask projectTask = ProjectTaskViewModel.CreateProjectTask(projectTaskViewModel);
                if (projectTask == null)
                {
                    return(BadRequest("Unable to convert ProjectTaskViewModel to ProjectTask"));
                }
                projectTask.Task    = (await taskService.GetTaskAsync(projectTask.TaskId)).Data;
                projectTask.Project = (await projectService.GetProjectDetailsAsync(projectTask.ProjectId)).Data;
                if (projectTask.Task == null || projectTask.Project == null)
                {
                    return(BadRequest("Unable to add project and or task to projecttask"));
                }

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

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

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