Skip to content

Commit

Permalink
refactor : 캐릭터 도감 조회 관련 domain 모듈과 api 모듈의 책임 할당 문제 리팩토링 (#104)
Browse files Browse the repository at this point in the history
* refactor : CharacterBook 애그리거트 삭제

* refactor : database 모듈 character 변경

* refactor : package 명view -> domain 으로 변경

* refactor : CharacterBook 애그리거트 제거 및 CharacterBookView 일급컬렉션 도입

* docs : docs 업데이트

* refactor : CharacterBookReadController 변경에 따른 리팩토링
  • Loading branch information
oownahcohc authored Aug 27, 2024
1 parent ae583b1 commit f356edd
Show file tree
Hide file tree
Showing 33 changed files with 616 additions and 585 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package univ.earthbreaker.namu.core.api.character;

import java.util.Arrays;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -8,30 +11,37 @@

import univ.earthbreaker.namu.core.api.auth.support.AuthMapping;
import univ.earthbreaker.namu.core.api.auth.support.LoginMember;
import univ.earthbreaker.namu.core.domain.character.CharacterType;
import univ.earthbreaker.namu.core.domain.character.book.CharacterBookDetailReadService;
import univ.earthbreaker.namu.core.domain.character.book.MemberCharacter;
import univ.earthbreaker.namu.core.domain.character.book.BookResult;
import univ.earthbreaker.namu.core.domain.character.book.CharacterBookReadService;
import univ.earthbreaker.namu.core.domain.character.book.MemberCharacterFinder;

@RestController
@RequestMapping("/v1/characters/books")
public class CharacterBookReadController {

private final CharacterBookReadService characterBookReadService;
private final MemberCharacterFinder memberCharacterFinder;
private final CharacterBookDetailReadService characterBookDetailReadService;

public CharacterBookReadController(
CharacterBookReadService characterBookReadService,
MemberCharacterFinder memberCharacterFinder,
CharacterBookDetailReadService characterBookDetailReadService
) {
this.characterBookReadService = characterBookReadService;
this.memberCharacterFinder = memberCharacterFinder;
this.characterBookDetailReadService = characterBookDetailReadService;
}

@AuthMapping
@GetMapping("/all")
public ResponseEntity<BookResult> readAll(@LoginMember Long memberNo) {
return ResponseEntity.ok(characterBookReadService.readAll(memberNo));
public ResponseEntity<CharacterBookResponse> readAll(@LoginMember Long memberNo) {
CharacterBookView bookView = new CharacterBookView(memberCharacterFinder.findAllBy(memberNo));
List<CharacterBookResponse.BookSectionResponse> bookSectionResponses = Arrays.stream(CharacterType.values())
.map(type -> CharacterBookViewReader.readByType(bookView, type))
.toList();
return ResponseEntity.ok(new CharacterBookResponse(
bookView.readTotalAcquiredCharacterCount(),
bookSectionResponses
));
}

@AuthMapping
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package univ.earthbreaker.namu.core.api.character;

import java.util.List;

import org.jetbrains.annotations.NotNull;

import univ.earthbreaker.namu.core.domain.character.CharacterType;
import univ.earthbreaker.namu.core.domain.common.Constant;

public record CharacterBookResponse(
int totalAcquiredCount,
List<BookSectionResponse> sectionResponses
) {
record BookSectionResponse(
CharacterType type,
int totalCountOfType,
int acquiredCount,
List<ProfileResponse> profileResponses
) {
}

record ProfileResponse(
long characterNo,
String thumbnailImageUrl,
boolean isAcquired
) {
static @NotNull ProfileResponse of(long characterNo, String thumbnailImagePath, boolean isAcquired) {
return new ProfileResponse(
characterNo,
Constant.IMAGE_ACCESS_URL + thumbnailImagePath,
isAcquired
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package univ.earthbreaker.namu.core.api.character;

import java.util.List;

import univ.earthbreaker.namu.core.domain.character.CharacterType;
import univ.earthbreaker.namu.core.domain.character.book.MemberCharacter;

public class CharacterBookView {

private final List<MemberCharacter> values;

public CharacterBookView(List<MemberCharacter> values) {
this.values = values;
}

int readTotalAcquiredCharacterCount() {
return values.stream()
.mapToInt(MemberCharacter::getCount)
.sum();
}

int readAcquiredCharacterCountByType(CharacterType type) {
return (int)values.stream()
.filter(memberCharacter -> memberCharacter.isSameWithCharacterType(type) && memberCharacter.isAcquired())
.count();
}

int readTotalCountOfType(CharacterType type) {
return (int)values.stream()
.filter(memberCharacter -> memberCharacter.isSameWithCharacterType(type))
.count();
}

List<CharacterBookResponse.ProfileResponse> readCharactersByType(CharacterType type) {
return values.stream()
.filter(memberCharacter -> memberCharacter.isSameWithCharacterType(type))
.map(memberCharacter -> CharacterBookResponse.ProfileResponse.of(
memberCharacter.getCharacterNo(),
memberCharacter.getThumbnailImagePath(),
memberCharacter.isAcquired()
))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package univ.earthbreaker.namu.core.api.character;

import univ.earthbreaker.namu.core.domain.character.CharacterType;

public class CharacterBookViewReader {

private CharacterBookViewReader() {
}

static CharacterBookResponse.BookSectionResponse readByType(CharacterBookView book, CharacterType type) {
return new CharacterBookResponse.BookSectionResponse(
type,
book.readTotalCountOfType(type),
book.readAcquiredCharacterCountByType(type),
book.readCharactersByType(type)
);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit f356edd

Please sign in to comment.