Skip to content

Commit

Permalink
Merge pull request #11 from PoolC/feat/book
Browse files Browse the repository at this point in the history
feat: 도서 기능을 추가 개발합니다
  • Loading branch information
becooq81 authored Dec 15, 2024
2 parents d29e3c3 + e9f8da4 commit 0a8f61a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public List<BookApiResponse> searchBooks(String query, int page) throws XMLParse
.toString();

String xmlResponse = restTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody();
System.out.println(xmlResponse);


try {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/poolc/api/book/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.poolc.api.book.client.BookClient;
import org.poolc.api.book.domain.BookSortOption;
import org.poolc.api.book.dto.request.CreateBookRequest;
import org.poolc.api.book.dto.request.UpdateBookRequest;
import org.poolc.api.book.service.BookService;
Expand Down Expand Up @@ -33,10 +34,13 @@ public ResponseEntity<?> searchBooks(@RequestParam String query,
}

@GetMapping("/all")
public ResponseEntity<?> getAllBooks(@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page) {
public ResponseEntity<?> getAllBooks(
@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page,
@RequestParam(value = "sort", required = false) BookSortOption sortOption) {
try {
return new ResponseEntity<>(bookService.getAllBooks(page), HttpStatus.OK);
return new ResponseEntity<>(bookService.getAllBooks(page, sortOption), HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand Down Expand Up @@ -85,7 +89,7 @@ public ResponseEntity<?> updateBook(@AuthenticationPrincipal Member member, @Pat
@PostMapping("/{id}/borrow")
public ResponseEntity<?> borrowBook(@AuthenticationPrincipal Member member, @PathVariable Long id) {
try {
bookService.borrow(member, id);
bookService.rent(member, id);
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/org/poolc/api/book/domain/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.poolc.api.member.domain.Member;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.time.LocalDate;

@Entity
Expand All @@ -29,8 +28,11 @@ public class Book extends TimestampEntity {
private Long id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "borrower", referencedColumnName = "UUID")
private Member borrower = null;
@JoinColumn(name = "renter", referencedColumnName = "UUID")
private Member renter = null;

@Column(name="donor")
private String donor;

@Column(name = "title", nullable = false)
private String title;
Expand Down Expand Up @@ -59,8 +61,8 @@ public class Book extends TimestampEntity {
@Column(name = "published_date")
private String publishedDate;

@Column(name = "borrow_date")
private LocalDate borrowDate;
@Column(name = "rent_date")
private LocalDate rentDate;

@Column(name = "status", columnDefinition = "varchar(64) default 'AVAILABLE'")
@Enumerated(EnumType.STRING)
Expand All @@ -69,16 +71,16 @@ public class Book extends TimestampEntity {
protected Book() {
}

public void borrowBook(Member member) {
public void rentBook(Member member) {
this.status = BookStatus.UNAVAILABLE;
this.borrowDate = LocalDate.now();
this.borrower = member;
this.rentDate = LocalDate.now();
this.renter = member;
}

public void returnBook() {
this.status = BookStatus.AVAILABLE;
this.borrowDate = null;
this.borrower = null;
this.rentDate = null;
this.renter = null;
}

public void update(UpdateBookRequest request) {
Expand All @@ -91,6 +93,7 @@ public void update(UpdateBookRequest request) {
if (request.getIsbn() != null) this.isbn = request.getIsbn();
if (request.getPublisher() != null) this.publisher = request.getPublisher();
if (request.getPubdate() != null) this.publishedDate = request.getPubdate();
if (request.getDonor() != null) this.donor = request.getDonor();
}

}
8 changes: 0 additions & 8 deletions src/main/java/org/poolc/api/book/domain/BookBorrower.java

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/org/poolc/api/book/domain/BookSortOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.poolc.api.book.domain;

public enum BookSortOption {

CREATED_AT("createdAt"),
TITLE("title"),
RENT_TIME("rentTime");

private final String value;

BookSortOption(String value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.poolc.api.book.dto.request;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;

Expand All @@ -12,30 +13,39 @@
@AllArgsConstructor
public class CreateBookRequest {

@Length(min = 1, max = 100)
@NotBlank
@Size(max = 30)
private String title;

@Length(min = 1, max = 100)
@NotBlank
@Size(max = 100)
private String author;

@Length(min = 1, max = 100)
@NotBlank
@Size(max = 100)
private String publisher;

@Length(min = 1, max = 100)
@NotBlank
@Size(max = 100)
private String isbn;

@Length(min = 1)
@NotBlank
private String description;

@Length(min = 1, max = 100)
@NotBlank
@Size(max = 100)
private String pubdate;

@Length(min = 1)
@NotBlank
private String image;

@NotNull
private Integer discount;

@Length(min = 1)
@NotBlank
private String link;

@NotBlank
@Size(max = 100)
private String donor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public class UpdateBookRequest {
private String image;
private Integer discount;
private String link;
private String donor;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.poolc.api.book.domain.BookStatus;

import java.time.LocalDate;
import org.poolc.api.member.dto.MemberResponse;

@Getter
@AllArgsConstructor
Expand All @@ -28,6 +29,9 @@ public class BookResponse {
private LocalDate borrowDate;
private BookStatus status;

private MemberResponse borrower;
private String donor;

public static BookResponse of(Book book) {
return BookResponse.builder()
.id(book.getId())
Expand All @@ -40,8 +44,10 @@ public static BookResponse of(Book book) {
.isbn(book.getIsbn())
.publisher(book.getPublisher())
.publishedDate(book.getPublishedDate())
.borrowDate(book.getBorrowDate())
.borrowDate(book.getRentDate())
.status(book.getStatus())
.donor(book.getDonor())
.borrower(book.getRenter() == null ? null : MemberResponse.of(book.getRenter()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
import org.springframework.data.jpa.repository.Query;

public interface BookRepository extends JpaRepository<Book, Long> {
boolean existsByTitleAndAuthor(String title, String author);

Optional<Book> findBookById(Long id);
Page<Book> findAllByOrderByCreatedAtDesc(Pageable pageable);
Page<Book> findAllByOrderByTitleAsc(Pageable pageable);

@Query("SELECT b FROM Book b " +
"ORDER BY CASE WHEN b.rentDate IS NULL THEN 0 ELSE 1 END, " +
"b.rentDate DESC, " +
"b.title ASC")
Page<Book> findAllByOrderByRentDateDescTitleAsc(Pageable pageable);

}
5 changes: 3 additions & 2 deletions src/main/java/org/poolc/api/book/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.poolc.api.book.service;

import org.poolc.api.book.domain.BookSortOption;
import org.poolc.api.book.dto.request.CreateBookRequest;
import org.poolc.api.book.dto.request.UpdateBookRequest;
import org.poolc.api.book.dto.response.BookResponse;
Expand All @@ -9,11 +10,11 @@

public interface BookService {

Page<BookResponse> getAllBooks(int page);
Page<BookResponse> getAllBooks(int page, BookSortOption option);
void createBook(Member member, CreateBookRequest request);
void deleteBook(Member member, Long id) throws Exception;
void updateBook(Member member, Long id, UpdateBookRequest request) throws Exception;
void borrow(Member member, Long id) throws Exception;
void rent(Member member, Long id) throws Exception;
void returnBook(Member member, Long id) throws Exception;
BookResponse getBook(Long id);

Expand Down
29 changes: 22 additions & 7 deletions src/main/java/org/poolc/api/book/service/BookServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.poolc.api.book.domain.Book;
import org.poolc.api.book.domain.BookSortOption;
import org.poolc.api.book.domain.BookStatus;
import org.poolc.api.book.dto.request.CreateBookRequest;
import org.poolc.api.book.dto.request.UpdateBookRequest;
Expand All @@ -22,9 +23,22 @@ public class BookServiceImpl implements BookService {
private static final int PAGE_SIZE = 10;

@Override
public Page<BookResponse> getAllBooks(int page) {
return bookRepository.findAllByOrderByCreatedAtDesc(PageRequest.of(page, PAGE_SIZE))
.map(BookResponse::of);
public Page<BookResponse> getAllBooks(int page, BookSortOption option) {
Page<Book> books;
System.out.println("option: " + option);

if (option == null || option == BookSortOption.TITLE) {
books = bookRepository.findAllByOrderByTitleAsc(PageRequest.of(page, PAGE_SIZE));
} else if (option == BookSortOption.CREATED_AT) {
books = bookRepository.findAllByOrderByCreatedAtDesc(PageRequest.of(page, PAGE_SIZE));
} else if (option == BookSortOption.RENT_TIME) {
books = bookRepository.findAllByOrderByRentDateDescTitleAsc(PageRequest.of(page, PAGE_SIZE));
} else {
throw new IllegalArgumentException("잘못된 정렬 옵션입니다.");
}

return books.map(BookResponse::of);

}

@Override
Expand All @@ -42,7 +56,8 @@ public void createBook(Member member, CreateBookRequest request) {
.isbn(request.getIsbn())
.description(request.getDescription())
.status(BookStatus.AVAILABLE)
.borrowDate(null)
.rentDate(null)
.donor(request.getDonor())
.build();
bookRepository.save(book);
}
Expand All @@ -67,21 +82,21 @@ public void updateBook(Member member, Long id, UpdateBookRequest request) throws

@Override
@Transactional
public void borrow(Member member, Long id) throws Exception {
public void rent(Member member, Long id) throws Exception {
Book book = bookRepository.findById(id)
.orElseThrow(() -> new Exception("책을 찾을 없습니다. id: " + id));
if (book.getStatus() == BookStatus.UNAVAILABLE) {
throw new Exception("대여 중인 책입니다. id: " + id);
}
book.borrowBook(member);
book.rentBook(member);
}

@Override
@Transactional
public void returnBook(Member member, Long id) throws Exception {
Book book = bookRepository.findById(id)
.orElseThrow(() -> new Exception("책을 찾을 없습니다. id: " + id));
if (!book.getBorrower().equals(member)) {
if (!book.getRenter().equals(member)) {
throw new Exception("대여한 사람만 반납할 수 있습니다. id: " + id);
}
if (book.getStatus() == BookStatus.AVAILABLE) {
Expand Down

0 comments on commit 0a8f61a

Please sign in to comment.