コード例 #1
0
        public Task <Guid> AddTodoAsync(CreateTodoItemDto input)
        {
            var item = input.ToEntity();

            _todoItems.Add(item);
            return(Task.FromResult(item.Id));
        }
コード例 #2
0
        public async Task <ActionResult <CreateTodoItemDto> > PostTodoItem([FromBody] CreateTodoItemDto input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }
            var insertedId = await _todoAppService.AddTodoAsync(input);

            return(CreatedAtAction(nameof(GetTodoItem), new { id = insertedId }, input));
        }
コード例 #3
0
        public void Create_ShouldCallCreateItem()
        {
            // Arrange
            var input = new CreateTodoItemDto();

            // Act
            controller.Create(input);

            // Assert
            todoItemLogic.Verify(u => u.CreateItem(1, It.Is <CreateTodoItemDto>(c => c == input)));
        }
コード例 #4
0
        public void CreateItem_ShouldMapResultToDto()
        {
            // Arrange
            var input = new CreateTodoItemDto();

            // Act
            logic.CreateItem(1, input);

            // Assert
            mapper.Verify(t => t.Map <TodoItemDto>(It.IsAny <TodoItem>()));
        }
コード例 #5
0
        public void CreateItem_ShouldCallCreateItem()
        {
            // Arrange
            var input = new CreateTodoItemDto();

            // Act
            logic.CreateItem(1, input);

            // Assert
            todoItemRepository.Verify(t => t.CreateItem(1, input));
        }
コード例 #6
0
 public static TodoItem ToEntity(this CreateTodoItemDto dto)
 {
     return(new TodoItem
     {
         Id = Guid.NewGuid(),
         Date = dto.Date,
         Description = dto.Description,
         IsCompleted = false,
         IsDeleted = false,
         Name = dto.Name
     });
 }
コード例 #7
0
        public async Task <Guid> AddTodoAsync(CreateTodoItemDto input)
        {
            var entity = input.ToEntity();

            entity.UserId = GetCurrentUserId();
            await _context.TodoItems
            .AddAsync(entity)
            .ConfigureAwait(false);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(entity.Id);
        }
コード例 #8
0
        public void Create_ShouldReturnBadRequestWhenCreateItemReturnsNull()
        {
            // Arrange
            var input = new CreateTodoItemDto();

            // Act
            var result = controller.Create(input);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
            var response = (result as BadRequestObjectResult).Value as ErrorResponse;

            Assert.Equal("User or list not found in the database.", response.Message);
        }
コード例 #9
0
        public async Task <Result <CreateTodoItemResponseDto> > Execute(CreateTodoItemDto todoItem)
        {
            //TODO: Validation

            var id = DateTime.Now.ToFileTime();
            await _taskRepository.AddTask(new TodoItem
            {
                Id         = id,
                Name       = todoItem.Name,
                IsComplete = false
            });

            return(Result <CreateTodoItemResponseDto> .Success(new CreateTodoItemResponseDto(id)));
        }
コード例 #10
0
        public void Create_ShouldReturnCreatedItem()
        {
            // Arrange
            var input = new CreateTodoItemDto();
            var model = new TodoItemDto();

            todoItemLogic.Setup(u => u.CreateItem(1, It.Is <CreateTodoItemDto>(c => c == input))).Returns(model);

            // Act
            var result = controller.Create(input);

            // Assert
            Assert.IsType <CreatedAtActionResult>(result);
        }
コード例 #11
0
        public void InvalidEnum()
        {
            // Arrange
            var dto = new CreateTodoItemDto
            {
                Title  = "TodoItemTitle",
                Status = (TodoTaskStatus)99
            };

            // Act
            var result = _validator.TestValidate(dto);

            // Assert
            result.IsValid.Should().BeFalse();
        }
コード例 #12
0
        public void Add_ValidObjectPassed_ReturnsCreatedResponse()
        {
            // Arrange
            CreateTodoItemDto testItem = new CreateTodoItemDto()
            {
                Name        = "My Valid Todo Task",
                Description = "This is my valid todo task item!",
                Date        = DateTime.UtcNow.AddDays(1)
            };

            // Act
            var createdResponse = _controller.Post(testItem).Result;

            // Assert
            Assert.IsType <CreatedAtActionResult>(createdResponse.Result);
        }
コード例 #13
0
        public void TitleRange(int length, string message)
        {
            // Arrange
            var dto = new CreateTodoItemDto
            {
                Title  = new string('*', length),
                Status = TodoTaskStatus.Open
            };

            // Act
            var result = _validator.TestValidate(dto);

            // Assert
            result.IsValid.Should().BeFalse();
            result.Errors.FirstOrDefault(x => x.ErrorMessage == message).Should().NotBeNull();
            result.Errors.Should().HaveCount(1);
        }
コード例 #14
0
        public void TitleIsNull()
        {
            // Arrange
            var dto = new CreateTodoItemDto
            {
                Title  = null,
                Status = TodoTaskStatus.Open
            };

            // Act
            var result = _validator.TestValidate(dto);

            // Assert
            result.IsValid.Should().BeFalse();
            result.Errors.Single().ErrorMessage.Should().Be("'Title' must not be empty.");
            result.Errors.Should().HaveCount(1);
        }
コード例 #15
0
        public void CreateItem_ShouldReturnNullIfListIsNotFound()
        {
            // Arrange
            var dbContext  = SetupDatabase(nameof(CreateItem_ShouldReturnNullIfListIsNotFound));
            var repository = new TodoItemRepository(dbContext);
            var input      = new CreateTodoItemDto
            {
                TodoListId  = 10,
                Description = "Cheese"
            };

            // Act
            var result = repository.CreateItem(1, input);

            // Assert
            Assert.Null(result);
        }
コード例 #16
0
        public void Add_InvalidObjectPassed_ReturnsBadRequest()
        {
            // Arrange
            var nameMissingItem = new CreateTodoItemDto()
            {
                Description = "Missing Name of Todo Item",
                Date        = DateTime.Now
            };

            _controller.ModelState.AddModelError("Name", "Required");

            // Act
            var badResponse = _controller.Post(nameMissingItem).Result;

            // Assert
            Assert.IsType <BadRequestObjectResult>(badResponse.Result);
        }
コード例 #17
0
        public void Add_ValidObjectPassed_ReturnedResponseHasCreatedItem()
        {
            // Arrange
            var testItem = new CreateTodoItemDto()
            {
                Name        = "My another valid todo item",
                Description = "This is my valid todo task item!",
                Date        = DateTime.UtcNow.AddDays(1)
            };

            // Act
            var createdResponse       = _controller.Post(testItem).Result;
            var createdResponseResult = createdResponse.Result as CreatedAtActionResult;
            var item = createdResponseResult.Value as CreateTodoItemDto;

            // Assert
            Assert.IsType <CreateTodoItemDto>(item);
            Assert.Equal("My another valid todo item", item.Name);
        }
コード例 #18
0
        public void CreateItem_ShouldCreateItem()
        {
            // Arrange
            var dbContext  = SetupDatabase(nameof(CreateItem_ShouldCreateItem));
            var repository = new TodoItemRepository(dbContext);
            var input      = new CreateTodoItemDto
            {
                TodoListId  = 1,
                Description = "Cheese"
            };

            // Act
            var result = repository.CreateItem(1, input);

            // Assert
            Assert.NotNull(result);
            var item = dbContext.TodoItems.FirstOrDefault(t => t.Description == "Cheese");

            Assert.NotNull(item);
        }
コード例 #19
0
        public async Task <ActionResult <CreateTodoItemDto> > Post([FromBody] CreateTodoItemDto data)
        {
            //js sends min year of 1899 if empty date is parsed for date
            if (data.Date.HasValue)
            {
                var dateComparisonResult = DateTime.Compare(DateTime.Now, data.Date.Value);
                // Today is later than js date..
                if (dateComparisonResult > 0)
                {
                    data.Date = null;
                }
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }
            var insertedId = await _todoAppService.AddTodoAsync(data);

            return(CreatedAtAction(nameof(Get), new { id = insertedId }, data));
        }
コード例 #20
0
        public IActionResult Create(CreateTodoItemDto input)
        {
            var userId = int.Parse(httpContextAccessor.HttpContext.User.FindFirst(Constants.UserIdClaim)?.Value);
            var result = todoItemLogic.CreateItem(userId, input);

            if (result == null)
            {
                return(BadRequest(new ErrorResponse
                {
                    Status = false,
                    Message = "User or list not found in the database."
                }));
            }
            else
            {
                return(CreatedAtAction(nameof(GetItemById), new { result.Id }, new Response <TodoItemDto>
                {
                    Status = true,
                    Model = result
                }));
            }
        }
コード例 #21
0
        /// <summary>
        /// Creates a item.
        /// Returns null if user is not found.
        /// </summary>
        /// <param name="userId">User id</param>
        /// <param name="createDto">todo item to be created</param>
        /// <returns>Todo item</returns>
        public TodoItem CreateItem(int userId, CreateTodoItemDto createDto)
        {
            var user = dbContext.Users.FirstOrDefault(u => u.UserId == userId);

            if (user == null)
            {
                return(null);
            }

            var toBeCreated = new TodoItem
            {
                Description  = createDto.Description,
                user         = user,
                LastModified = DateTime.Now
            };

            var result = dbContext.Add(toBeCreated).Entity;

            dbContext.SaveChanges();

            return(result);
        }
コード例 #22
0
        public TodoItem CreateItem(CreateTodoItemDto createItemDto)
        {
            var userId = CheckAuthentication();

            return(todoItemRepository.CreateItem(userId, createItemDto));
        }
コード例 #23
0
        /// <summary>
        /// Creates a todo item.
        /// </summary>
        /// <param name="userId">User id</param>
        /// <param name="createDto">todo item to be created</param>
        /// <returns>Todo item dto</returns>
        public TodoItemDto CreateItem(int userId, CreateTodoItemDto createDto)
        {
            var dbItem = todoItemRepository.CreateItem(userId, createDto);

            return(mapper.Map <TodoItemDto>(dbItem));
        }
コード例 #24
0
        public async Task <IActionResult> CreateTodo(CreateTodoItemDto todoItem)
        {
            var result = await _createTaskCommand.Execute(todoItem);

            return(OkOrFailure(result, HttpStatusCode.Created));
        }