Skip to content

Commit ed8fc1b

Browse files
authored
[feat] 컨텐츠 조회 API 구현 (#70)
* feat: 컨텐츠 아이디를 기반으로 컨텐츠를 조회하는 service 메서드 작성 * feat: 컨텐츠 아이디를 기반으로 컨텐츠를 조회하는 API 구현 * test: 컨텐츠 아이디를 기반으로 컨텐츠를 조회하는 API 테스트 작성
1 parent 06f4fed commit ed8fc1b

4 files changed

Lines changed: 69 additions & 2 deletions

File tree

backend/src/main/java/turip/content/controller/ContentController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import lombok.RequiredArgsConstructor;
44
import org.springframework.http.ResponseEntity;
55
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
67
import org.springframework.web.bind.annotation.RequestMapping;
78
import org.springframework.web.bind.annotation.RequestParam;
89
import org.springframework.web.bind.annotation.RestController;
910
import turip.content.controller.dto.response.ContentCountResponse;
11+
import turip.content.controller.dto.response.ContentResponse;
1012
import turip.content.service.ContentService;
1113

1214
@RestController
@@ -22,4 +24,12 @@ public ResponseEntity<ContentCountResponse> readCountByRegionName(
2224
ContentCountResponse response = contentService.countByRegionName(regionName);
2325
return ResponseEntity.ok(response);
2426
}
27+
28+
@GetMapping("/{id}")
29+
public ResponseEntity<ContentResponse> readContentById(
30+
@PathVariable Long id
31+
) {
32+
ContentResponse response = contentService.getById(id);
33+
return ResponseEntity.ok(response);
34+
}
2535
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package turip.content.controller.dto.response;
2+
3+
import java.time.LocalDate;
4+
import turip.content.domain.Content;
5+
6+
public record ContentResponse(
7+
Long id,
8+
Long creatorId,
9+
Long regionId,
10+
String title,
11+
String url,
12+
LocalDate uploadedDate
13+
) {
14+
public static ContentResponse of(Content content) {
15+
return new ContentResponse(
16+
content.getId(),
17+
content.getCreator().getId(),
18+
content.getRegion().getId(),
19+
content.getTitle(),
20+
content.getUrl(),
21+
content.getUploadedDate()
22+
);
23+
}
24+
}

backend/src/main/java/turip/content/service/ContentService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.RequiredArgsConstructor;
44
import org.springframework.stereotype.Service;
55
import turip.content.controller.dto.response.ContentCountResponse;
6+
import turip.content.controller.dto.response.ContentResponse;
67
import turip.content.domain.Content;
78
import turip.content.repository.ContentRepository;
89
import turip.exception.NotFoundException;
@@ -18,8 +19,9 @@ public ContentCountResponse countByRegionName(String regionName) {
1819
return ContentCountResponse.from(count);
1920
}
2021

21-
private Content getById(Long id) {
22-
return contentRepository.findById(id)
22+
public ContentResponse getById(Long id) {
23+
Content content = contentRepository.findById(id)
2324
.orElseThrow(() -> new NotFoundException("컨텐츠를 찾을 수 없습니다."));
25+
return ContentResponse.of(content);
2426
}
2527
}

backend/src/test/java/turip/content/api/ContentApiTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,35 @@ void readCountByRegionName() {
5757
.body("count", is(2));
5858
}
5959
}
60+
61+
@DisplayName("/contents/{id} GET 컨텐츠 단건 조회 테스트")
62+
@Nested
63+
class readContentById {
64+
@DisplayName("id로 컨텐츠 단건 조회 성공 시 200 OK 코드와 컨텐츠 정보를 응답한다")
65+
@Test
66+
void readContentById() {
67+
// given
68+
jdbcTemplate.update(
69+
"INSERT INTO Creator (profile_image, channel_name) VALUES (?, ?)",
70+
"https://image.example.com/creator1.jpg", "TravelMate");
71+
jdbcTemplate.update(
72+
"INSERT INTO Region (name) VALUES (?)",
73+
"seoul");
74+
jdbcTemplate.update(
75+
"INSERT INTO Content (creator_id, region_id, url, title, uploaded_date) VALUES (?, ?, ?, ?, ?)",
76+
1, 1, "https://youtube.com/watch?v=abcd1", "서울 데이트 코스 추천", "2024-07-01");
77+
78+
// when & then
79+
RestAssured.given().port(port)
80+
.when().get("/contents/{id}", 1)
81+
.then()
82+
.statusCode(200)
83+
.body("id", is(1))
84+
.body("creatorId", is(1))
85+
.body("regionId", is(1))
86+
.body("title", is("서울 데이트 코스 추천"))
87+
.body("url", is("https://youtube.com/watch?v=abcd1"))
88+
.body("uploadedDate", is("2024-07-01"));
89+
}
90+
}
6091
}

0 commit comments

Comments
 (0)