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] 여행후기 기본 구현 완료 #39

Merged
merged 6 commits into from
Jan 8, 2024
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
@@ -1,7 +1,7 @@
package com.haejwo.tripcometrue.domain.place.controller;

import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDTO;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDTO;
import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDto;
import com.haejwo.tripcometrue.domain.place.service.PlaceService;
import com.haejwo.tripcometrue.global.util.ResponseDTO;
import lombok.RequiredArgsConstructor;
Expand All @@ -26,38 +26,38 @@ public class PlaceController {
private final PlaceService placeService;

@PostMapping
public ResponseEntity<ResponseDTO<PlaceResponseDTO>> placeAdd(
@RequestBody PlaceRequestDTO requestDto
public ResponseEntity<ResponseDTO<PlaceResponseDto>> placeAdd(
@RequestBody PlaceRequestDto requestDto
) {

PlaceResponseDTO responseDto = placeService.addPlace(requestDto);
ResponseDTO<PlaceResponseDTO> responseBody = ResponseDTO.okWithData(responseDto);
PlaceResponseDto responseDto = placeService.addPlace(requestDto);
ResponseDTO<PlaceResponseDto> responseBody = ResponseDTO.okWithData(responseDto);

return ResponseEntity
.status(responseBody.getCode())
.body(responseBody);
}

@GetMapping("/{placeId}")
public ResponseEntity<ResponseDTO<PlaceResponseDTO>> placeDetails(
public ResponseEntity<ResponseDTO<PlaceResponseDto>> placeDetails(
@PathVariable Long placeId
) {

PlaceResponseDTO responseDto = placeService.findPlace(placeId);
ResponseDTO<PlaceResponseDTO> responseBody = ResponseDTO.okWithData(responseDto);
PlaceResponseDto responseDto = placeService.findPlace(placeId);
ResponseDTO<PlaceResponseDto> responseBody = ResponseDTO.okWithData(responseDto);

return ResponseEntity
.status(responseBody.getCode())
.body(responseBody);
}

@GetMapping
public ResponseEntity<ResponseDTO<Page<PlaceResponseDTO>>> placeList(
public ResponseEntity<ResponseDTO<Page<PlaceResponseDto>>> placeList(
Pageable pageable,
@RequestParam Integer storedCount
) {

Page<PlaceResponseDTO> placePage = placeService.findPlaces(pageable, storedCount);
Page<PlaceResponseDto> placePage = placeService.findPlaces(pageable, storedCount);

ResponseDTO responseBody = ResponseDTO.okWithData(placePage);

Expand All @@ -67,13 +67,13 @@ public ResponseEntity<ResponseDTO<Page<PlaceResponseDTO>>> placeList(
}

@PutMapping("/{placeId}")
public ResponseEntity<ResponseDTO<PlaceResponseDTO>> placeModify(
public ResponseEntity<ResponseDTO<PlaceResponseDto>> placeModify(
@PathVariable Long placeId,
@RequestBody PlaceRequestDTO requestDto
@RequestBody PlaceRequestDto requestDto
) {

PlaceResponseDTO responseDto = placeService.modifyPlace(placeId, requestDto);
ResponseDTO<PlaceResponseDTO> responseBody = ResponseDTO.okWithData(responseDto);
PlaceResponseDto responseDto = placeService.modifyPlace(placeId, requestDto);
ResponseDTO<PlaceResponseDto> responseBody = ResponseDTO.okWithData(responseDto);

return ResponseEntity
.status(responseBody.getCode())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.haejwo.tripcometrue.domain.place.dto.request;

public record PlaceFilterRequestDTO(
public record PlaceFilterRequestDto(
Integer stored_count,
Integer storedCount
) {

// record는 Compact Constructor라는 기능있어, 생성자 내부의 변수에 대한 로직이 마지막으로 동작하여 변수 초기화를 한다.
public PlaceFilterRequestDTO {
public PlaceFilterRequestDto {
storedCount = stored_count;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.time.LocalTime;
import lombok.Builder;

public record PlaceRequestDTO(
public record PlaceRequestDto(
String name,
String address,
String description,
Expand All @@ -18,7 +18,7 @@ public record PlaceRequestDTO(
) {

@Builder
public PlaceRequestDTO(
public PlaceRequestDto(
String name,
String address,
String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.haejwo.tripcometrue.domain.place.entity.Place;
import java.time.LocalTime;
import lombok.Builder;
public record PlaceResponseDTO(
public record PlaceResponseDto(
Long id,
String name,
String address,
Expand All @@ -18,7 +18,7 @@ public record PlaceResponseDTO(
) {

@Builder
public PlaceResponseDTO(
public PlaceResponseDto(
Long id,
String name,
String address,
Expand All @@ -42,8 +42,8 @@ public PlaceResponseDTO(
this.cityId = cityId;
}

public static PlaceResponseDTO fromEntity(Place entity) {
return PlaceResponseDTO.builder()
public static PlaceResponseDto fromEntity(Place entity) {
return PlaceResponseDto.builder()
.id(entity.getId())
.name(entity.getName())
.address(entity.getAddress())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.haejwo.tripcometrue.domain.place.entity;

import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDTO;
import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDto;
import com.haejwo.tripcometrue.global.entity.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -60,7 +60,7 @@ public Place(
this.cityId = cityId;
}

public void update(PlaceRequestDTO requestDto) {
public void update(PlaceRequestDto requestDto) {
this.name = requestDto.name();
this.address = requestDto.address();
this.description = requestDto.description();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.haejwo.tripcometrue.domain.place.service;

import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDTO;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDTO;
import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDto;
import com.haejwo.tripcometrue.domain.place.entity.Place;
import com.haejwo.tripcometrue.domain.place.exception.PlaceNotFoundException;
import com.haejwo.tripcometrue.domain.place.repositroy.PlaceRepository;
Expand All @@ -20,45 +20,43 @@ public class PlaceService {
private final PlaceRepository placeRepository;

@Transactional
public PlaceResponseDTO addPlace(PlaceRequestDTO requestDto) {
public PlaceResponseDto addPlace(PlaceRequestDto requestDto) {

System.out.println(requestDto.storedCount());
Place requestPlace = requestDto.toEntity();
Place savedPlace = placeRepository.save(requestPlace);
System.out.println(savedPlace.getStoredCount());
PlaceResponseDTO responseDto = PlaceResponseDTO.fromEntity(savedPlace);
PlaceResponseDto responseDto = PlaceResponseDto.fromEntity(savedPlace);

return responseDto;

}

@Transactional(readOnly = true)
public PlaceResponseDTO findPlace(Long placeId) {
public PlaceResponseDto findPlace(Long placeId) {

Place findPlace = findPlaceById(placeId);

PlaceResponseDTO responseDto = PlaceResponseDTO.fromEntity(findPlace);
PlaceResponseDto responseDto = PlaceResponseDto.fromEntity(findPlace);

return responseDto;
}

@Transactional(readOnly = true)
public Page<PlaceResponseDTO> findPlaces(Pageable pageable, Integer storedCount) {
public Page<PlaceResponseDto> findPlaces(Pageable pageable, Integer storedCount) {

Page<Place> findPlaces = placeRepository.findPlaceWithFilter(pageable, storedCount);

Page<PlaceResponseDTO> result = findPlaces.map(PlaceResponseDTO::fromEntity);
Page<PlaceResponseDto> result = findPlaces.map(PlaceResponseDto::fromEntity);

return result;

}

@Transactional
public PlaceResponseDTO modifyPlace(Long placeId, PlaceRequestDTO requestDto) {
public PlaceResponseDto modifyPlace(Long placeId, PlaceRequestDto requestDto) {

Place place = findPlaceById(placeId);
place.update(requestDto);
PlaceResponseDTO responseDto = PlaceResponseDTO.fromEntity(place);
PlaceResponseDto responseDto = PlaceResponseDto.fromEntity(place);

return responseDto;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.haejwo.tripcometrue.domain.triprecord.controller;

import com.haejwo.tripcometrue.domain.triprecord.dto.request.TripRecordRequestDto;
import com.haejwo.tripcometrue.domain.triprecord.dto.response.TripRecordResponseDto;
import com.haejwo.tripcometrue.domain.triprecord.service.TripRecordService;
import com.haejwo.tripcometrue.global.util.ResponseDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/trip-record")
Copy link
Member

Choose a reason for hiding this comment

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

URL에 도메인을 어떻게 표시할지는 추후에 논의해서 통일하면 좋을듯 합니다 :)

Copy link
Member Author

Choose a reason for hiding this comment

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

원래는 restful 기준에 따르면 소문자와 - _ 이 두개로 표현하는게 정석으로 알고 있습니다.

@RequiredArgsConstructor
public class TripRecordController {

private final TripRecordService tripRecordService;

@PostMapping
public ResponseEntity<ResponseDTO<TripRecordResponseDto>> tripRecordAdd(
Copy link
Member

Choose a reason for hiding this comment

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

컨트롤러의 메소드에선 서비스 계층의 메소드와 다르게 동사를 뒤에 배치한 이유가 있나요?
일반적으로 메소드명은 동사가 앞에 나오는 것으로 이해하고 있어서요.

Copy link
Member Author

Choose a reason for hiding this comment

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

구분을 위해서 그렇게 했습니다.
사람에 따라서는 컨트롤러와 서비스 단 구분을 위해서 함수명 단어를 아예 다르게 하시는 분도 있는 모양입니다.
제가 알기로는 이 부분은 팀내 약속과 개인의 취향의 영역이라고 알고있는데, 대표적인 컨벤션 같은게 있나요?

Copy link
Member

Choose a reason for hiding this comment

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

아하 그렇군요! 제가 알기로도 정해진 건 없다고 들었습니다! 메소드명까지 굳이 통일할 필요가 없다면 재량껏 사용해도 될듯 해요. 다른 분들은 어떻게 생각하시는지 궁금하네요 ㅎㅎ

@RequestBody TripRecordRequestDto requestDto
) {

TripRecordResponseDto responseDto = tripRecordService.addTripRecord(requestDto);
ResponseDTO<TripRecordResponseDto> responseBody = ResponseDTO.okWithData(responseDto);

return ResponseEntity
Copy link
Member

Choose a reason for hiding this comment

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

등록한 여행 후기 내용 전체를 다시 프론트로 돌려주는 이유가 있나요?
등록된 여행 후기 Id만 반환시키고, 프론트에서 해당 id를 통해 GET 요청을 하도록 하면 되지 않을까 궁금해서 여쭤봅니다.
프론트에서 POST url을 그대로 가지고 있으면 중복 등록이 될 수 있어서 GET으로 리다이렉트 하는 것처럼요!

Copy link
Member Author

Choose a reason for hiding this comment

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

이건 그냥 습관, 성향 차이로 알고 있습니다.

.status(responseBody.getCode())
.body(responseBody);
}

@GetMapping("/{tripRecordId}")
public ResponseEntity<ResponseDTO<TripRecordResponseDto>> tripRecordDetails(
@PathVariable Long tripRecordId
) {

TripRecordResponseDto responseDto = tripRecordService.findTripRecord(tripRecordId);
ResponseDTO<TripRecordResponseDto> responseBody = ResponseDTO.okWithData(responseDto);

return ResponseEntity
.status(responseBody.getCode())
.body(responseBody);
}

@PutMapping("/{tripRecordId}")
public ResponseEntity<ResponseDTO<TripRecordResponseDto>> tripRecordModify(
@PathVariable Long tripRecordId,
@RequestBody TripRecordRequestDto requestDto
) {

TripRecordResponseDto responseDto = tripRecordService.modifyTripRecord(tripRecordId, requestDto);
ResponseDTO<TripRecordResponseDto> responseBody = ResponseDTO.okWithData(responseDto);

return ResponseEntity
.status(responseBody.getCode())
.body(responseBody);

}

@DeleteMapping("/{tripRecordId}")
public ResponseEntity<ResponseDTO> tripRecordRemove(
@PathVariable Long tripRecordId
) {

tripRecordService.removeTripRecord(tripRecordId);
ResponseDTO responseBody = ResponseDTO.ok();

return ResponseEntity
.status(responseBody.getCode())
.body(responseBody);
}





}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.haejwo.tripcometrue.domain.triprecord.dto.request;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.haejwo.tripcometrue.domain.triprecord.entity.ExpenseRangeType;
import com.haejwo.tripcometrue.domain.triprecord.entity.TripRecord;
import java.time.LocalDate;
import lombok.Builder;

public record TripRecordRequestDto(
String title,
String content,
ExpenseRangeType expenseRangeType,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripStartDay,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripEndDay,
String countries
) {

@Builder
public TripRecordRequestDto(String title, String content, ExpenseRangeType expenseRangeType,
LocalDate tripStartDay, LocalDate tripEndDay, String countries) {
this.title = title;
this.content = content;
this.expenseRangeType = expenseRangeType;
this.tripStartDay = tripStartDay;
this.tripEndDay = tripEndDay;
this.countries = countries;
}

public TripRecord toEntity() {
return TripRecord.builder()
.title(this.title)
.content(this.content)
.expenseRangeType(this.expenseRangeType)
.tripStartDay(this.tripStartDay)
.tripEndDay(this.tripEndDay)
.countries(this.countries)
.build();
}

}
Loading