Skip to content

Commit

Permalink
feat: 복덕방 단일 조회 (#156)
Browse files Browse the repository at this point in the history
* feat : controller 구현

* feat : repository에 id로 조회 기능 생성

* feat : service에 조회 기능 구현

* feat : 응답 DTO 구현

* refactor : domain 필드 자료형 변경

* refactor : stream 라인 변경

* refactor : 코드 축약

* test : 단일 조회 테스트 작성

* refactor : 에러 메시지 구체적으로 수정

* refactor : 기존 응답 구조와 동일하도록 수정

* test : 테스트에 이미지 추가
  • Loading branch information
daheeParkk authored Jan 17, 2024
1 parent 1130bfb commit d969487
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ repositories {
}

dependencies {
implementation group: 'org.json', name: 'json', version: '20231013'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

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

import in.koreatech.koin.domain.land.dto.LandListItemResponse;
import in.koreatech.koin.domain.land.dto.LandResponse;
import in.koreatech.koin.domain.land.service.LandService;
import lombok.RequiredArgsConstructor;

Expand All @@ -21,4 +23,10 @@ public ResponseEntity<List<LandListItemResponse>> getLands() {
List<LandListItemResponse> responses = landService.getLands();
return ResponseEntity.ok(responses);
}

@GetMapping("/lands/{id}")
public ResponseEntity<LandResponse> getLand(@PathVariable Long id) {
LandResponse response = landService.getLand(id);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
public record LandListItemResponse(
String internalName,
String monthlyFee,
String latitude,
Double latitude,
String charterFee,
String name,
Long id,
String longitude,
Double longitude,
String roomType) {

public static LandListItemResponse from(Land land) {
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/in/koreatech/koin/domain/land/dto/LandResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package in.koreatech.koin.domain.land.dto;

import static com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;

import java.time.LocalDateTime;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.land.model.Land;

@JsonNaming(value = SnakeCaseStrategy.class)
public record LandResponse(
Boolean optElectronicDoorLocks,
Boolean optTv,
String monthlyFee,
Boolean optElevator,
Boolean optWaterPurifier,
Boolean optWasher,
Double latitude,
String charterFee,
Boolean optVeranda,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createdAt,
String description,
List<String> imageUrls,
Boolean optGasRange,
Boolean optInduction,
String internalName,
Boolean isDeleted,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updatedAt,
Boolean optBidet,
Boolean optShoeCloset,
Boolean optRefrigerator,
Long id,
Long floor,
String managementFee,
Boolean optDesk,
Boolean optCloset,
Double longitude,
String address,
Boolean optBed,
Double size,
String phone,
Boolean optAirConditioner,
String name,
String deposit,
Boolean optMicrowave,
String permalink,
String roomType) {

public static LandResponse of(Land land, List<String> imageUrls, String permalink) {
return new LandResponse(
land.getOptElectronicDoorLocks(),
land.getOptTv(),
land.getMonthlyFee(),
land.getOptElevator(),
land.getOptWaterPurifier(),
land.getOptWasher(),
land.getLatitude(),
land.getCharterFee(),
land.getOptVeranda(),
land.getCreatedAt(),
land.getDescription(),
imageUrls,
land.getOptGasRange(),
land.getOptInduction(),
land.getInternalName(),
land.getIsDeleted(),
land.getUpdatedAt(),
land.getOptBidet(),
land.getOptShoeCloset(),
land.getOptRefrigerator(),
land.getId(),
land.getFloor(),
land.getManagementFee(),
land.getOptDesk(),
land.getOptCloset(),
land.getLongitude(),
land.getAddress(),
land.getOptBed(),
land.getSize(),
land.getPhone(),
land.getOptAirConditioner(),
land.getName(),
land.getDeposit(),
land.getOptMicrowave(),
permalink,
land.getRoomType()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package in.koreatech.koin.domain.land.exception;

public class LandNotFoundException extends RuntimeException {
private static final String DEFAULT_MESSAGE = "복덕방이 존재하지 않습니다.";

public LandNotFoundException(String message) {
super(message);
}

public static LandNotFoundException withDetail(String detail) {
String message = String.format("%s %s", DEFAULT_MESSAGE, detail);
return new LandNotFoundException(message);
}
}
14 changes: 6 additions & 8 deletions src/main/java/in/koreatech/koin/domain/land/model/Land.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package in.koreatech.koin.domain.land.model;

import in.koreatech.koin.global.common.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand All @@ -18,7 +19,7 @@
@Entity
@Table(name = "lands")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Land {
public class Land extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -35,21 +36,18 @@ public class Land {
@Column(name = "internal_name", nullable = false, length = 50)
private String internalName;

@Size(max = 20)
@Column(name = "size", length = 20)
private String size;
private Double size;

@Size(max = 20)
@Column(name = "room_type", length = 20)
private String roomType;

@Size(max = 20)
@Column(name = "latitude", length = 20)
private String latitude;
private Double latitude;

@Size(max = 20)
@Column(name = "longitude", length = 20)
private String longitude;
private Double longitude;

@Size(max = 20)
@Column(name = "phone", length = 20)
Expand Down Expand Up @@ -155,7 +153,7 @@ public class Land {
private Boolean isDeleted = false;

@Builder
private Land(String internalName, String name, String size, String roomType, String latitude, String longitude,
private Land(String internalName, String name, Double size, String roomType, Double latitude, Double longitude,
String phone, String imageUrls, String address, String description, Long floor, String deposit,
String monthlyFee, String charterFee, String managementFee) {
this.internalName = internalName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package in.koreatech.koin.domain.land.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.repository.Repository;

Expand All @@ -10,5 +11,7 @@ public interface LandRepository extends Repository<Land, Long> {

List<Land> findAll();

Optional<Land> findById(Long id);

Land save(Land request);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package in.koreatech.koin.domain.land.service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

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

import in.koreatech.koin.domain.land.dto.LandListItemResponse;
import in.koreatech.koin.domain.land.dto.LandResponse;
import in.koreatech.koin.domain.land.exception.LandNotFoundException;
import in.koreatech.koin.domain.land.model.Land;
import in.koreatech.koin.domain.land.repository.LandRepository;
import lombok.RequiredArgsConstructor;

Expand All @@ -22,4 +28,22 @@ public List<LandListItemResponse> getLands() {
.map(LandListItemResponse::from)
.toList();
}

public LandResponse getLand(Long id) {
Land land = landRepository.findById(id)
.orElseThrow(() -> LandNotFoundException.withDetail("id: " + id));

String image = land.getImageUrls();
List<String> imageUrls = null;

if (image != null) {
imageUrls = new JSONArray(image)
.toList()
.stream()
.map(Object::toString)
.toList();
}

return LandResponse.of(land, imageUrls, URLEncoder.encode(land.getInternalName(), StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import in.koreatech.koin.domain.auth.exception.AuthException;
import in.koreatech.koin.domain.community.exception.ArticleNotFoundException;
import in.koreatech.koin.domain.land.exception.LandNotFoundException;
import in.koreatech.koin.domain.user.exception.UserNotFoundException;
import in.koreatech.koin.global.exception.ErrorResponse.ErrorResponseWrapper;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -42,6 +43,13 @@ public ResponseEntity<ErrorResponse> handleAuthException(AuthException e) {
.body(ErrorResponse.from("잘못된 인증정보입니다."));
}

@ExceptionHandler
public ResponseEntity<ErrorResponseWrapper> handleAuthException(LandNotFoundException e) {
log.warn(e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ErrorResponseWrapper.from(ErrorResponse.from("복덕방이 존재하지 않습니다.")));
}

@ExceptionHandler
public ResponseEntity<ErrorResponseWrapper> handleArticleNotFoundException(ArticleNotFoundException e) {
log.warn(e.getMessage());
Expand Down
Loading

0 comments on commit d969487

Please sign in to comment.