示例#1
0
        public async Task <IActionResult> PostBook(BookForDetailedDto book)
        {
            if (book == null)
            {
                return(StatusCode(400));
            }

            var bookToSave = _mapper.Map <Book>(book);

            if (!await _repo.AuthorExist(bookToSave.AuthorId))
            {
                bookToSave.AuthorId = null;
            }

            if (book.Id == 0)
            {
                _repo.AddBook(bookToSave);
            }
            else
            {
                _repo.EditBook(bookToSave);
            }

            await _repo.SaveAll();

            return(StatusCode(201, book));
        }
示例#2
0
        public async Task PostBook_PassNull_StatusCode400()
        {
            //Arrange
            var config = new MapperConfiguration(cfg => {
                cfg.AddProfile(new AutoMapperProfiles());
            });
            var repository          = new Mock <ILibraryRepository>();
            var mapper              = config.CreateMapper();
            BookForDetailedDto book = null;
            var controller          = new BooksController(repository.Object, mapper);

            //Act
            var result = await controller.PostBook(book);

            var statusCode = result as StatusCodeResult;

            //Assert
            Assert.NotNull(statusCode);
            Assert.Equal(400, statusCode.StatusCode);
        }
示例#3
0
        public async Task PostBook_PassBookForDetailedDtoObject_StatusCode201()
        {
            //Arrange
            var config = new MapperConfiguration(cfg => {
                cfg.AddProfile(new AutoMapperProfiles());
            });
            var repository          = new Mock <ILibraryRepository>();
            var mapper              = config.CreateMapper();
            BookForDetailedDto book = new BookForDetailedDto()
            {
                Id = 1, Title = "Test", Description = "Testing"
            };
            var controller = new BooksController(repository.Object, mapper);

            //Act
            var result = await controller.PostBook(book);

            var objectResult = result as ObjectResult;

            //Assert
            Assert.NotNull(objectResult);
            Assert.Equal(201, objectResult.StatusCode);
        }