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

feat: 복덕방 단일 조회 #156

Merged
merged 12 commits into from
Jan 17, 2024
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(),
Comment on lines +80 to +81
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R

여기 사이에 comments가 빠져있습니다!

참고)

 "comments": [
    {
      "id": 2,
      "user_id": 53,
      "land_id": 2,
      "content": "ok",
      "score": 4,
      "nickname": "mellonia",
      "is_deleted": false,
      "grantEdit": false,
      "grantDelete": false,
      "created_at": "2024-01-12 14:18:02",
      "updated_at": "2024-01-12 14:18:02"
    }
  ]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존 코인을 찾아봤을 때 comments와 관련된 api를 사용하고 있지 않았고,
프론트쪽에 물어보니 comments를 사용하지 않는다는 답변을 받았습니다!

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

DB에 JSON형태로 배열이 저장되어서 JSONArray를 사용했군요! 👍
일일히 배열 모양을 파싱하는 것보다는 훨씬 좋은 방법이라고 생각됩니다
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSONArray는 처음보는데 이런 것도 있었네요 👍
일반 문자열 파싱에 비해 코드가 간결해져서 좋네요!

.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 @@ -20,4 +20,11 @@ public static ErrorResponse of(int code, String message) {
public static ErrorResponse from(String message) {
return new ErrorResponse(0, message);
}

public record ErrorResponseWrapper(ErrorResponse error) {

public static ErrorResponseWrapper from(ErrorResponse error) {
return new ErrorResponseWrapper(error);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package in.koreatech.koin.global.exception;

import in.koreatech.koin.domain.auth.exception.AuthException;
import in.koreatech.koin.domain.user.exception.UserNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import in.koreatech.koin.domain.auth.exception.AuthException;
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;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
Expand Down Expand Up @@ -38,4 +41,11 @@ public ResponseEntity<ErrorResponse> handleAuthException(AuthException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.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("복덕방이 존재하지 않습니다.")));
}
}
Loading