Skip to content

Commit

Permalink
Merge pull request #9 from f-lab-edu/feature/3
Browse files Browse the repository at this point in the history
코드정리
  • Loading branch information
shine-17 authored Dec 17, 2024
2 parents 5ee5c6f + d139598 commit 7ae80df
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 37 deletions.
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로 역직렬화
}
}
23 changes: 0 additions & 23 deletions src/main/java/com/study/bookcafe/dao/GeneralMemberRepository.java

This file was deleted.

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.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.Borrow;
import com.study.bookcafe.entity.BookEntity;
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.BorrowRepository;
import com.study.bookcafe.repository.BorrowRepository;
import com.study.bookcafe.domain.Borrow;
import org.springframework.stereotype.Service;

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();
}
}

0 comments on commit 7ae80df

Please sign in to comment.