Skip to content

Commit

Permalink
Merge pull request #110 from dnd-side-project/refactor/#109
Browse files Browse the repository at this point in the history
Refactor Word Dict Response Value
  • Loading branch information
miraexhoi authored Sep 4, 2024
2 parents cf0f73d + af6d849 commit 1075dac
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.dnd.spaced.domain.word.application.dto.request.WordConditionInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.DetailWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.InputWordCandidateDto;
import com.dnd.spaced.domain.word.application.dto.response.MultipleWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.ListWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.PopularWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.WordSearchInfoDto;
import com.dnd.spaced.domain.word.application.event.dto.request.FoundWordPopularViewCountEvent;
Expand Down Expand Up @@ -42,7 +42,7 @@ public class WordService {
private final PopularWordRepository popularWordRepository;
private final ApplicationEventPublisher eventPublisher;

public List<MultipleWordInfoDto> findAllBy(WordConditionInfoDto dto) {
public List<ListWordInfoDto> findAllBy(WordConditionInfoDto dto) {
WordConditionDto wordConditionDto = WordRepositoryMapper.to(
dto.categoryName(),
dto.lastWordName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.dnd.spaced.domain.word.application.dto.request.WordConditionInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.DetailWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.InputWordCandidateDto;
import com.dnd.spaced.domain.word.application.dto.response.MultipleWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.ListWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.PopularWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.WordSearchInfoDto;
import com.dnd.spaced.domain.word.domain.Word;
Expand All @@ -27,9 +27,9 @@ public static WordConditionInfoDto to(
return new WordConditionInfoDto(categoryName, lastWordName, pageable);
}

public static List<MultipleWordInfoDto> to(List<Word> words) {
public static List<ListWordInfoDto> to(List<Word> words) {
return words.stream()
.map(MultipleWordInfoDto::from)
.map(ListWordInfoDto::from)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.dnd.spaced.domain.word.application.dto.response;

import com.dnd.spaced.domain.word.domain.Word;

import java.time.LocalDateTime;

public record ListWordInfoDto(
Long id,
String name,
String meaning,
String category,
int commentCount,
int viewCount,
String example,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {

public static ListWordInfoDto from(Word word) {
return new ListWordInfoDto(
word.getId(),
word.getName(),
word.getMeaning(),
word.getCategory().getName(),
word.getCommentCount(),
word.getViewCount(),
word.getExample(),
word.getCreatedAt(),
word.getUpdatedAt()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.dnd.spaced.domain.word.presentation.dto.response.DetailWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.InputWordCandidateResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.MultipleSearchWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.MultipleWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.ListWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.PopularWordResponse;
import com.dnd.spaced.global.docs.annotation.ExceptionSpec;
import com.dnd.spaced.global.docs.annotation.ExcludeCommonHeaderSpec;
Expand Down Expand Up @@ -39,7 +39,7 @@ public interface SwaggerWordController {
@Parameter(name = "sortBy", description = "정렬 기준(사전순 고정)", schema = @Schema(defaultValue = "name", allowableValues = "name", requiredMode = RequiredMode.NOT_REQUIRED))
})
@ApiResponse(responseCode = "200", description = "OK")
ResponseEntity<MultipleWordInfoResponse> findAllBy(
ResponseEntity<ListWordInfoResponse> findAllBy(
@Parameter(hidden = true) MultipleWordConditionRequest request,
@Parameter(hidden = true) @WordSortCondition Pageable pageable
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.dnd.spaced.domain.word.application.dto.WordServiceMapper;
import com.dnd.spaced.domain.word.application.dto.response.DetailWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.InputWordCandidateDto;
import com.dnd.spaced.domain.word.application.dto.response.MultipleWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.ListWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.PopularWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.WordSearchInfoDto;
import com.dnd.spaced.domain.word.presentation.dto.WordControllerMapper;
Expand All @@ -13,7 +13,7 @@
import com.dnd.spaced.domain.word.presentation.dto.response.DetailWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.InputWordCandidateResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.MultipleSearchWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.MultipleWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.ListWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.PopularWordResponse;
import com.dnd.spaced.global.resolver.auth.AuthAccount;
import com.dnd.spaced.global.resolver.auth.AuthAccountInfo;
Expand All @@ -37,19 +37,19 @@ public class WordController implements SwaggerWordController {
private final WordService wordService;

@GetMapping
public ResponseEntity<MultipleWordInfoResponse> findAllBy(
public ResponseEntity<ListWordInfoResponse> findAllBy(
MultipleWordConditionRequest request,
@WordSortCondition Pageable pageable
) {
List<MultipleWordInfoDto> result = wordService.findAllBy(
List<ListWordInfoDto> result = wordService.findAllBy(
WordServiceMapper.to(
request.category(),
request.lastWordName(),
pageable
)
);

return ResponseEntity.ok(WordControllerMapper.to(result));
return ResponseEntity.ok(WordControllerMapper.too(result));
}

@GetMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.dnd.spaced.domain.word.application.dto.response.DetailWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.InputWordCandidateDto;
import com.dnd.spaced.domain.word.application.dto.response.MultipleWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.ListWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.PopularWordInfoDto;
import com.dnd.spaced.domain.word.application.dto.response.WordSearchInfoDto;
import com.dnd.spaced.domain.word.presentation.dto.response.DetailWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.InputWordCandidateResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.MultipleSearchWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.ListWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.MultipleWordInfoResponse;
import com.dnd.spaced.domain.word.presentation.dto.response.PopularWordResponse;
import java.util.List;
Expand All @@ -21,6 +23,10 @@ public static MultipleWordInfoResponse to(List<MultipleWordInfoDto> dtos) {
return MultipleWordInfoResponse.from(dtos);
}

public static ListWordInfoResponse too(List<ListWordInfoDto> dtos) {
return ListWordInfoResponse.from(dtos);
}

public static MultipleSearchWordInfoResponse toResponse(List<WordSearchInfoDto> dtos) {
return MultipleSearchWordInfoResponse.from(dtos);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.dnd.spaced.domain.word.presentation.dto.response;

import com.dnd.spaced.domain.word.application.dto.response.ListWordInfoDto;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Collections;
import java.util.List;

public record ListWordInfoResponse(

@Schema(description = "용어 정보")
List<WordInfoResponse> words,

@Schema(description = "마지막으로 조회한 용어 이름", nullable = true)
String lastWordName
) {

public static ListWordInfoResponse from(List<ListWordInfoDto> dtos) {
if (dtos == null || dtos.isEmpty()) {
return new ListWordInfoResponse(Collections.emptyList(), null);
}

List<WordInfoResponse> words = dtos.stream()
.map(WordInfoResponse::from)
.toList();

return new ListWordInfoResponse(words, words.get(words.size() - 1).name());
}

private record WordInfoResponse(
@Schema(description = "용어 ID")
Long id,

@Schema(description = "용어 이름")
String name,

@Schema(description = "용어 뜻")
String meaning,

@Schema(description = "용어 카테고리", allowableValues = {"개발", "비즈니스", "디자인"})
String category,

@Schema(description = "댓글 개수")
int commentCount,

@Schema(description = "조회수")
int viewCount
) {

public static WordInfoResponse from(ListWordInfoDto dto) {
return new WordInfoResponse(
dto.id(),
dto.name(),
dto.meaning(),
dto.category(),
dto.commentCount(),
dto.viewCount()
);
}
}
}

0 comments on commit 1075dac

Please sign in to comment.