-
Notifications
You must be signed in to change notification settings - Fork 0
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
[BE] feat: 전체 태그 목록 조회 기능 추가 #161
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e1a0019
refactor: TagDto 패키지 변경
Go-Jaecheol 58ef81c
feat: TagController 인터페이스 및 전체 태그 목록 조회 메서드 추가
Go-Jaecheol 59449c3
feat: TagType enum 추가
Go-Jaecheol 27cc3be
feat: 전체 태그 목록을 태그 타입 별로 조회하는 기능 구현
Go-Jaecheol 18a5e1c
test: 태그 타입으로 태그들을 조회하는 repository 테스트 추가
Go-Jaecheol 65b3d7f
test: 전체 태그 목록을 조회하는 인수 테스트 추가
Go-Jaecheol a9e91b8
refactor: TagsResponse 생성자 대신 정적 팩토리 메서드를 사용하도록 수정
Go-Jaecheol aca7d8a
test: 사용하지 않는 Tag 생성자 삭제 및 테스트 코드 수정
Go-Jaecheol a308ea7
refactor: getTagsByTagType 메서드에서 메서드명에 맞게 TagsResponse를 바로 반환하도록 수정
Go-Jaecheol 2264706
style: 개행 컨벤션 적용
Go-Jaecheol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/com/funeat/tag/application/TagService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.funeat.tag.application; | ||
|
||
import com.funeat.tag.domain.TagType; | ||
import com.funeat.tag.dto.TagDto; | ||
import com.funeat.tag.dto.TagsResponse; | ||
import com.funeat.tag.persistence.TagRepository; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
public class TagService { | ||
|
||
private final TagRepository tagRepository; | ||
|
||
public TagService(final TagRepository tagRepository) { | ||
this.tagRepository = tagRepository; | ||
} | ||
|
||
public List<TagsResponse> getAllTags() { | ||
final List<TagsResponse> responses = new ArrayList<>(); | ||
for (final TagType tagType : TagType.values()) { | ||
getTagsByTagType(responses, tagType); | ||
} | ||
return responses; | ||
} | ||
|
||
private void getTagsByTagType(final List<TagsResponse> responses, final TagType tagType) { | ||
final List<TagDto> tags = tagRepository.findTagsByTagType(tagType).stream() | ||
.map(TagDto::toDto) | ||
.collect(Collectors.toList()); | ||
final TagsResponse tagsResponse = TagsResponse.toResponse(tagType.name(), tags); | ||
responses.add(tagsResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.funeat.tag.domain; | ||
|
||
public enum TagType { | ||
|
||
TASTE, PRICE, ETC | ||
} |
4 changes: 3 additions & 1 deletion
4
...in/java/com/funeat/tag/domain/TagDto.java → .../main/java/com/funeat/tag/dto/TagDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
backend/src/main/java/com/funeat/tag/dto/TagsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.funeat.tag.dto; | ||
|
||
import java.util.List; | ||
|
||
public class TagsResponse { | ||
|
||
private final String tagType; | ||
private final List<TagDto> tags; | ||
|
||
public TagsResponse(final String tagType, final List<TagDto> tags) { | ||
this.tagType = tagType; | ||
this.tags = tags; | ||
} | ||
|
||
public static TagsResponse toResponse(final String tagType, final List<TagDto> tags) { | ||
return new TagsResponse(tagType, tags); | ||
} | ||
|
||
public String getTagType() { | ||
return tagType; | ||
} | ||
|
||
public List<TagDto> getTags() { | ||
return tags; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
backend/src/main/java/com/funeat/tag/persistence/TagRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package com.funeat.tag.persistence; | ||
|
||
import com.funeat.tag.domain.Tag; | ||
import com.funeat.tag.domain.TagType; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TagRepository extends JpaRepository<Tag, Long> { | ||
|
||
List<Tag> findTagsByIdIn(final List<Long> tagIds); | ||
|
||
List<Tag> findTagsByTagType(final TagType tagType); | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/com/funeat/tag/presentation/TagApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.funeat.tag.presentation; | ||
|
||
import com.funeat.tag.application.TagService; | ||
import com.funeat.tag.dto.TagsResponse; | ||
import java.util.List; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class TagApiController implements TagController { | ||
|
||
private final TagService tagService; | ||
|
||
public TagApiController(final TagService tagService) { | ||
this.tagService = tagService; | ||
} | ||
|
||
@GetMapping("/api/tags") | ||
public ResponseEntity<List<TagsResponse>> getAllTags() { | ||
final List<TagsResponse> responses = tagService.getAllTags(); | ||
return ResponseEntity.ok(responses); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/com/funeat/tag/presentation/TagController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.funeat.tag.presentation; | ||
|
||
import com.funeat.tag.dto.TagsResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@Tag(name = "04.Tag", description = "태그 기능") | ||
public interface TagController { | ||
|
||
@Operation(summary = "전체 태그 목록 조회", description = "전체 태그 목록을 태그 타입 별로 조회한다.") | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "전체 태그 목록 조회 성공." | ||
) | ||
@GetMapping | ||
ResponseEntity<List<TagsResponse>> getAllTags(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
backend/src/test/java/com/funeat/acceptance/tag/TagAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.funeat.acceptance.tag; | ||
|
||
import static com.funeat.acceptance.common.CommonSteps.STATUS_CODE를_검증한다; | ||
import static com.funeat.acceptance.common.CommonSteps.정상_처리; | ||
import static com.funeat.acceptance.tag.TagSteps.전체_태그_목록_조회_요청; | ||
import static com.funeat.tag.domain.TagType.ETC; | ||
import static com.funeat.tag.domain.TagType.PRICE; | ||
import static com.funeat.tag.domain.TagType.TASTE; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.funeat.acceptance.common.AcceptanceTest; | ||
import com.funeat.tag.domain.Tag; | ||
import com.funeat.tag.domain.TagType; | ||
import com.funeat.tag.dto.TagsResponse; | ||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
public class TagAcceptanceTest extends AcceptanceTest { | ||
|
||
@Test | ||
void 전체_태그_목록을_조회할_수_있다() { | ||
// given | ||
final var tag1 = 태그_추가_요청(new Tag("단짠단짠", TASTE)); | ||
final var tag2 = 태그_추가_요청(new Tag("매콤해요", TASTE)); | ||
final var tag3 = 태그_추가_요청(new Tag("갓성비", PRICE)); | ||
final var tag4 = 태그_추가_요청(new Tag("바삭바삭", ETC)); | ||
|
||
// when | ||
final var response = 전체_태그_목록_조회_요청(); | ||
|
||
// then | ||
STATUS_CODE를_검증한다(response, 정상_처리); | ||
전체_태그_목록_조회_결과를_검증한다(response, List.of(tag1, tag2, tag3, tag4)); | ||
} | ||
|
||
private Tag 태그_추가_요청(final Tag tag) { | ||
return tagRepository.save(tag); | ||
} | ||
|
||
private void 전체_태그_목록_조회_결과를_검증한다(final ExtractableResponse<Response> response, final List<Tag> tags) { | ||
final var expectedByType = tags.stream().collect(Collectors.groupingBy(Tag::getTagType)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Collectors.groupingBy()는 처음 보네요 |
||
final var actual = response.jsonPath().getList("", TagsResponse.class); | ||
|
||
for (final TagsResponse tagsResponse : actual) { | ||
final TagType tagType = TagType.valueOf(tagsResponse.getTagType()); | ||
assertThat(tagType).isIn(expectedByType.keySet()); | ||
assertThat(tagsResponse.getTags()).usingRecursiveComparison() | ||
.isEqualTo(expectedByType.get(tagType)); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/test/java/com/funeat/acceptance/tag/TagSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.funeat.acceptance.tag; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
public class TagSteps { | ||
|
||
public static ExtractableResponse<Response> 전체_태그_목록_조회_요청() { | ||
return given() | ||
.when() | ||
.get("/api/tags") | ||
.then() | ||
.extract(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
메소드 이름이
getTagsByTagType
니까 TagsResponse를 반환하고getAllTags
에서 add해주는건 어떨까용