Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import turip.content.controller.dto.response.ContentCountResponse;
import turip.content.controller.dto.response.ContentResponse;
import turip.content.service.ContentService;

@RestController
Expand All @@ -22,4 +24,12 @@ public ResponseEntity<ContentCountResponse> readCountByRegionName(
ContentCountResponse response = contentService.countByRegionName(regionName);
return ResponseEntity.ok(response);
}

@GetMapping("/{id}")
public ResponseEntity<ContentResponse> readContentById(
@PathVariable Long id
Comment thread
RaZel713 marked this conversation as resolved.
Comment thread
RaZel713 marked this conversation as resolved.
) {
ContentResponse response = contentService.getById(id);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package turip.content.controller.dto.response;

import java.time.LocalDate;
import turip.content.domain.Content;

public record ContentResponse(
Long id,
Long creatorId,
Long regionId,
String title,
String url,
LocalDate uploadedDate
) {
public static ContentResponse of(Content content) {
Comment thread
RaZel713 marked this conversation as resolved.
Comment thread
RaZel713 marked this conversation as resolved.
return new ContentResponse(
content.getId(),
content.getCreator().getId(),
content.getRegion().getId(),
content.getTitle(),
content.getUrl(),
content.getUploadedDate()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import turip.content.controller.dto.response.ContentCountResponse;
import turip.content.controller.dto.response.ContentResponse;
import turip.content.domain.Content;
import turip.content.repository.ContentRepository;
import turip.exception.NotFoundException;
Expand All @@ -18,8 +19,9 @@ public ContentCountResponse countByRegionName(String regionName) {
return ContentCountResponse.from(count);
}

private Content getById(Long id) {
return contentRepository.findById(id)
public ContentResponse getById(Long id) {
Content content = contentRepository.findById(id)
.orElseThrow(() -> new NotFoundException("컨텐츠를 찾을 수 없습니다."));
return ContentResponse.of(content);
}
}
31 changes: 31 additions & 0 deletions backend/src/test/java/turip/content/api/ContentApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,35 @@ void readCountByRegionName() {
.body("count", is(2));
}
}

@DisplayName("/contents/{id} GET 컨텐츠 단건 조회 테스트")
@Nested
class readContentById {
@DisplayName("id로 컨텐츠 단건 조회 성공 시 200 OK 코드와 컨텐츠 정보를 응답한다")
@Test
void readContentById() {
// given
jdbcTemplate.update(
"INSERT INTO Creator (profile_image, channel_name) VALUES (?, ?)",
"https://image.example.com/creator1.jpg", "TravelMate");
jdbcTemplate.update(
"INSERT INTO Region (name) VALUES (?)",
"seoul");
jdbcTemplate.update(
"INSERT INTO Content (creator_id, region_id, url, title, uploaded_date) VALUES (?, ?, ?, ?, ?)",
1, 1, "https://youtube.com/watch?v=abcd1", "서울 데이트 코스 추천", "2024-07-01");

// when & then
RestAssured.given().port(port)
.when().get("/contents/{id}", 1)
.then()
.statusCode(200)
.body("id", is(1))
.body("creatorId", is(1))
.body("regionId", is(1))
.body("title", is("서울 데이트 코스 추천"))
.body("url", is("https://youtube.com/watch?v=abcd1"))
.body("uploadedDate", is("2024-07-01"));
}
}
}