示例#1
0
        public async Task <JsonResult> AddNewSharedTodo(SharedTodoVM sharedTodoData)
        {
            var errorMessages = sharedTodoData.VerifySharedTodoData();

            if (errorMessages.Count != 0)
            {
                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Data = errorMessages
                }));
            }

            var hasCollaboration = await _collaborationService.DoesUserHasThisCollaborator(sharedTodoData.Todo.UserId, sharedTodoData.SharedToId);

            switch (hasCollaboration)
            {
            case null:
                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Message = "An issue happened while getting data."
                }));

            case < 1:
                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Message = "Collaborator not found."
                }));
            }

            sharedTodoData.Todo.IsShared = true;
            await _todoService.StartTransaction();

            var saveTodoResult = await _todoService.InsertNewTodo(sharedTodoData.Todo);

            if (!saveTodoResult.HasValue || saveTodoResult.Value < 1)
            {
                await _todoService.RevertTransaction();

                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Message = "An issue happened while updating data."
                }));
            }

            var collaboratorTask = new CollaboratorTask {
                CollaborationId = hasCollaboration.Value,
                TaskId          = saveTodoResult.Value,
                TaskType        = nameof(Todo),
                Message         = sharedTodoData.Message,
                AssignedOn      = DateTime.UtcNow,
                Permission      = (byte)sharedTodoData.Permission
            };

            var saveTaskResult = await _collaborationService.InsertNewCollaboratorTask(collaboratorTask);

            if (!saveTaskResult.HasValue || saveTaskResult.Value < 1)
            {
                await _todoService.RevertTransaction();

                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Message = "An issue happened while updating data."
                }));
            }

            var todoOwner = await _userService.GetUserById(sharedTodoData.Todo.UserId);

            var isNotified = await SendNotification(
                todoOwner,
                sharedTodoData.SharedToId,
                new UserNotification {
                Message = $"{ TokenNotifierName } has just assigned a Todo to you. Please tap to see the item.",
                Title   = "Task Assigned"
            },
                _userService
                );

            if (!isNotified)
            {
                await _todoService.RevertTransaction();

                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Message = "An issue happened while updating data."
                }));
            }

            await _todoService.CommitTransaction();

            return(new JsonResult(new JsonResponse {
                Result = SharedEnums.RequestResults.Success, Data = saveTodoResult.Value
            }));
        }