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

해시태그 목록 조회 기능 구현(issue #307) #308

Merged
merged 4 commits into from
Aug 16, 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
30 changes: 30 additions & 0 deletions backend/src/main/java/develup/api/HashTagApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package develup.api;

import java.util.List;
import develup.api.common.ApiResponse;
import develup.application.hashtag.HashTagResponse;
import develup.application.hashtag.HashTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "해시태그 API")
class HashTagApi {

private final HashTagService hashTagService;

public HashTagApi(HashTagService hashTagService) {
this.hashTagService = hashTagService;
}

@GetMapping("/hash-tags")
@Operation(summary = "해시태그 목록 조회 API", description = "해시태그 목록을 조회합니다.")
public ResponseEntity<ApiResponse<List<HashTagResponse>>> getHashTags() {
List<HashTagResponse> responses = hashTagService.getHashTags();

return ResponseEntity.ok(new ApiResponse<>(responses));
}
}
2 changes: 1 addition & 1 deletion backend/src/main/java/develup/api/SolutionCommentApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import develup.api.common.ApiResponse;
import develup.application.auth.Accessor;
import develup.application.solution.comment.CreateSolutionCommentResponse;
import develup.application.solution.comment.SolutionCommentRepliesResponse;
import develup.application.solution.comment.SolutionCommentRequest;
import develup.application.solution.comment.SolutionCommentService;
import develup.application.solution.comment.SolutionCommentRepliesResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package develup.application.hashtag;

import java.util.List;
import develup.domain.hashtag.HashTagRepository;
import org.springframework.stereotype.Service;

@Service
public class HashTagService {

private final HashTagRepository hashTagRepository;

public HashTagService(HashTagRepository hashTagRepository) {
this.hashTagRepository = hashTagRepository;
}

public List<HashTagResponse> getHashTags() {
return hashTagRepository.findAll().stream()
.map(HashTagResponse::from)
.toList();
}
}
6 changes: 5 additions & 1 deletion backend/src/test/java/develup/api/ApiTestSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import develup.api.auth.AuthArgumentResolver;
import develup.api.auth.CookieAuthorizationExtractor;
import develup.application.auth.AuthService;
import develup.application.hashtag.HashTagService;
import develup.application.member.MemberService;
import develup.application.mission.MissionService;
import develup.application.solution.SolutionService;
Expand Down Expand Up @@ -38,8 +39,11 @@ public class ApiTestSupport {
protected SolutionCommentService solutionCommentService;

@MockBean
protected CookieAuthorizationExtractor cookieAuthorizationExtractor;
protected HashTagService hashTagService;

@MockBean
protected AuthArgumentResolver argumentResolver;

@MockBean
protected CookieAuthorizationExtractor cookieAuthorizationExtractor;
}
37 changes: 37 additions & 0 deletions backend/src/test/java/develup/api/HashTagApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package develup.api;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.List;
import develup.application.hashtag.HashTagResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;

class HashTagApiTest extends ApiTestSupport {

@Test
@DisplayName("해시 태그 목록을 조회한다.")
void getHashTags() throws Exception {
List<HashTagResponse> responses = List.of(
new HashTagResponse(1L, "JAVA"),
new HashTagResponse(2L, "JAVASCRIPT")
);
BDDMockito.given(hashTagService.getHashTags())
.willReturn(responses);

mockMvc.perform(get("/hash-tags"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.data[0].id", is(1)))
.andExpect(jsonPath("$.data[0].name", equalTo("JAVA")))
.andExpect(jsonPath("$.data[1].id", is(2)))
.andExpect(jsonPath("$.data[1].name", equalTo("JAVASCRIPT")))
.andExpect(jsonPath("$.data.length()", is(2)));
}
}
Loading