Ejemplo n.º 1
0
        public void CallsTodoWhenTodoCommand()
        {
            TodoCommand todoCommand = new TodoCommand();

            _controllerFixture.Controller.Execute(todoCommand);
            _controllerFixture.Todo.Verify(x => x.Execute(It.IsAny <Core.Boundaries.Todo.Request>()), Times.Once);
        }
Ejemplo n.º 2
0
 private void Validate(TodoCommand cmd)
 {
     if (string.IsNullOrEmpty(cmd.Title))
     {
         throw new TodoException("Cannot perform operation. Title is empty");
     }
 }
Ejemplo n.º 3
0
 private void TrySave(TodoCommand cmd)
 {
     Validate(cmd);
     _repo.TryAdd(new TodoItem()
     {
         Title       = cmd.Title,
         IsCompleted = cmd.IsCompleted
     });
 }
Ejemplo n.º 4
0
 public static TodoResponse CreateSuccessResponse(this TodoCommand cmd)
 {
     return(new TodoResponse()
     {
         OperationUUID = cmd.OperationUUID,
         IsSuccess = true,
         Operation = cmd.Operation
     });
 }
Ejemplo n.º 5
0
 public static TodoResponse CreateErrorResponse(this TodoCommand cmd, string error)
 {
     return(new TodoResponse()
     {
         OperationUUID = cmd.OperationUUID,
         IsSuccess = false,
         Operation = cmd.Operation,
         ErrorMessage = error
     });
 }
Ejemplo n.º 6
0
 public static TodoResponse CreateSuccessResponse(this TodoCommand cmd, IEnumerable <ITodoItem> list)
 {
     return(new TodoResponse()
     {
         OperationUUID = cmd.OperationUUID,
         IsSuccess = true,
         Operation = cmd.Operation,
         TaskList = list.Cast <TodoItem>().ToList()
     });
 }
Ejemplo n.º 7
0
        private static async Task ExecuteTodoOperation(TodoCommand cmd)
        {
            PrintDebug($"Sending operation {cmd.Operation} for {cmd.Title}");

            HttpContent content  = new StringContent(JsonConvert.SerializeObject(cmd), Encoding.UTF8, "application/json");
            var         response = await httpClient.PostAsync(server_url, content);

            var bodyOfResponse = await response.Content.ReadAsStringAsync();

            PrintDebug(bodyOfResponse);

            var todoResponse = JsonConvert.DeserializeObject <TodoResponse>(bodyOfResponse);

            todoResponse.WriteToConsole();
        }
Ejemplo n.º 8
0
        public TodoResponse Handle(TodoCommand command)
        {
            try
            {
                //we can add here some safeguard to protect heavy load on _repo..
                IEnumerable <ITodoItem> response = null;
                switch (command.Operation)
                {
                case EnumOperation.Add:
                    this.TrySave(command); break;

                case EnumOperation.Update:
                    this.RenameTask(command);
                    break;

                case EnumOperation.Delete:
                    this.TryRemove(command);
                    break;

                case EnumOperation.Complete:
                    this.SetCompletedMode(command, true);
                    break;

                case EnumOperation.Undo:
                    this.SetCompletedMode(command, false);
                    break;

                case EnumOperation.List:
                    response = this.GetItems();
                    break;

                case EnumOperation.ListCompleted:
                    response = this.GetItems(true);
                    break;

                default:
                    throw new Exception($"Unhandled operation {command.Operation}");
                }

                return(response == null?command.CreateSuccessResponse() : command.CreateSuccessResponse(response));
            }
            catch (Exception ex)
            {
                _logger.LogError($"TodoProcessor failed for operation {command.OperationUUID} on executing {command.Operation}: {ex.ToString()}");
                return(command.CreateErrorResponse(ex.Message));
            }
        }
Ejemplo n.º 9
0
        private void SetCompletedMode(TodoCommand cmd, bool isCompleted)
        {
            Validate(cmd);
            var existingItem = _repo.Get(p => p.Title == cmd.Title).FirstOrDefault();

            if (existingItem == null)
            {
                throw new TodoException($"item {cmd.Title} is not exist");
            }
            if (existingItem.IsCompleted == isCompleted)
            {
                throw new TodoException($"cannot set completed/undo to {cmd.Title}. It's already in the same state");
            }

            _repo.Update(cmd.Title, new TodoItem()
            {
                Title = cmd.Title, IsCompleted = isCompleted
            });
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> CreateTodo(string todoDescription)
        {
            Devon4NetLogger.Debug("Executing CreateTodo from controller RabbitMqController");

            if (RabbitMqOptions?.Hosts == null || !RabbitMqOptions.Hosts.Any())
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "No RabbitMq instance set up"));
            }

            if (string.IsNullOrEmpty(todoDescription))
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "Please provide a valid description for the TO-DO"));
            }

            var todoCommand = new TodoCommand {
                Description = todoDescription
            };
            var published = await TodoRabbitMqHandler.Publish(todoCommand).ConfigureAwait(false);

            return(Ok(published));
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> Post([FromBody] TodoCommand command)
        {
            try
            {
                var processorResponse = await Task.Run(() => _processor.Handle(command)); //wrapping as Task to utilize async/await pattern

                if (processorResponse.IsSuccess)
                {
                    _logger.LogInformation($"Command {command.OperationUUID} Operation:{command.Operation} completed successfully");
                    return(Ok(processorResponse));
                }
                else
                {
                    _logger.LogError($"Command {command.OperationUUID} Operation:{command.Operation} completed with error: {processorResponse.ErrorMessage}");
                    return(StatusCode(500, processorResponse));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Command {command.OperationUUID} Operation:{command.Operation} completed with error: {ex.ToString()}");
                return(StatusCode(500, command.CreateErrorResponse(ex.Message)));
            }
        }
Ejemplo n.º 12
0
        private void RenameTask(TodoCommand cmd)
        {
            Validate(cmd);
            if (string.IsNullOrEmpty(cmd.NewTitle))
            {
                throw new TodoException("Cannot perform operation. NewTitle is empty");
            }

            var existing = _repo.Get(p => p.Title == cmd.Title).FirstOrDefault();

            if (existing == null)
            {
                throw new TodoException($"item {cmd.Title} is not exist");
            }

            //let's make sure we able to add this task as no one else "catch" same name already
            _repo.TryAdd(new TodoItem()
            {
                Title       = cmd.NewTitle,
                IsCompleted = existing.IsCompleted
            });
            //now let's remove the original task
            TryRemove(cmd);
        }
Ejemplo n.º 13
0
 private void TryRemove(TodoCommand cmd)
 {
     Validate(cmd);
     _repo.Delete(cmd.Title);
 }
Ejemplo n.º 14
0
        public void Execute(TodoCommand todoCommand)
        {
            var request = new Core.Boundaries.Todo.Request(todoCommand.Title);

            todoUseCase.Execute(request);
        }