Skip to content

Commit

Permalink
#11 [Fix] 리뷰, 태그 DTO 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 13, 2022
1 parent ac7147e commit 50fabbd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 241 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.mpnp.baechelin.review.controller;

import com.mpnp.baechelin.review.dto.ReviewMainResDto;
import com.mpnp.baechelin.review.dto.ReviewReqDTO;
import com.mpnp.baechelin.review.dto.ReviewResDTO;
import com.mpnp.baechelin.review.dto.ReviewMainResponseDto;
import com.mpnp.baechelin.review.dto.ReviewRequestDto;
import com.mpnp.baechelin.review.repository.ReviewQueryRepository;
import com.mpnp.baechelin.review.service.ReviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -28,37 +27,25 @@ public class ReviewController {
* 리뷰 작성
*/
@PostMapping("/review")
public ReviewResDTO review(@RequestParam double point,
@RequestParam String comment,
@RequestParam int storeId,
@RequestParam int userId,
@RequestParam(required = false) MultipartFile imageFile) throws IOException {

ReviewReqDTO reviewReqDTO = ReviewReqDTO.builder()
.point(point)
.comment(comment)
.storeId(storeId)
.userId(userId)
.imageFile(imageFile)
.build();

reviewService.review(reviewReqDTO);
return null;
public ResponseEntity<?> review(@ModelAttribute ReviewRequestDto reviewRequestDto,
@AuthenticationPrincipal User user) throws IOException {
reviewService.review(reviewRequestDto, user.getUsername());
return new ResponseEntity<>(HttpStatus.OK);
}

// TODO - 최근 등록한 리뷰 보여주기
// 반경 넓히기
@GetMapping("/recent-review")
public List<ReviewMainResDto> recentReview(@RequestParam(required = false) BigDecimal lat,
@RequestParam(required = false) BigDecimal lng,
@RequestParam int limit) {
public List<ReviewMainResponseDto> recentReview(@RequestParam(required = false) BigDecimal lat,
@RequestParam(required = false) BigDecimal lng,
@RequestParam int limit) {

BigDecimal[] locationRange = getRange(lat, lng, 20);


List<ReviewMainResDto> result = reviewQueryRepository
List<ReviewMainResponseDto> result = reviewQueryRepository
.findRecentReviews(locationRange[0], locationRange[1], locationRange[2], locationRange[3], limit)
.stream().map(ReviewMainResDto::new).collect(Collectors.toList());
.stream().map(ReviewMainResponseDto::new).collect(Collectors.toList());
return result;
}

Expand Down
39 changes: 12 additions & 27 deletions src/main/java/com/mpnp/baechelin/review/domain/Review.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.mpnp.baechelin.review.domain;

import com.mpnp.baechelin.review.dto.ReviewReqDTO;
import com.mpnp.baechelin.review.service.ReviewService;
import com.mpnp.baechelin.review.dto.ReviewRequestDto;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.tag.domain.Tag;
import com.mpnp.baechelin.user.entity.user.User;
import com.mpnp.baechelin.user.domain.User;
import com.mpnp.baechelin.util.TimeStamped;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -13,6 +12,7 @@

import javax.persistence.*;
import java.io.IOException;
import java.util.List;


@Entity
Expand All @@ -35,7 +35,7 @@ public class Review extends TimeStamped {
private double point;

//리뷰 이미지 URL
@Column(nullable = true)
@Column
private String reviewImageUrl;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -46,31 +46,16 @@ public class Review extends TimeStamped {
@JoinColumn(name = "USER_ID", nullable = false)
private User userId;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TAG_ID", nullable = false)
private Tag tagId;
@OneToMany(mappedBy = "reviewId", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Tag> tagList;

@Builder
public Review (ReviewReqDTO reviewReqDTO, Store store, Tag tag, User user, String url) throws IOException {

if(reviewReqDTO.getImageFile() != null) { //이미지 파일이 있을 경우
System.out.println("reviewReqDTO.getImageFile() != null");
ReviewService reviewService = null;
this.point = reviewReqDTO.getPoint();
this.review = reviewReqDTO.getComment();
this.reviewImageUrl = url;
this.storeId = store;
this.tagId = tag;
this.userId = user;

} else if(reviewReqDTO.getImageFile() == null) { //이미지 파일이 없을 경우
System.out.println("reviewReqDTO.getImageFile() == null");
this.point = reviewReqDTO.getPoint();
this.review = reviewReqDTO.getComment();
this.storeId = store;
this.tagId = tag;
this.userId = user;
}
public Review(ReviewRequestDto reviewRequestDto, Store store, User user, String url) throws IOException {
this.point = reviewRequestDto.getPoint();
this.review = reviewRequestDto.getComment();
this.reviewImageUrl = url;
this.storeId = store;
this.userId = user;
}

}
102 changes: 20 additions & 82 deletions src/main/java/com/mpnp/baechelin/review/service/ReviewService.java
Original file line number Diff line number Diff line change
@@ -1,118 +1,56 @@
package com.mpnp.baechelin.review.service;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.mpnp.baechelin.review.domain.Review;
import com.mpnp.baechelin.review.dto.ReviewReqDTO;
import com.mpnp.baechelin.review.dto.ReviewRequestDto;
import com.mpnp.baechelin.review.repository.ReviewRepository;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.repository.StoreRepository;
import com.mpnp.baechelin.tag.domain.Tag;
import com.mpnp.baechelin.tag.repository.TagRepository;
import com.mpnp.baechelin.user.entity.user.User;
import com.mpnp.baechelin.user.domain.User;
import com.mpnp.baechelin.user.repository.UserRepository;
import com.mpnp.baechelin.util.AwsS3Manager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class ReviewService {
private final ReviewRepository reviewRepository;
private final ReviewRepository reviewRepository;
private final UserRepository userRepository;
private final StoreRepository storeRepository;
private final TagRepository tagRepository;
private final TagRepository tagRepository;
private final AwsS3Manager awsS3Manager;

/** 리뷰작성 */
public void review(ReviewReqDTO reviewReqDTO) throws IOException {
Tag tag = new Tag(reviewReqDTO); //리뷰 태그
int storeId = reviewReqDTO.getStoreId(); //리뷰 업장id
int userId = reviewReqDTO.getUserId(); //리뷰 유저id
/**
* 리뷰작성
*/
public void review(ReviewRequestDto reviewRequestDto, String socialId) throws IOException {

tag = tagRepository.save(tag);
Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당하는 업장이 존재하지 않습니다."));
User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("해당하는 유저가 존재하지 않습니다."));
int storeId = reviewRequestDto.getStoreId(); //리뷰 업장id
Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당하는 업장이 존재하지 않습니다."));
User user = userRepository.findBySocialId(socialId);


String reviewImageUrl = awsS3Manager.uploadFile(reviewReqDTO.getImageFile());
Review review = new Review(reviewReqDTO, store, tag, user, reviewImageUrl);
String reviewImageUrl = awsS3Manager.uploadFile(reviewRequestDto.getImageFile());
Review review = new Review(reviewRequestDto, store, user, reviewImageUrl);
reviewRepository.save(review);
storeRepository.save(store.updatePointAvg(reviewReqDTO.getPoint()));

// if(reviewReqDTO.getImageFile() != null) { //이미지 파일이 있을 경우
// Review review = new Review(reviewReqDTO, store, tag, user,
// upload(reviewReqDTO.getImageFile(), "/Volumes/Data/MY_PROJECT/HangHae99/bae-sulin/src/main/java/com/mpnp/baechelin/fileSample"));
// reviewRepository.save(review);
// storeRepository.save(store.updatePointAvg(reviewReqDTO.getPoint())); // Store 별점 업데이트
// } else if(reviewReqDTO.getImageFile() == null) { //이미지 파일이 없을 경우
// Review review = new Review(reviewReqDTO, store, tag, user,"");
// reviewRepository.save(review);
// storeRepository.save(store.updatePointAvg(reviewReqDTO.getPoint())); // Store 별점 업데이트
// }
storeRepository.save(store.updatePointAvg(reviewRequestDto.getPoint()));

List<Tag> tagList = new ArrayList<>();
for (String s : reviewRequestDto.getTagList()) {
// 확인해보기! - review
tagList.add(Tag.builder().reviewId(review).tag(s).build()); //리뷰 태그
}
tagRepository.saveAll(tagList);

}


// @Value("${cloud.aws.s3.bucket}")
// public String bucket; // S3 버킷 이름
//
// public String upload(MultipartFile multipartFile, String dirName) throws IOException {
// File uploadFile = convert(multipartFile) // 파일 변환할 수 없으면 에러
// .orElseThrow(() -> new IllegalArgumentException("error: MultipartFile -> File convert fail"));
//
// return upload(uploadFile, dirName);
// }
//
// // S3로 파일 업로드하기
// private String upload(File uploadFile, String dirName) {
// String fileName = dirName + "/" + UUID.randomUUID() + uploadFile.getName(); // S3에 저장된 파일 이름
// String uploadImageUrl = putS3(uploadFile, fileName); // s3로 업로드
// removeNewFile(uploadFile);
// return uploadImageUrl;
// }
//
// // S3로 업로드
// private String putS3(File uploadFile, String fileName) {
// amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, uploadFile).withCannedAcl(CannedAccessControlList.PublicRead));
// return amazonS3Client.getUrl(bucket, fileName).toString();
// }
//
// // 로컬에 저장된 이미지 지우기
// private void removeNewFile(File targetFile) {
// if (targetFile.delete()) {
// log.info("File delete success");
// return;
// }
// log.info("File delete fail");
// }
//
// // 로컬에 파일 업로드 하기
// private Optional<File> convert(MultipartFile file) throws IOException {
// File convertFile = new File(System.getProperty("user.dir") + "/" + file.getOriginalFilename());
// if (convertFile.createNewFile()) { // 바로 위에서 지정한 경로에 File이 생성됨 (경로가 잘못되었다면 생성 불가능)
// try (FileOutputStream fos = new FileOutputStream(convertFile)) { // FileOutputStream 데이터를 파일에 바이트 스트림으로 저장하기 위함
// fos.write(file.getBytes());
// }
// return Optional.of(convertFile);
// }
//
// return Optional.empty();
// }
}
80 changes: 11 additions & 69 deletions src/main/java/com/mpnp/baechelin/tag/domain/Tag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mpnp.baechelin.tag.domain;

import com.mpnp.baechelin.review.dto.ReviewReqDTO;
import com.mpnp.baechelin.review.domain.Review;
import com.mpnp.baechelin.util.TimeStamped;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -14,78 +15,19 @@
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class Tag {
public class Tag extends TimeStamped {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
//배리어프리 키오스크
@Column(nullable = false)
private char bKiosk;
@GeneratedValue(strategy = GenerationType.AUTO)
int id;

//입식 테이블
@Column(nullable = false)
private char bTable;
@Column
String tag;
//jpa를 리스트/JSON 집어넣는 방법?

//점자 매뉴판
@Column(nullable = false)
private char bMenu;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REVIEW_ID", nullable = false)
private Review reviewId;

//휠체어가 들어갈 수 있는
@Column(nullable = false)
private char bWheelchair;

//직원에 도움 요청
@Column(nullable = false)
private char bHelp;

//자동문
@Column(nullable = false)
private char bAutoDoor;

//음식이 맛있는
@Column(nullable = false)
private char fDelicious;

//매장이 깔끔한
@Column(nullable = false)
private char fClean;

//분위기 좋은
@Column(nullable = false)
private char fVibe;

//양이 많은
@Column(nullable = false)
private char fQuantity;

//먹기 편한
@Column(nullable = false)
private char fGoodToEat;

//가격이 착한
@Column(nullable = false)
private char fPrice;

// @OneToOne(fetch = FetchType.LAZY)
// @JoinColumn(name = "REVIEW_ID", nullable = false)
// private Review reviewId;

@Builder
public Tag(ReviewReqDTO reviewReqDTO){
this.bKiosk = reviewReqDTO.getBKiosk(); //키오스크 유무
this.bTable = reviewReqDTO.getBTable(); //입식 테이블
this.bWheelchair = reviewReqDTO.getBWheelchair(); //휠체어가 들어갈 수 있는
this.bMenu = reviewReqDTO.getBMenu(); //점자 매뉴판
this.bHelp = reviewReqDTO.getBHelp(); //직원에 도움 요청
this.bAutoDoor = reviewReqDTO.getBAutoDoor(); //자동문
this.fDelicious = reviewReqDTO.getFDelicious(); //음식이 맛있는
this.fClean = reviewReqDTO.getFClean(); //매장이 깔끔한
this.fVibe = reviewReqDTO.getFVibe(); //분위기 좋은
this.fQuantity = reviewReqDTO.getFQuantity(); //양이 많은
this.fGoodToEat = reviewReqDTO.getFGoodToEat(); //먹기 편한
this.fPrice = reviewReqDTO.getFPrice(); //가격이 착한

}

}
Loading

0 comments on commit 50fabbd

Please sign in to comment.