Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into dev
  • Loading branch information
dltjdgh0428 committed Mar 27, 2024
2 parents a24d659 + e4c9221 commit 77e6d10
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 77 deletions.
46 changes: 46 additions & 0 deletions src/main/java/com/book_everywhere/likes/entity/Likes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.book_everywhere.likes.entity;

import com.book_everywhere.auth.entity.User;
import com.book_everywhere.review.entity.Review;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.Fetch;

import java.sql.Timestamp;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Entity
@Table(
name="likes",
uniqueConstraints={
@UniqueConstraint(
name = "likes_uk",
columnNames={"review_id", "user_id"}
)
}
)
public class Likes {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "review_id")
private Review review;

@JsonIgnoreProperties({"reviews"})
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@CreationTimestamp
private Timestamp createAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.book_everywhere.likes.repository;

import com.book_everywhere.likes.entity.Likes;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;


public interface LikesRepository extends JpaRepository<Likes, Long> {

@Modifying
@Query(value = "INSERT INTO likes(review_id, user_id) VALUES(:reviewId, :userId)", nativeQuery = true)
void mLike(@Param("userId") Long userId, @Param("reviewId") Long reviewId);

@Modifying
@Query(value = "DELETE FROM likes WHERE review_id = :reviewId AND user_id = :userId", nativeQuery = true)
void mUnLike(@Param("userId") Long userId, @Param("reviewId") Long reviewId);

Long countByReviewId(@Param("reviewId") Long reviewId);

boolean existsByUserIdAndReviewId(Long userId, Long reviewId);

}
11 changes: 11 additions & 0 deletions src/main/java/com/book_everywhere/likes/service/LikesService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.book_everywhere.likes.service;

public interface LikesService {


void 좋아요(Long socialId,Long review_id);

void 좋아요취소(Long socialId,Long review_id);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.book_everywhere.likes.service;

import com.book_everywhere.auth.entity.User;
import com.book_everywhere.auth.repository.UserRepository;
import com.book_everywhere.likes.entity.Likes;
import com.book_everywhere.likes.repository.LikesRepository;
import com.book_everywhere.review.entity.Review;
import com.book_everywhere.review.repository.ReviewRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@RequiredArgsConstructor
@Transactional
@Service
public class LikesServiceImpl implements LikesService {

private final LikesRepository likesRepository;
private final UserRepository userRepository;

@Override
public void 좋아요(Long socialId, Long review_id) {
User user = userRepository.findBySocialId(socialId).orElseThrow();
likesRepository.mLike(user.getId(),review_id);
}

@Override
public void 좋아요취소(Long socialId, Long review_id) {
User user = userRepository.findBySocialId(socialId).orElseThrow();
likesRepository.mUnLike(user.getId(), review_id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CMRespDto<?> allPublicPinWithTagCount(@PathVariable boolean isPrivate) {
//핀을 눌렀을때 핀에 해당하는 독후감 정보 조회
@GetMapping("/api/pin/{pinId}")
public CMRespDto<?> pinDetails(@PathVariable Long pinId, @AuthenticationPrincipal OAuth2User oAuth2User) {
List<ReviewDto> result = reviewService.단일핀독후감조회(pinId, oAuth2User);
List<ReviewDto> result = reviewService.단일핀독후감조회((Long) oAuth2User.getAttributes().get("id"),pinId);
return new CMRespDto<>(HttpStatus.OK, result,"단일 핀 종속 독후감 조회 성공!");
}

Expand All @@ -63,7 +63,7 @@ public CMRespDto<?> taggedPin(@PathVariable String content) {

@GetMapping("/api/mypage/notyet")
public CMRespDto<?> userReview(@AuthenticationPrincipal OAuth2User oAuth2User) {
List<ReviewDto> result = reviewService.유저모든독후감조회(oAuth2User);
List<ReviewDto> result = reviewService.유저모든독후감조회((Long) oAuth2User.getAttributes().get("id"));
return new CMRespDto<>(HttpStatus.OK, result,"모든 독후감 조회 성공!");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.book_everywhere.book.service.BookService;
import com.book_everywhere.likes.service.LikesService;
import com.book_everywhere.pin.service.PinService;
import com.book_everywhere.pin.service.VisitService;
import com.book_everywhere.review.service.ReviewService;
Expand All @@ -16,6 +17,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -30,6 +33,7 @@ public class ReviewController {
private final VisitService visitService;
private final BookService bookService;
private final ReviewService reviewService;
private final LikesService likesService;

@PostMapping("/api/write")
@Operation(summary = "독후감 추가", description = "독후감을 새로 추가합니다.")
Expand All @@ -55,21 +59,27 @@ public CMRespDto<?> addReview(@RequestBody ReviewRespDto reviewRespDto) {
//조회
//공개 독후감 조회
@GetMapping("/api/reviews")
public CMRespDto<?> publicReviews(){
public CMRespDto<?> publicReviews() {
List<ReviewDto> result = reviewService.모든독후감조회();
return new CMRespDto<>(HttpStatus.OK, result, "전체 공유 독후감 조회");
}
@GetMapping("/api/review/public")
public CMRespDto<?> findPublicReviews() {
List<ReviewDto> result = reviewService.모든공유독후감조회();
return new CMRespDto<>(HttpStatus.OK, result, "모든 공유 독후감 조회 완료");
}


@GetMapping("/api/detail/{bookId}")
public CMRespDto<?> bookReviews(@PathVariable Long bookId) {
List<ReviewDto> result = reviewService.책에따른모든리뷰(bookId);
public CMRespDto<?> bookReviews(@AuthenticationPrincipal OAuth2User oAuth2User,@PathVariable Long bookId) {
List<ReviewDto> result = reviewService.책에따른모든리뷰((Long) oAuth2User.getAttributes().get("id"),bookId);
return new CMRespDto<>(HttpStatus.OK, result, "책에 따른 전체 독후감 조회");
}

//수정
@GetMapping("/api/review/{reviewId}")
public CMRespDto<?> getReview(@PathVariable Long reviewId) {
reviewService.단일독후감조회(reviewId);
public CMRespDto<?> getReview(@AuthenticationPrincipal OAuth2User oAuth2User,@PathVariable Long reviewId) {
reviewService.단일독후감조회((Long) oAuth2User.getAttributes().get("id"),reviewId);
return new CMRespDto<>(HttpStatus.OK, null, "단일 독후감 조회");
}

Expand All @@ -91,12 +101,6 @@ public CMRespDto<?> updateReview(@PathVariable Long reviewId,
return new CMRespDto<>(HttpStatus.OK, null, "독후감 수정 완료");
}

@GetMapping("/api/review/public")
public CMRespDto<?> findPublicReviews() {
List<ReviewDto> result = reviewService.모든공유독후감조회();
return new CMRespDto<>(HttpStatus.OK, result, "모든 공유 독후감 조회 완료");
}

@DeleteMapping("/api/review/delete/{reviewId}")
public CMRespDto<?> deleteReview(@PathVariable Long reviewId,
@RequestParam Long socialId,
Expand All @@ -108,4 +112,18 @@ public CMRespDto<?> deleteReview(@PathVariable Long reviewId,
reviewService.독후감개수검증후핀삭제(address, socialId);
return new CMRespDto<>(HttpStatus.OK, null, "독후감 삭제 완료");
}

// 좋아요 구현

@PostMapping("/api/review/{reviewId}/likes")
public CMRespDto<?> like(@AuthenticationPrincipal OAuth2User oAuth2User, @PathVariable Long reviewId) {
likesService.좋아요((Long) oAuth2User.getAttributes().get("id"),reviewId);
return new CMRespDto<>(HttpStatus.OK, null ,"좋아요 등록 완료!");
}

@DeleteMapping("/api/review/{reviewId}/likes")
public CMRespDto<?> unLike(@AuthenticationPrincipal OAuth2User oAuth2User, @PathVariable Long reviewId) {
likesService.좋아요취소((Long) oAuth2User.getAttributes().get("id"),reviewId);
return new CMRespDto<>(HttpStatus.OK, null ,"좋아요 취소 완료!");
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/book_everywhere/review/dto/ReviewDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@ public class ReviewDto {
private String title;
private String content;
private boolean isPrivate;
private Long likeCount;
private boolean likeState;
private Timestamp createdAt;
private Timestamp updatedAt;


public static ReviewDto toDto(Review review, Long likeCount, boolean likeState) {
return new ReviewDto(
review.getId(),
review.getWriter(),
review.getTitle(),
review.getContent(),
review.isPrivate(),
likeCount,
likeState,
review.getCreateAt(),
review.getUpdateAt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.book_everywhere.pin.dto.PinRespDto;
import com.book_everywhere.pin.entity.Pin;
import com.book_everywhere.review.entity.Review;
import jakarta.persistence.Transient;
import lombok.AllArgsConstructor;
import lombok.Data;

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/book_everywhere/review/entity/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ public class Review {
@Column(nullable = false)
private boolean isBookComplete;


@Transient // 칼럼이 만들어지지 않는다.
private Long likeCount;

@Transient
private boolean likeState;

@CreationTimestamp
private Timestamp createAt;

@UpdateTimestamp
private Timestamp updateAt;



//==연관 관계 편의 메서드==//
public void setBook(Book book) {
if(this.book != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
public interface ReviewService {
Long 독후감생성(ReviewRespDto reviewRespDto);

List<ReviewDto> 책에따른모든리뷰(Long bookId);
List<ReviewDto> 책에따른모든리뷰(Long socialId, Long bookId);

List<ReviewDto> 모든독후감조회();

List<ReviewDto> 모든공유독후감조회();

void 등록또는수정전예외처리(ReviewRespDto reviewRespDto);

ReviewDto 단일독후감조회(Long reviewId);
ReviewDto 단일독후감조회(Long socialId, Long reviewId);

void 독후감수정(Long reviewId, ReviewRespDto reviewRespDto);

Expand All @@ -25,11 +27,11 @@ public interface ReviewService {

void 독후감개수검증후핀삭제(String prevAddress, Long socialId);

List<ReviewDto> 모든공유독후감조회();

void 독후감삭제(Long reviewId);

List<ReviewDto> 유저모든독후감조회(OAuth2User oAuth2User);
List<ReviewDto> 유저모든독후감조회(Long socialId);

List<ReviewDto> 단일핀독후감조회(Long socialId, Long pinId);


List<ReviewDto> 단일핀독후감조회(Long pinId, OAuth2User oAuth2User);
}
Loading

0 comments on commit 77e6d10

Please sign in to comment.