Example #1
0
        public async Task <IActionResult> Post([FromBody] BookLendingRequestDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            //Checking the book if it is available to Lend
            var bookDetail = await _bookService.GetByIdAsync(model.BookId);

            if (bookDetail == null)
            {
                return(BadRequest());
            }
            if (!bookDetail.Available2Lend)
            {
                return(NoContent());
            }

            // Lending a book
            var result = await _bookLendingService.BookLending(model);

            if (result != null)
            {
                // Update the field Available2Lend to False in Book
                _bookService.UpdateAvailable2Lend(bookDetail.Id, false);

                return(Created(nameof(Post), result));
            }
            else
            {
                return(NoContent());
            }
        }
Example #2
0
        public async Task BorrowABook_BookExistFailedToBorrow_NoContent()
        {
            // Arrange
            var bookId             = 1;
            var userId             = "2";
            var bookLendingRequest = new BookLendingRequestDto
            {
                BookId = bookId,
                Id     = 1,
                UserId = userId
            };

            var bookInfo = new BookResponseDto
            {
                Id             = bookId,
                Name           = "Book 1",
                PublishedDate  = new DateTime(2015, 10, 1),
                Available2Lend = true
            };

            _bookServiceMock.Setup(c => c.GetByIdAsync(bookLendingRequest.BookId)).ReturnsAsync(bookInfo);
            _bookLendingServiceMock.Setup(c => c.BookLending(bookLendingRequest)).ReturnsAsync((BookLendingResponseDto)null);
            _bookLendingApiController = new BookLendingApiController(_bookLendingServiceMock.Object, _bookServiceMock.Object);
            // Act
            var result = await _bookLendingApiController.Post(bookLendingRequest);

            // Assert
            var resultStatus = ((Microsoft.AspNetCore.Mvc.NoContentResult)result).StatusCode;

            Assert.Equal((int)HttpStatusCode.NoContent, resultStatus);
        }
Example #3
0
        public async Task <BookLendingResponseDto> BookLending(BookLendingRequestDto bookLending)
        {
            var bookLendingEntityRequest = MapToRequest(bookLending);
            // Create record in book_lending
            var bookLendingEntityResponse = await _bookLendingRepository.AddAsync(bookLendingEntityRequest);

            // Update the field Available2Lend to False
            var insertedBookLending = await _bookLendingRepository.FindByIdAsync(bookLendingEntityResponse.Id, IncludeProperties);

            return(MapToResponse(insertedBookLending));
        }
Example #4
0
 private Model.BookLending MapToRequest(BookLendingRequestDto request)
 {
     if (request != null)
     {
         return(new Model.BookLending
         {
             BookId = request.BookId,
             UserId = request.UserId,
             BorrowDate = DateTime.UtcNow,
             ReturnDate = null,
             Id = request.Id
         });
     }
     return(null);
 }
Example #5
0
        public async Task <IActionResult> Put([FromRoute] int id, [FromBody] BookLendingRequestDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var result = await _bookLendingService.BookReturning(id, model);

            if (result)
            {
                return(Accepted(result));
            }
            else
            {
                return(NoContent());
            }
        }
Example #6
0
        public async Task BorrowABook_BookNotExist_BadRequest()
        {
            // Arrange
            var bookLendingRequest = new BookLendingRequestDto
            {
                BookId = 1,
                Id     = 1,
                UserId = "2"
            };

            _bookServiceMock.Setup(c => c.GetByIdAsync(bookLendingRequest.BookId)).ReturnsAsync((BookResponseDto)null);
            _bookLendingApiController = new BookLendingApiController(_bookLendingServiceMock.Object, _bookServiceMock.Object);
            // Act
            var result = await _bookLendingApiController.Post(bookLendingRequest);

            // Assert
            var resultStatus = ((Microsoft.AspNetCore.Mvc.BadRequestResult)result).StatusCode;

            Assert.Equal((int)HttpStatusCode.BadRequest, resultStatus);
        }
Example #7
0
        public async Task ReturnBook_BookLendingId_NoContent()
        {
            // Arrange
            var returnResult       = false;
            var bookLendingId      = 1;
            var bookLendingRequest = new BookLendingRequestDto
            {
                BookId = 1,
                Id     = bookLendingId,
                UserId = "2"
            };

            _bookLendingServiceMock.Setup(c => c.BookReturning(bookLendingId, bookLendingRequest)).ReturnsAsync(returnResult);
            _bookLendingApiController = new BookLendingApiController(_bookLendingServiceMock.Object, _bookServiceMock.Object);
            // Act
            var result = await _bookLendingApiController.Put(bookLendingId, bookLendingRequest);

            // Assert
            var resultStatus = ((Microsoft.AspNetCore.Mvc.NoContentResult)result).StatusCode;

            Assert.Equal((int)HttpStatusCode.NoContent, resultStatus);
        }
Example #8
0
        public async Task <bool> BookReturning(int id, BookLendingRequestDto bookLendingRes)
        {
            var bookLendingToReturn = await _GetByIdAsync(id);

            // Could not find the bookLending record
            // OR the book has been returned
            // OR the bookToReturn does not belong to the user
            // OR the book has Available2Lend is TRUE
            if (bookLendingToReturn == null ||
                bookLendingToReturn.ReturnDate != null ||
                bookLendingToReturn.UserId != bookLendingRes.UserId ||
                bookLendingToReturn.Book.Available2Lend)
            {
                return(false);
            }

            // Update the ReturnDate and the Available to lend to TRUE
            bookLendingToReturn.ReturnDate          = DateTime.UtcNow;
            bookLendingToReturn.Book.Available2Lend = true;

            _bookLendingRepository.Update(bookLendingToReturn);

            return(true);
        }