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 : 리뷰 작성 #45

Merged
merged 4 commits into from
Aug 2, 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
@@ -0,0 +1,32 @@
package com.example.likelion12.controller;
import com.example.likelion12.common.response.BaseResponse;
import com.example.likelion12.dto.PostReviewRequest;
import com.example.likelion12.dto.PostReviewResponse;
import com.example.likelion12.service.ReviewService;
import com.example.likelion12.util.JwtProvider;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/review")
public class ReviewController {

private final ReviewService reviewService;
private final JwtProvider jwtProvider;

@PostMapping
public BaseResponse<PostReviewResponse> createReview(@RequestHeader("Authorization") String authorization , @RequestBody PostReviewRequest postReviewRequest) {

Long memberId = jwtProvider.extractIdFromHeader(authorization);

Long reviewId = reviewService.createReview(postReviewRequest.getFacilityId(),
Copy link
Member

Choose a reason for hiding this comment

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

헤더에 토큰 받는 부분 누락된 것 같습니다!

postReviewRequest.getRanking(),
postReviewRequest.getComment(),
memberId);
PostReviewResponse response = new PostReviewResponse(reviewId);
return new BaseResponse<>(response);
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/example/likelion12/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import static jakarta.persistence.FetchType.LAZY;

Expand All @@ -28,7 +29,7 @@ public class Review extends BaseTime {

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private BaseStatus status;
private BaseStatus status = BaseStatus.ACTIVE;

/** 멤버 와의 연관관계의 주인 */
@ManyToOne(fetch = LAZY)
Expand All @@ -39,4 +40,12 @@ public class Review extends BaseTime {
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "facility_id")
private Facility facility;

public void setReview(Facility facility , int ranking, String comment , Member member)
{
this.facility = facility;
this.ranking = ranking;
this.comment = comment;
this.member = member;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/example/likelion12/dto/PostReviewRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.likelion12.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PostReviewRequest {
private Long facilityId;
private int ranking;
private String comment;
}
10 changes: 10 additions & 0 deletions src/main/java/com/example/likelion12/dto/PostReviewResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.likelion12.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PostReviewResponse {
private Long ReviewId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.likelion12.repository;

import com.example.likelion12.domain.Review;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> {

// 리뷰 작성
// 기본적으로 JpaRepository의 save() 메서드를 사용하여 작성할 수 있습니다.
// 예: reviewRepository.save(review);

// 리뷰 수정
// 기본적으로 JpaRepository의 save() 메서드를 사용하여 수정할 수 있습니다.
// 예: reviewRepository.save(updatedReview);

// 리뷰 삭제
void deleteById(Long reviewId);
}
48 changes: 48 additions & 0 deletions src/main/java/com/example/likelion12/service/ReviewService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.likelion12.service;

import com.example.likelion12.common.exception.MemberException;
import com.example.likelion12.domain.Facility;
import com.example.likelion12.domain.Member;
import com.example.likelion12.domain.Review;
import com.example.likelion12.domain.base.BaseStatus;
import com.example.likelion12.repository.FacilityRepository;
import com.example.likelion12.repository.MemberRepository;
import com.example.likelion12.repository.ReviewRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.example.likelion12.common.response.status.BaseExceptionResponseStatus.CANNOT_FOUND_MEMBER;

@Service
public class ReviewService {

@Autowired
private ReviewRepository reviewRepository;

@Autowired
private FacilityRepository facilityRepository;

@Autowired
private MemberRepository memberRepository;

@Transactional
public Long createReview(Long facilityId, int ranking, String comment , Long memberId) {

Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE)
.orElseThrow(()-> new MemberException(CANNOT_FOUND_MEMBER));

// facilityId로 facility 찾고
Facility facility = facilityRepository.findById(facilityId)
.orElseThrow(() -> new IllegalArgumentException("Invalid facility ID"));

//리뷰에 받아온 값들 넣고
Review review = new Review();
review.setReview(facility, ranking, comment, member);

//레퍼지토리에 저장
Review savedReview = reviewRepository.save(review);

return savedReview.getReviewId();
}
}
Loading