Search in sources :

Example 1 with Book

use of org.springside.examples.bootapi.domain.Book in project springside4 by springside.

the class BookBorrowService method markBookReturned.

@Transactional
public void markBookReturned(Long id, Account owner) {
    Book book = bookDao.findOne(id);
    if (!book.status.equals(Book.STATUS_OUT)) {
        logger.error("User return the book not out, user id:" + owner.id + ",book id:" + id + ",status:" + book.status);
        throw new ServiceException("The book is not borrowing", ErrorCode.BOOK_STATUS_WRONG);
    }
    if (!owner.id.equals(book.owner.id)) {
        logger.error("User return the book not himself, user id:" + owner.id + ",book id:" + id + ",owner id" + book.owner.id);
        throw new ServiceException("User can't make others book returned", ErrorCode.BOOK_OWNERSHIP_WRONG);
    }
    book.status = Book.STATUS_IDLE;
    book.borrowDate = null;
    book.borrower = null;
    bookDao.save(book);
    Message message = new Message(book.borrower, String.format("Mark book <%s> returned by %s", book.title, owner.name), ClockUtil.currentDate());
    messageDao.save(message);
}
Also used : ServiceException(org.springside.examples.bootapi.service.exception.ServiceException) Message(org.springside.examples.bootapi.domain.Message) Book(org.springside.examples.bootapi.domain.Book) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Book

use of org.springside.examples.bootapi.domain.Book in project springside4 by springside.

the class BookDaoTest method findByOwnerId.

@Test
public void findByOwnerId() {
    List<Book> books = bookDao.findByOwnerId(1L, new PageRequest(0, 10));
    assertThat(books).hasSize(2);
    assertThat(books.get(0).title).isEqualTo("Big Data日知录");
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Book(org.springside.examples.bootapi.domain.Book) Test(org.junit.Test)

Example 3 with Book

use of org.springside.examples.bootapi.domain.Book in project springside4 by springside.

the class BookDaoTest method findByBorrowerId.

@Test
public void findByBorrowerId() {
    List<Book> books = bookDao.findByBorrowerId(1L, new PageRequest(0, 10));
    assertThat(books).hasSize(0);
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Book(org.springside.examples.bootapi.domain.Book) Test(org.junit.Test)

Example 4 with Book

use of org.springside.examples.bootapi.domain.Book in project springside4 by springside.

the class BookEndpoint method modifyBook.

@RequestMapping(value = "/api/books/{id}/modify", method = RequestMethod.POST, consumes = MediaTypes.JSON_UTF_8)
public void modifyBook(@RequestBody BookDto bookDto, @RequestParam(value = "token", required = false) String token) {
    checkToken(token);
    Account currentUser = accountService.getLoginUser(token);
    Book book = BeanMapper.map(bookDto, Book.class);
    adminService.modifyBook(book, currentUser.id);
}
Also used : Account(org.springside.examples.bootapi.domain.Account) Book(org.springside.examples.bootapi.domain.Book) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Book

use of org.springside.examples.bootapi.domain.Book in project springside4 by springside.

the class BookEndpoint method listMyBorrowedBook.

@RequestMapping(value = "/api/myborrowedbook", produces = MediaTypes.JSON_UTF_8)
public List<BookDto> listMyBorrowedBook(@RequestParam(value = "token", required = false) String token, Pageable pageable) {
    checkToken(token);
    Account currentUser = accountService.getLoginUser(token);
    List<Book> books = borrowService.listMyBorrowedBook(currentUser.id, pageable);
    return BeanMapper.mapList(books, Book.class, BookDto.class);
}
Also used : Account(org.springside.examples.bootapi.domain.Account) Book(org.springside.examples.bootapi.domain.Book) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Book (org.springside.examples.bootapi.domain.Book)17 Transactional (org.springframework.transaction.annotation.Transactional)7 ServiceException (org.springside.examples.bootapi.service.exception.ServiceException)7 Test (org.junit.Test)6 Account (org.springside.examples.bootapi.domain.Account)6 Message (org.springside.examples.bootapi.domain.Message)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 PageRequest (org.springframework.data.domain.PageRequest)2 ErrorResult (org.springside.examples.bootapi.api.support.ErrorResult)1