diff --git a/src/main/java/com/study/bookcafe/common/JsonHelper.java b/src/main/java/com/study/bookcafe/common/JsonHelper.java index df242e2..ea3b90c 100644 --- a/src/main/java/com/study/bookcafe/common/JsonHelper.java +++ b/src/main/java/com/study/bookcafe/common/JsonHelper.java @@ -5,6 +5,7 @@ import com.google.gson.ToNumberPolicy; import java.time.LocalDate; +import java.time.LocalDateTime; public class JsonHelper { private static Gson gson; @@ -13,6 +14,7 @@ public static Gson getGson() { if(gson == null) { gson = new GsonBuilder() .registerTypeAdapter(LocalDate.class, new LocalDateAdapter()) + .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()) .setPrettyPrinting() .serializeNulls() .disableHtmlEscaping() diff --git a/src/main/java/com/study/bookcafe/common/LocalDateTimeAdapter.java b/src/main/java/com/study/bookcafe/common/LocalDateTimeAdapter.java new file mode 100644 index 0000000..5a43b21 --- /dev/null +++ b/src/main/java/com/study/bookcafe/common/LocalDateTimeAdapter.java @@ -0,0 +1,21 @@ +package com.study.bookcafe.common; + +import com.google.gson.*; + +import java.lang.reflect.Type; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class LocalDateTimeAdapter implements JsonDeserializer, JsonSerializer { + private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; + + @Override + public JsonElement serialize(LocalDateTime date, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(date.format(formatter)); // "yyyy-MM-dd" 형식으로 직렬화 + } + + @Override + public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return LocalDateTime.parse(json.getAsString(), formatter); // JSON 문자열을 LocalDate로 역직렬화 + } +} diff --git a/src/main/java/com/study/bookcafe/controller/MemberController.java b/src/main/java/com/study/bookcafe/controller/MemberController.java index 24df377..c4eb7af 100644 --- a/src/main/java/com/study/bookcafe/controller/MemberController.java +++ b/src/main/java/com/study/bookcafe/controller/MemberController.java @@ -36,8 +36,8 @@ public ResponseEntity member(@PathVariable long id) { @PostMapping("/member/borrow") @ResponseBody public ResponseEntity> borrowBook(@RequestBody RequestBorrowDto requestBorrowDto) { - List borrowList = memberService.borrowBook(requestBorrowDto.getMemberId(), requestBorrowDto.getBookdIdList()); - return ResponseEntity.ok(borrowMapper.toBorrowDtoList(borrowList)); + List borrows = memberService.borrowBook(requestBorrowDto.getMemberId(), requestBorrowDto.getBookdIdList()); + return ResponseEntity.ok(borrowMapper.toBorrowDtoList(borrows)); } } diff --git a/src/main/java/com/study/bookcafe/dao/GeneralMemberRepository.java b/src/main/java/com/study/bookcafe/dao/GeneralMemberRepository.java deleted file mode 100644 index 0f1baec..0000000 --- a/src/main/java/com/study/bookcafe/dao/GeneralMemberRepository.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.study.bookcafe.dao; - -import com.study.bookcafe.domain.Level; -import com.study.bookcafe.domain.Member; -import org.springframework.stereotype.Repository; - -import java.util.HashMap; -import java.util.Map; - -@Repository -public class GeneralMemberRepository implements MemberRepository { - - Map members = new HashMap<>(){{ - put(1L, Member.builder().id(1L).name("슈카").level(Level.BASIC).borrowCount(0).build()); - put(2L, Member.builder().id(2L).name("머스크").level(Level.WORM).borrowCount(3).build()); - put(3L, Member.builder().id(3L).name("트럼프").level(Level.LIBRARIAN).borrowCount(5).build()); - }}; - - @Override - public Member findById(long memberId) { - return members.get(memberId); - } -} diff --git a/src/main/java/com/study/bookcafe/domain/Member.java b/src/main/java/com/study/bookcafe/domain/Member.java index d6e839b..31266cc 100644 --- a/src/main/java/com/study/bookcafe/domain/Member.java +++ b/src/main/java/com/study/bookcafe/domain/Member.java @@ -27,16 +27,16 @@ public boolean canBorrow() { return this.getLevel().isBookBorrowCountLeft(getBorrowCount()); } - public List borrowBook(List bookList) { - List borrowList = new ArrayList<>(); + public List borrowBook(List books) { + List borrows = new ArrayList<>(); // 회원이 대출 가능한 상태 확인 if(!this.canBorrow()) { - return borrowList; + return borrows; } // 대출 가능한 도서만 목록에 담기 - return bookList.stream() + return books.stream() .filter(Book::canBorrow) .map(book -> new Borrow(this, book, LocalDateTime.now())) .toList(); diff --git a/src/main/java/com/study/bookcafe/mapper/MemberMapper.java b/src/main/java/com/study/bookcafe/mapper/MemberMapper.java index 615830a..cda5995 100644 --- a/src/main/java/com/study/bookcafe/mapper/MemberMapper.java +++ b/src/main/java/com/study/bookcafe/mapper/MemberMapper.java @@ -19,7 +19,7 @@ public interface MemberMapper { @Mapping(target = "name", source = "name") @Mapping(target = "level", source = "level") @Mapping(target = "borrowCount", source = "borrowCount") - // Member -> MemberDto + // Member -> MemberDto MemberDto toMemberDto(Member member); @Named("MemberDtoToMember") diff --git a/src/main/java/com/study/bookcafe/dao/BookRepository.java b/src/main/java/com/study/bookcafe/repository/BookRepository.java similarity index 64% rename from src/main/java/com/study/bookcafe/dao/BookRepository.java rename to src/main/java/com/study/bookcafe/repository/BookRepository.java index 6fe945e..7b29044 100644 --- a/src/main/java/com/study/bookcafe/dao/BookRepository.java +++ b/src/main/java/com/study/bookcafe/repository/BookRepository.java @@ -1,4 +1,4 @@ -package com.study.bookcafe.dao; +package com.study.bookcafe.repository; import com.study.bookcafe.domain.Book; @@ -7,5 +7,5 @@ public interface BookRepository { Book findById(long bookId); - List findByIdList(Collection bookIds); + List findByIds(Collection bookIds); } diff --git a/src/main/java/com/study/bookcafe/dao/BorrowRepository.java b/src/main/java/com/study/bookcafe/repository/BorrowRepository.java similarity index 86% rename from src/main/java/com/study/bookcafe/dao/BorrowRepository.java rename to src/main/java/com/study/bookcafe/repository/BorrowRepository.java index 02fa92f..998f3e2 100644 --- a/src/main/java/com/study/bookcafe/dao/BorrowRepository.java +++ b/src/main/java/com/study/bookcafe/repository/BorrowRepository.java @@ -1,4 +1,4 @@ -package com.study.bookcafe.dao; +package com.study.bookcafe.repository; import com.study.bookcafe.domain.Borrow; diff --git a/src/main/java/com/study/bookcafe/repository/GeneralMemberRepository.java b/src/main/java/com/study/bookcafe/repository/GeneralMemberRepository.java new file mode 100644 index 0000000..debf484 --- /dev/null +++ b/src/main/java/com/study/bookcafe/repository/GeneralMemberRepository.java @@ -0,0 +1,32 @@ +package com.study.bookcafe.repository; + +import com.study.bookcafe.domain.Level; +import com.study.bookcafe.domain.Member; +import com.study.bookcafe.entity.MemberEntity; +import com.study.bookcafe.mapper.MemberMapper; +import org.springframework.stereotype.Repository; + +import java.util.HashMap; +import java.util.Map; + +@Repository +public class GeneralMemberRepository implements MemberRepository { + + private final MemberMapper memberMapper; + + public GeneralMemberRepository(MemberMapper memberMapper) { + this.memberMapper = memberMapper; + } + + Map members = new HashMap<>(){{ + put(1L, MemberEntity.builder().id(1L).name("슈카").level(Level.BASIC).borrowCount(0).build()); + put(2L, MemberEntity.builder().id(2L).name("머스크").level(Level.WORM).borrowCount(3).build()); + put(3L, MemberEntity.builder().id(3L).name("트럼프").level(Level.LIBRARIAN).borrowCount(5).build()); + }}; + + @Override + public Member findById(long memberId) { + MemberEntity memberEntity = members.get(memberId); + return memberMapper.toMember(memberEntity); + } +} diff --git a/src/main/java/com/study/bookcafe/dao/MemberRepository.java b/src/main/java/com/study/bookcafe/repository/MemberRepository.java similarity index 75% rename from src/main/java/com/study/bookcafe/dao/MemberRepository.java rename to src/main/java/com/study/bookcafe/repository/MemberRepository.java index ffc1ba3..fb0c345 100644 --- a/src/main/java/com/study/bookcafe/dao/MemberRepository.java +++ b/src/main/java/com/study/bookcafe/repository/MemberRepository.java @@ -1,4 +1,4 @@ -package com.study.bookcafe.dao; +package com.study.bookcafe.repository; import com.study.bookcafe.domain.Member; diff --git a/src/main/java/com/study/bookcafe/dao/TestBookRepository.java b/src/main/java/com/study/bookcafe/repository/TestBookRepository.java similarity index 93% rename from src/main/java/com/study/bookcafe/dao/TestBookRepository.java rename to src/main/java/com/study/bookcafe/repository/TestBookRepository.java index 3f8c7ae..1f9535d 100644 --- a/src/main/java/com/study/bookcafe/dao/TestBookRepository.java +++ b/src/main/java/com/study/bookcafe/repository/TestBookRepository.java @@ -1,4 +1,4 @@ -package com.study.bookcafe.dao; +package com.study.bookcafe.repository; import com.study.bookcafe.domain.Book; import com.study.bookcafe.domain.Inventory; @@ -35,7 +35,7 @@ public Book findById(long bookId) { } @Override - public List findByIdList(Collection bookIds) { + public List findByIds(Collection bookIds) { List bookEntities = bookIds.stream() .filter(id -> books.containsKey(id)) .map(id -> books.get(id)).toList(); diff --git a/src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java b/src/main/java/com/study/bookcafe/repository/TestBorrowRepository.java similarity index 97% rename from src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java rename to src/main/java/com/study/bookcafe/repository/TestBorrowRepository.java index 466fedb..35690df 100644 --- a/src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java +++ b/src/main/java/com/study/bookcafe/repository/TestBorrowRepository.java @@ -1,4 +1,4 @@ -package com.study.bookcafe.dao; +package com.study.bookcafe.repository; import com.study.bookcafe.domain.Borrow; import com.study.bookcafe.entity.BookEntity; diff --git a/src/main/java/com/study/bookcafe/service/BookService.java b/src/main/java/com/study/bookcafe/service/BookService.java index 17b254f..49f8092 100644 --- a/src/main/java/com/study/bookcafe/service/BookService.java +++ b/src/main/java/com/study/bookcafe/service/BookService.java @@ -10,5 +10,5 @@ public interface BookService { Book findById(long bookId); // 도서 목록 조회 (id list) - List findByIdList(Collection bookIds); + List findByIds(Collection bookIds); } diff --git a/src/main/java/com/study/bookcafe/service/BookServiceImpl.java b/src/main/java/com/study/bookcafe/service/BookServiceImpl.java index 5d5d57f..b2283f9 100644 --- a/src/main/java/com/study/bookcafe/service/BookServiceImpl.java +++ b/src/main/java/com/study/bookcafe/service/BookServiceImpl.java @@ -1,6 +1,6 @@ package com.study.bookcafe.service; -import com.study.bookcafe.dao.BookRepository; +import com.study.bookcafe.repository.BookRepository; import com.study.bookcafe.domain.Book; import org.springframework.stereotype.Service; @@ -34,7 +34,7 @@ public Book findById(long bookId) { * @return 도서 목록 */ @Override - public List findByIdList(Collection bookIds) { - return bookRepository.findByIdList(bookIds); + public List findByIds(Collection bookIds) { + return bookRepository.findByIds(bookIds); } } diff --git a/src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java b/src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java index f25de28..cba4285 100644 --- a/src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java +++ b/src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java @@ -1,6 +1,6 @@ package com.study.bookcafe.service; -import com.study.bookcafe.dao.BorrowRepository; +import com.study.bookcafe.repository.BorrowRepository; import com.study.bookcafe.domain.Borrow; import org.springframework.stereotype.Service; diff --git a/src/main/java/com/study/bookcafe/service/MemberServiceImpl.java b/src/main/java/com/study/bookcafe/service/MemberServiceImpl.java index 4e37722..3b7f70a 100644 --- a/src/main/java/com/study/bookcafe/service/MemberServiceImpl.java +++ b/src/main/java/com/study/bookcafe/service/MemberServiceImpl.java @@ -1,6 +1,6 @@ package com.study.bookcafe.service; -import com.study.bookcafe.dao.MemberRepository; +import com.study.bookcafe.repository.MemberRepository; import com.study.bookcafe.domain.Book; import com.study.bookcafe.domain.Borrow; import com.study.bookcafe.domain.Member; @@ -43,7 +43,7 @@ public Member findById(long memberId) { @Override public List borrowBook(long memberId, Collection bookIds) { Member member = findById(memberId); - List books = bookService.findByIdList(bookIds); + List books = bookService.findByIds(bookIds); List borrows = member.borrowBook(books); if(borrows.isEmpty()) return borrows; diff --git a/src/test/java/com/study/bookcafe/book/MapperTest.java b/src/test/java/com/study/bookcafe/book/MapperTest.java index c4aaf70..5ceb18f 100644 --- a/src/test/java/com/study/bookcafe/book/MapperTest.java +++ b/src/test/java/com/study/bookcafe/book/MapperTest.java @@ -14,6 +14,8 @@ import java.sql.Date; import java.time.LocalDate; +import static org.assertj.core.api.Assertions.assertThat; + public class MapperTest { private final BookMapper bookMapper = Mappers.getMapper(BookMapper.class); private final Gson gson = JsonHelper.getGson(); @@ -27,7 +29,7 @@ public void checkMapperBookToBookDto() { .price(35000).inventory(new Inventory(5)).build(); BookDto bookDto = bookMapper.toBookDto(book); - System.out.println(gson.toJson(bookDto)); + assertThat(bookDto).isNotNull(); } @Test @@ -39,7 +41,7 @@ public void checkMapperBookDtoToBook() { .price(35000).inventory(new Inventory(5)).build(); Book book = bookMapper.toBook(bookDto); - System.out.println(gson.toJson(book)); + assertThat(book).isNotNull(); } @Test @@ -51,7 +53,7 @@ public void checkMapperBookToBookEntity() { .price(35000).inventory(new Inventory(5)).build(); BookEntity bookEntity = bookMapper.toBookEntity(book); - System.out.println(gson.toJson(bookEntity)); + assertThat(bookEntity).isNotNull(); } @Test @@ -63,6 +65,6 @@ public void checkMapperBookEntityToBook() { .price(35000).inventory(new Inventory(5)).build(); Book book = bookMapper.toBook(bookEntity); - System.out.println(gson.toJson(book)); + assertThat(book).isNotNull(); } } diff --git a/src/test/java/com/study/bookcafe/borrow/BorrowTest.java b/src/test/java/com/study/bookcafe/borrow/BorrowTest.java index 9c67208..a8df752 100644 --- a/src/test/java/com/study/bookcafe/borrow/BorrowTest.java +++ b/src/test/java/com/study/bookcafe/borrow/BorrowTest.java @@ -1,12 +1,17 @@ package com.study.bookcafe.borrow; +import com.google.gson.Gson; +import com.study.bookcafe.common.JsonHelper; import com.study.bookcafe.domain.Borrow; +import com.study.bookcafe.entity.BorrowEntity; +import com.study.bookcafe.mapper.BorrowMapper; import com.study.bookcafe.service.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.time.LocalDateTime; import java.util.List; import static org.assertj.core.api.Assertions.*; @@ -16,6 +21,10 @@ public class BorrowTest { @Autowired private MemberService memberService; + @Autowired + private BorrowMapper borrowMapper; + + private final Gson gson = JsonHelper.getGson(); @Test @DisplayName("회원이 도서를 대출한다.") @@ -24,7 +33,6 @@ public void testBorrowBook() { List borrows1 = memberService.borrowBook(1, List.of(1L)); List borrows2 = memberService.borrowBook(3, List.of(2L)); -// assertThat(Borrow.successBorrow(borrow1)).isTrue(); borrows1.forEach(borrow -> assertThat(Borrow.successBorrow(borrow)).isEqualTo(true)); borrows2.forEach(borrow -> assertThat(Borrow.successBorrow(borrow)).isEqualTo(true)); diff --git a/src/test/java/com/study/bookcafe/member/MapperTest.java b/src/test/java/com/study/bookcafe/member/MapperTest.java index b5195ac..eec647b 100644 --- a/src/test/java/com/study/bookcafe/member/MapperTest.java +++ b/src/test/java/com/study/bookcafe/member/MapperTest.java @@ -11,6 +11,8 @@ import org.junit.jupiter.api.Test; import org.mapstruct.factory.Mappers; +import static org.assertj.core.api.Assertions.assertThat; + public class MapperTest { private final MemberMapper memberMapper = Mappers.getMapper(MemberMapper.class); private final Gson gson = JsonHelper.getGson(); @@ -21,7 +23,7 @@ public void checkMapperMemberToMemberDto() { MemberDto memberDto = MemberDto.builder().id(2).name("슈카").level(Level.WORM).borrowCount(5).build(); Member member = memberMapper.toMember(memberDto); - System.out.println(gson.toJson(member)); + assertThat(member).isNotNull(); } @Test @@ -30,7 +32,7 @@ public void checkMapperMemberDtoToMember() { Member member = Member.builder().id(1).name("김도훈").level(Level.BASIC).borrowCount(2).build(); MemberDto memberDto = memberMapper.toMemberDto(member); - System.out.println(gson.toJson(memberDto)); + assertThat(memberDto).isNotNull(); } @Test @@ -39,7 +41,7 @@ public void checkMapperMemberToMemberEntity() { Member member = Member.builder().id(1).name("트럼프").level(Level.LIBRARIAN).borrowCount(7).build(); MemberEntity memberEntity = memberMapper.toMemberEntity(member); - System.out.println(gson.toJson(memberEntity)); + assertThat(memberEntity).isNotNull(); } @Test @@ -48,6 +50,6 @@ public void checkMapperMemberEntityToMember() { MemberEntity memberEntity = MemberEntity.builder().id(3).name("머스크").level(Level.WORM).borrowCount(2).build(); Member member = memberMapper.toMember(memberEntity); - System.out.println(gson.toJson(member)); + assertThat(member).isNotNull(); } }