Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

코드정리 #9

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/study/bookcafe/common/JsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.ToNumberPolicy;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class JsonHelper {
private static Gson gson;
Expand All @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/study/bookcafe/common/LocalDateTimeAdapter.java
Original file line number Diff line number Diff line change
@@ -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<LocalDateTime>, JsonSerializer<LocalDateTime> {
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로 역직렬화
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public ResponseEntity<MemberDto> member(@PathVariable long id) {
@PostMapping("/member/borrow")
@ResponseBody
public ResponseEntity<List<BorrowDto>> borrowBook(@RequestBody RequestBorrowDto requestBorrowDto) {
List<Borrow> borrowList = memberService.borrowBook(requestBorrowDto.getMemberId(), requestBorrowDto.getBookdIdList());
return ResponseEntity.ok(borrowMapper.toBorrowDtoList(borrowList));
List<Borrow> borrows = memberService.borrowBook(requestBorrowDto.getMemberId(), requestBorrowDto.getBookdIdList());
return ResponseEntity.ok(borrowMapper.toBorrowDtoList(borrows));
}

}
23 changes: 0 additions & 23 deletions src/main/java/com/study/bookcafe/dao/GeneralMemberRepository.java

This file was deleted.

8 changes: 4 additions & 4 deletions src/main/java/com/study/bookcafe/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public boolean canBorrow() {
return this.getLevel().isBookBorrowCountLeft(getBorrowCount());
}

public List<Borrow> borrowBook(List<Book> bookList) {
List<Borrow> borrowList = new ArrayList<>();
public List<Borrow> borrowBook(List<Book> books) {
List<Borrow> 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();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/study/bookcafe/mapper/MemberMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.study.bookcafe.dao;
package com.study.bookcafe.repository;

import com.study.bookcafe.domain.Book;

Expand All @@ -7,5 +7,5 @@

public interface BookRepository {
Book findById(long bookId);
List<Book> findByIdList(Collection<Long> bookIds);
List<Book> findByIds(Collection<Long> bookIds);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.study.bookcafe.dao;
package com.study.bookcafe.repository;

import com.study.bookcafe.domain.Borrow;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long, MemberEntity> 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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.study.bookcafe.dao;
package com.study.bookcafe.repository;

import com.study.bookcafe.domain.Member;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,7 +35,7 @@ public Book findById(long bookId) {
}

@Override
public List<Book> findByIdList(Collection<Long> bookIds) {
public List<Book> findByIds(Collection<Long> bookIds) {
List<BookEntity> bookEntities = bookIds.stream()
.filter(id -> books.containsKey(id))
.map(id -> books.get(id)).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/study/bookcafe/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface BookService {
Book findById(long bookId);

// 도서 목록 조회 (id list)
List<Book> findByIdList(Collection<Long> bookIds);
List<Book> findByIds(Collection<Long> bookIds);
}
6 changes: 3 additions & 3 deletions src/main/java/com/study/bookcafe/service/BookServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -34,7 +34,7 @@ public Book findById(long bookId) {
* @return 도서 목록
*/
@Override
public List<Book> findByIdList(Collection<Long> bookIds) {
return bookRepository.findByIdList(bookIds);
public List<Book> findByIds(Collection<Long> bookIds) {
return bookRepository.findByIds(bookIds);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,7 +43,7 @@ public Member findById(long memberId) {
@Override
public List<Borrow> borrowBook(long memberId, Collection<Long> bookIds) {
Member member = findById(memberId);
List<Book> books = bookService.findByIdList(bookIds);
List<Book> books = bookService.findByIds(bookIds);
List<Borrow> borrows = member.borrowBook(books);

if(borrows.isEmpty()) return borrows;
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/com/study/bookcafe/book/MapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
}
}
10 changes: 9 additions & 1 deletion src/test/java/com/study/bookcafe/borrow/BorrowTest.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -16,6 +21,10 @@ public class BorrowTest {

@Autowired
private MemberService memberService;
@Autowired
private BorrowMapper borrowMapper;

private final Gson gson = JsonHelper.getGson();

@Test
@DisplayName("회원이 도서를 대출한다.")
Expand All @@ -24,7 +33,6 @@ public void testBorrowBook() {
List<Borrow> borrows1 = memberService.borrowBook(1, List.of(1L));
List<Borrow> 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));

Expand Down
10 changes: 6 additions & 4 deletions src/test/java/com/study/bookcafe/member/MapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
}
}
Loading