Skip to content

Commit

Permalink
Merge pull request #25 from Team-Sopetit/feature/#24-doll-get-api
Browse files Browse the repository at this point in the history
[FEAT] 인형 이미지 조회
  • Loading branch information
thguss authored Jan 10, 2024
2 parents 4d81f1b + 80efaa0 commit c6fced6
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.soptie.server.doll.controller;

import static com.soptie.server.common.dto.Response.*;
import static com.soptie.server.doll.message.ResponseMessage.*;

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.RestController;

import com.soptie.server.common.dto.Response;
import com.soptie.server.doll.entity.DollType;
import com.soptie.server.doll.service.DollService;

import lombok.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/dolls")
public class DollController {

private final DollService dollService;

@GetMapping("/image/{type}")
public ResponseEntity<Response> getDollImages(@PathVariable DollType type) {
val response = dollService.getDollImage(type);
return ResponseEntity.ok(success(SUCCESS_GET_IMAGE.getMessage(), response));
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/soptie/server/doll/dto/DollImageResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.soptie.server.doll.dto;

import com.soptie.server.doll.entity.Doll;

public record DollImageResponse(
String faceImageUrl
) {

public static DollImageResponse of(Doll doll) {
return new DollImageResponse(doll.getImageInfo().getFaceImageUrl());
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/soptie/server/doll/entity/Doll.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@Getter
public class Doll {

@Id
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/soptie/server/doll/entity/DollImage.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.soptie.server.doll.entity;

import jakarta.persistence.Embeddable;
import lombok.Getter;

@Embeddable
@Getter
public class DollImage {

private String faceImageUrl;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/soptie/server/doll/message/ErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.soptie.server.doll.message;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public enum ErrorMessage {
INVALID_TYPE("유효하지 않은 인형 타입입니다."),
;

private final String message;
}
13 changes: 13 additions & 0 deletions src/main/java/com/soptie/server/doll/message/ResponseMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.soptie.server.doll.message;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public enum ResponseMessage {
SUCCESS_GET_IMAGE("인형 이미지 조회 성공"),
;

private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.soptie.server.doll.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.soptie.server.doll.entity.Doll;
import com.soptie.server.doll.entity.DollType;

public interface DollRepository extends JpaRepository<Doll, Long> {
Optional<Doll> findByDollType(DollType type);
}
8 changes: 8 additions & 0 deletions src/main/java/com/soptie/server/doll/service/DollService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.soptie.server.doll.service;

import com.soptie.server.doll.dto.DollImageResponse;
import com.soptie.server.doll.entity.DollType;

public interface DollService {
DollImageResponse getDollImage(DollType type);
}
33 changes: 33 additions & 0 deletions src/main/java/com/soptie/server/doll/service/DollServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.soptie.server.doll.service;

import static com.soptie.server.doll.message.ErrorMessage.*;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.soptie.server.doll.dto.DollImageResponse;
import com.soptie.server.doll.entity.Doll;
import com.soptie.server.doll.entity.DollType;
import com.soptie.server.doll.repository.DollRepository;

import jakarta.persistence.EntityNotFoundException;
import lombok.*;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class DollServiceImpl implements DollService {

private final DollRepository dollRepository;

@Override
public DollImageResponse getDollImage(DollType type) {
val doll = getDoll(type);
return DollImageResponse.of(doll);
}

private Doll getDoll(DollType type) {
return dollRepository.findByDollType(type)
.orElseThrow(() -> new EntityNotFoundException(INVALID_TYPE.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.soptie.server.doll.controller;

import static com.epages.restdocs.apispec.ResourceDocumentation.*;
import static org.mockito.Mockito.*;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.restdocs.payload.JsonFieldType.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;

import com.epages.restdocs.apispec.ResourceSnippetParameters;
import com.soptie.server.base.BaseControllerTest;
import com.soptie.server.common.dto.Response;
import com.soptie.server.doll.dto.DollImageResponse;
import com.soptie.server.doll.entity.DollType;
import com.soptie.server.doll.fixture.DollFixture;

@WebMvcTest(DollController.class)
class DollControllerTest extends BaseControllerTest {

@MockBean
DollController controller;

private final String DEFAULT_URL = "/api/v1/dolls";
private final String TAG = "DOLL";

@Test
@DisplayName("인형 이미지 조회 성공")
void success_getDollImage() throws Exception {
// given
DollType dollType = DollType.BEAR;
DollImageResponse dollImage = DollFixture.createDollImageResponseDTO();
ResponseEntity<Response> response = ResponseEntity.ok(Response.success("인형 이미지 조회 성공", dollImage));

// when
when(controller.getDollImages(dollType)).thenReturn(response);

// then
mockMvc
.perform(
RestDocumentationRequestBuilders.get(DEFAULT_URL + "/image/{type}", dollType)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(
MockMvcRestDocumentation.document(
"get-doll-image-docs",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
resource(
ResourceSnippetParameters.builder()
.tag(TAG)
.description("인형 이미지 조회")
.pathParameters(
parameterWithName("type").description("인형 타입")
)
.requestFields()
.responseFields(
fieldWithPath("success").type(BOOLEAN).description("응답 성공 여부"),
fieldWithPath("message").type(STRING).description("응답 메시지"),
fieldWithPath("data").type(OBJECT).description("응답 데이터"),
fieldWithPath("data.faceImageUrl").type(STRING).description("인형 얼굴 이미지 url")
)
.build())))
.andExpect(status().isOk());
}
}
12 changes: 12 additions & 0 deletions src/test/java/com/soptie/server/doll/fixture/DollFixture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.soptie.server.doll.fixture;

import com.soptie.server.doll.dto.DollImageResponse;

public class DollFixture {

private static final String FACE_IMAGE_URL = "face-image-url";

public static DollImageResponse createDollImageResponseDTO() {
return new DollImageResponse(FACE_IMAGE_URL);
}
}

0 comments on commit c6fced6

Please sign in to comment.