Skip to content

Commit

Permalink
feat: #8 버스 관련 리뷰 CRUD 추가
Browse files Browse the repository at this point in the history
- 버스 리뷰 조회 (버스, 정류장, 시간대 옵션)
- 버스 리뷰 생성
- 버스 리뷰 수정 (리뷰, 시간대, 별점 옵션)
- 버스 리뷰 삭제
  • Loading branch information
ss0ngcode committed Aug 2, 2024
1 parent 559881b commit e7278a6
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.talkka.server.review.controller;

import java.util.List;

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.talkka.server.review.dto.BusReviewReqDto;
import com.talkka.server.review.dto.BusReviewRespDto;
import com.talkka.server.review.service.BusReviewService;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/bus-review")
@RequiredArgsConstructor
public class BusReviewController {

private final BusReviewService busReviewService;

@GetMapping("")
public List<BusReviewRespDto> getBusReviewList(
@SessionAttribute(name = "userId") Long userId,
@RequestParam Long routeId,
@RequestParam Long stationId,
@RequestParam Integer timeSlot
) {
return busReviewService.getBusReviewList(userId, routeId, stationId, timeSlot);
}

@PostMapping("")
public void saveBusReview(
@SessionAttribute(name = "userId") Long userId,
@RequestBody BusReviewReqDto busReviewReqDto
) {
busReviewService.createBusReview(userId, busReviewReqDto);
}

@PutMapping("{bus_review_id}")
public BusReviewRespDto updateBusReview(
@SessionAttribute(name = "userId") Long userId,
@PathVariable(name = "bus_review_id") Long busReviewId,
@RequestBody BusReviewReqDto busReviewReqDto
) {
return busReviewService.updateBusReview(busReviewId, busReviewReqDto);
}

@DeleteMapping("{bus_review_id}")
public void deleteBusReview(
@SessionAttribute(name = "userId") Long userId,
@PathVariable(name = "bus_review_id") Long busReviewId
) {
busReviewService.deleteBusReview(busReviewId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.talkka.server.review.dao;

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

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

@Repository
public interface BusReviewRepository extends JpaRepository<BusReviewEntity, Long> {

public Optional<List<BusReviewEntity>> findAllByUserIdAndRouteIdAndStationIdAndTimeSlot(
Long userId,
Long routeId,
Long stationId,
Integer timeSlot);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.talkka.server.review.dto;

import java.util.Optional;

import com.talkka.server.bus.dao.BusRouteEntity;
import com.talkka.server.bus.dao.BusRouteStationEntity;
import com.talkka.server.review.dao.BusReviewEntity;
import com.talkka.server.user.dao.UserEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@Builder
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class BusReviewReqDto {

private Long routeId;
private Long busRouteStationId;
private Optional<String> content;
private Integer timeSlot;
private Integer rating;

public BusReviewEntity toEntity(UserEntity user, BusRouteStationEntity station, BusRouteEntity route) {
return BusReviewEntity.builder()
.content(content.orElse(null))
.timeSlot(timeSlot)
.rating(rating)
.writer(user)
.station(station)
.route(route)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.talkka.server.review.dto;

import java.util.Optional;

import com.talkka.server.review.dao.BusReviewEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@Builder
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class BusReviewRespDto {

private Long userId;
private Long routeId;
private Long busRouteStationId;
private Optional<String> content;
private Integer timeSlot;
private Integer rating;

public static BusReviewRespDto of(BusReviewEntity busEntity) {
return BusReviewRespDto.builder()
.userId(busEntity.getWriter().getUserId())
.routeId(busEntity.getRoute().getRouteId())
.busRouteStationId(busEntity.getStation().getBusRouteStationId())
.content(Optional.ofNullable(busEntity.getContent()))
.timeSlot(busEntity.getTimeSlot())
.rating(busEntity.getRating())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.talkka.server.review.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.talkka.server.bus.dao.BusRouteEntity;
import com.talkka.server.bus.dao.BusRouteRepository;
import com.talkka.server.bus.dao.BusRouteStationEntity;
import com.talkka.server.bus.dao.BusRouteStationRepository;
import com.talkka.server.common.exception.http.NotFoundException;
import com.talkka.server.review.dao.BusReviewEntity;
import com.talkka.server.review.dao.BusReviewRepository;
import com.talkka.server.review.dto.BusReviewReqDto;
import com.talkka.server.review.dto.BusReviewRespDto;
import com.talkka.server.user.dao.UserEntity;
import com.talkka.server.user.dao.UserRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class BusReviewService {

private final BusReviewRepository busReviewRepository;
private final UserRepository userRepository;
private final BusRouteStationRepository busRouteStationRepository;
private final BusRouteRepository busRouteRepository;

public List<BusReviewRespDto> getBusReviewList(
Long userId, Long routeId, Long stationId, Integer timeSlot
) {
Optional<List<BusReviewEntity>> optionalList = busReviewRepository.findAllByUserIdAndRouteIdAndStationIdAndTimeSlot(
userId, routeId, stationId, timeSlot);

List<BusReviewEntity> list = optionalList.orElseGet(ArrayList::new);

return list.stream()
.map(BusReviewRespDto::of)
.collect(Collectors.toList());
}

public BusReviewRespDto createBusReview(Long userId, BusReviewReqDto busReviewReqDto) {
final UserEntity user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 유저입니다."));

final BusRouteStationEntity station = busRouteStationRepository.findById(busReviewReqDto.getBusRouteStationId())
.orElseThrow(() -> new NotFoundException("존재하지 않는 경유 정류장입니다."));

final BusRouteEntity route = busRouteRepository.findById(busReviewReqDto.getRouteId())
.orElseThrow(() -> new NotFoundException("존재하지 않는 노선입니다."));

final BusReviewEntity entity = busReviewReqDto.toEntity(user, station, route);

BusReviewEntity savedReview = busReviewRepository.save(entity);

return BusReviewRespDto.of(savedReview);
}

public BusReviewRespDto updateBusReview(Long busReviewId, BusReviewReqDto busReviewReqDto) {
final BusReviewEntity review = busReviewRepository.findById(busReviewId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 리뷰입니다."));

review.setContent(busReviewReqDto.getContent()
.orElse(null));
review.setTimeSlot(busReviewReqDto.getTimeSlot());
review.setRating(busReviewReqDto.getRating());

BusReviewEntity updatedReview = busReviewRepository.save(review);

return BusReviewRespDto.of(updatedReview);
}

public void deleteBusReview(Long busReviewId) {
busReviewRepository.deleteById(busReviewId);
}
}
Loading

0 comments on commit e7278a6

Please sign in to comment.