Ejemplo n.º 1
0
        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
                }));
            }
        }
Ejemplo n.º 2
0
        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
                }));
            }
        }