Skip to content

Commit

Permalink
Merge pull request #66 from LIKELION-TEAM4-HACKATHON/feature/save-review
Browse files Browse the repository at this point in the history
후기 저장 기능
  • Loading branch information
chaeyoungeee authored Jul 30, 2024
2 parents b23c83c + 0d493a6 commit 1dd7440
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 5 deletions.
43 changes: 40 additions & 3 deletions src/main/java/likelion/MZConnent/api/review/ReviewController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package likelion.MZConnent.api.review;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import likelion.MZConnent.dto.paging.response.PageContentResponse;
import likelion.MZConnent.dto.review.request.SaveReviewRequest;
import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse;
import likelion.MZConnent.dto.review.response.SaveReviewResponse;
import likelion.MZConnent.jwt.principle.UserPrinciple;
import likelion.MZConnent.service.review.ReviewService;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand All @@ -23,4 +32,32 @@ ResponseEntity<PageContentResponse> getReviewSimpleList(@RequestParam(required =
PageContentResponse<ReviewsSimpleResponse> response = reviewService.getReviewsSimpleList(keyword, page);
return ResponseEntity.ok(response);
}

@PostMapping(value="/api/reviews/culture/{cultureId}", consumes = {"multipart/form-data"})
public ResponseEntity<SaveReviewResponse> saveReview(
@AuthenticationPrincipal UserPrinciple userPrinciple,
@RequestPart("info") String info,
@RequestPart(value = "reviewImage1", required = true) MultipartFile reviewImage1,
@RequestPart(value = "reviewImage2", required = false) MultipartFile reviewImage2,
@RequestPart(value = "reviewImage3", required = false) MultipartFile reviewImage3,
@RequestPart(value = "reviewImage4", required = false) MultipartFile reviewImage4,
@PathVariable Long cultureId) throws JsonProcessingException {

try {
ObjectMapper objectMapper = new ObjectMapper();
SaveReviewRequest request = objectMapper.readValue(info, SaveReviewRequest.class);

List<MultipartFile> images = new ArrayList<>();
if (reviewImage1 != null) images.add(reviewImage1);
if (reviewImage2 != null) images.add(reviewImage2);
if (reviewImage3 != null) images.add(reviewImage3);
if (reviewImage4 != null) images.add(reviewImage4);

SaveReviewResponse response = reviewService.createReview(userPrinciple.getEmail(), request, images, cultureId);
return ResponseEntity.ok(response);
} catch (IOException e) {
log.error("리뷰 저장 실패", e);
return ResponseEntity.status(500).body(null);
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/likelion/MZConnent/domain/culture/Culture.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Culture {
@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package likelion.MZConnent.dto.review.request;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@Getter
@NoArgsConstructor
public class SaveReviewRequest {
private String title;
private String content;

@Builder
public SaveReviewRequest(String title, String content, List<String> reviewImageUrls) {
this.title = title;
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package likelion.MZConnent.dto.review.response;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
@Getter
@NoArgsConstructor
public class SaveReviewResponse {
private Long reviewId;
private ReviewerDto reviewer;
private CultureDto culture;
private String title;
private String reviewImageUrl1;
private String reviewImageUrl2;
private String reviewImageUrl3;
private String reviewImageUrl4;
private LocalDateTime createdDate;
private int likeCount;

@Builder
public SaveReviewResponse(Long reviewId, ReviewerDto reviewer, CultureDto culture, String title, String reviewImageUrl1, String reviewImageUrl2, String reviewImageUrl3, String reviewImageUrl4, LocalDateTime createdDate, int likeCount) {
this.reviewId = reviewId;
this.reviewer = reviewer;
this.culture = culture;
this.title = title;
this.reviewImageUrl1 = reviewImageUrl1;
this.reviewImageUrl2 = reviewImageUrl2;
this.reviewImageUrl3 = reviewImageUrl3;
this.reviewImageUrl4 = reviewImageUrl4;
this.createdDate = createdDate;
this.likeCount = likeCount;
}

@Getter
@NoArgsConstructor
public static class ReviewerDto {
private Long userId;
private String username;
private String profileImage;

@Builder
public ReviewerDto(Long userId, String username, String profileImage) {
this.userId = userId;
this.username = username;
this.profileImage = profileImage;
}
}

@Getter
@NoArgsConstructor
public static class CultureDto {
private Long cultureId;
private String cultureName;

@Builder
public CultureDto(Long cultureId, String cultureName) {
this.cultureId = cultureId;
this.cultureName = cultureName;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ClubService {
public CreateClubResponse createClub(CreateClubRequest request, Member member) {

Culture culture = cultureRepository.findById(request.getCultureId()).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 문화입니다."));
culture.setClubCount(culture.getClubCount() + 1);
RegionCategory region = regionCategoryRepository.findById(request.getRegionId()).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 지역입니다."));

Club club = Club.builder()
Expand Down
75 changes: 73 additions & 2 deletions src/main/java/likelion/MZConnent/service/review/ReviewService.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package likelion.MZConnent.service.review;

import likelion.MZConnent.domain.culture.Culture;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.review.Review;
import likelion.MZConnent.dto.paging.response.PageContentResponse;
import likelion.MZConnent.dto.review.request.SaveReviewRequest;
import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse;
import likelion.MZConnent.dto.review.response.SaveReviewResponse;
import likelion.MZConnent.repository.culture.CultureRepository;
import likelion.MZConnent.repository.member.MemberRepository;
import likelion.MZConnent.repository.review.ReviewRepository;
import likelion.MZConnent.service.image.S3ImageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
@RequiredArgsConstructor
public class ReviewService {
private final ReviewRepository reviewRepository;
private final S3ImageService s3ImageService;
private final MemberRepository memberRepository;
private final CultureRepository cultureRepository;
private final int PAGE_SIZE = 6;

public PageContentResponse<ReviewsSimpleResponse> getReviewsSimpleList(String keyword, int page) {
Expand All @@ -43,4 +55,63 @@ public PageContentResponse<ReviewsSimpleResponse> getReviewsSimpleList(String ke
.build();

}

public SaveReviewResponse createReview(String email, SaveReviewRequest request, List<MultipartFile> images, Long cultureId) throws IOException {
List<String> imageUrls = uploadImages(images);

Member member = memberRepository.findByEmail(email).orElseThrow(() -> new IllegalArgumentException("회원 정보를 찾을 수 없습니다."));
Culture culture = cultureRepository.findById(cultureId).orElseThrow(() -> new IllegalArgumentException("문화 정보를 찾을 수 없습니다."));

Review review = buildReview(request, imageUrls, member, culture);
review = reviewRepository.save(review);

return buildSaveReviewResponse(review, member, culture);
}

private List<String> uploadImages(List<MultipartFile> images) throws IOException {
List<String> imageUrls = new ArrayList<>();
for (MultipartFile image : images) {
String imageUrl = s3ImageService.upload(image, "reviews");
imageUrls.add(imageUrl);
}
return imageUrls;
}

private Review buildReview(SaveReviewRequest request, List<String> imageUrls, Member member, Culture culture) {
return Review.builder()
.title(request.getTitle())
.content(request.getContent())
.reviewImageUrl1(imageUrls.size() > 0 ? imageUrls.get(0) : null)
.reviewImageUrl2(imageUrls.size() > 1 ? imageUrls.get(1) : null)
.reviewImageUrl3(imageUrls.size() > 2 ? imageUrls.get(2) : null)
.reviewImageUrl4(imageUrls.size() > 3 ? imageUrls.get(3) : null)
.createdDate(LocalDateTime.now())
.likeCount(0)
.commentCount(0)
.culture(culture)
.member(member)
.build();
}

private SaveReviewResponse buildSaveReviewResponse(Review review, Member member, Culture culture) {
return SaveReviewResponse.builder()
.reviewId(review.getReviewId())
.reviewer(SaveReviewResponse.ReviewerDto.builder()
.userId(member.getId())
.username(member.getUsername())
.profileImage(member.getProfileImageUrl())
.build())
.culture(SaveReviewResponse.CultureDto.builder()
.cultureId(culture.getCultureId())
.cultureName(culture.getName())
.build())
.title(review.getTitle())
.reviewImageUrl1(review.getReviewImageUrl1())
.reviewImageUrl2(review.getReviewImageUrl2())
.reviewImageUrl3(review.getReviewImageUrl3())
.reviewImageUrl4(review.getReviewImageUrl4())
.createdDate(review.getCreatedDate())
.likeCount(review.getLikeCount())
.build();
}
}

0 comments on commit 1dd7440

Please sign in to comment.