Skip to content
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
Expand Up @@ -51,9 +51,9 @@ public void handleCommentCreated(CommentCreatedEvent event) {
public void handleLikeCreated(LikeCreatedEvent event) {
try {
handleNotification(
event.postWriterId(),
event.likeWriterName() + "왹이 외계 수집물에 푸 딩을 달았다!",
"/posts/" + event.postId(),
event.receiverId(),
event.senderName() + "왹이 외계 수집물에 푸 딩을 달았다!",
"/posts/" + event.targetId(),
NotificationType.LIKE
);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void handleLikeCreated(LikeCreatedEvent event) {
if (!validateIsToday(event.createdAt())) return;

try {
userStatsService.updateReceivedLikes(event.postWriterId(), 1);
userStatsService.updateReceivedLikes(event.receiverId(), 1);
} catch (Exception e) {
log.warn("좋아요 생성 이벤트 통계 처리 실패: {}", event, e);
}
Expand All @@ -66,7 +66,7 @@ public void handleLikeDeleted(LikeDeletedEvent event) {
if (!validateIsToday(event.createdAt())) return;

try {
userStatsService.updateReceivedLikes(event.postWriterId(), -1);
userStatsService.updateReceivedLikes(event.receiverId(), -1);
} catch (Exception e) {
log.warn("좋아요 삭제 이벤트 통계 처리 실패: {}", event, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.time.LocalDateTime;

public record LikeCreatedEvent(
Long postId, Long postWriterId, Long likeWriterId, String likeWriterName,
Long targetId,
Long receiverId,
Long senderId,
String senderName,
LocalDateTime createdAt) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import java.time.LocalDateTime;

public record LikeDeletedEvent(
Long postId, Long postWriterId, Long likeWriterId, LocalDateTime createdAt) {
Long targetId,
Long receiverId,
Long senderId,
LocalDateTime createdAt) {
}
32 changes: 0 additions & 32 deletions src/main/java/com/jiwon/mylog/domain/like/LikeRepository.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jiwon.mylog.domain.like;
package com.jiwon.mylog.domain.like.controller;

import com.jiwon.mylog.domain.like.service.PostLikeService;
import com.jiwon.mylog.global.common.entity.PageResponse;
import com.jiwon.mylog.global.common.entity.SliceResponse;
import com.jiwon.mylog.global.security.auth.annotation.LoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -21,37 +21,37 @@
@RestController
public class LikeController {

private final LikeService likeService;
private final PostLikeService postLikeService;

@PostMapping("/posts/{postId}/likes")
public ResponseEntity<Void> createLike(@LoginUser Long userId, @PathVariable Long postId) {
likeService.createLike(userId, postId);
public ResponseEntity<Void> likePost(@LoginUser Long userId, @PathVariable Long postId) {
postLikeService.like(userId, postId);
return new ResponseEntity<>(HttpStatus.CREATED);
}

@DeleteMapping("/posts/{postId}/likes")
public ResponseEntity<Void> deleteLike(@LoginUser Long userId, @PathVariable Long postId) {
likeService.deleteLike(userId, postId);
public ResponseEntity<Void> unlikePost(@LoginUser Long userId, @PathVariable Long postId) {
postLikeService.unlike(userId, postId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@GetMapping("/posts/{postId}/likes/count")
public ResponseEntity<Long> getLikeCount(@PathVariable Long postId) {
long likeCount = likeService.getLikeCount(postId);
public ResponseEntity<Long> countPostLike(@PathVariable Long postId) {
long likeCount = postLikeService.countLikes(postId);
return ResponseEntity.ok(likeCount);
}

@GetMapping("/posts/{postId}/likes")
public ResponseEntity<Boolean> getLikeStatus(@LoginUser Long userId, @PathVariable Long postId) {
boolean likeStatus = likeService.getLikeStatus(userId, postId);
public ResponseEntity<Boolean> isLikedPost(@LoginUser Long userId, @PathVariable Long postId) {
boolean likeStatus = postLikeService.isLiked(userId, postId);
return ResponseEntity.ok(likeStatus);
}

@GetMapping("/users/{userId}/likes")
public ResponseEntity<PageResponse> getUserLikes(
@PathVariable Long userId,
@PageableDefault(size = 10, sort="createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
PageResponse response = likeService.getUserLikes(userId, pageable);
PageResponse response = postLikeService.getUserLikes(userId, pageable);
return ResponseEntity.ok(response);
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/jiwon/mylog/domain/like/entity/BaseLike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.jiwon.mylog.domain.like.entity;

import com.jiwon.mylog.domain.user.entity.User;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseLike {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdAt;

BaseLike(User user) {
this.user = user;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.jiwon.mylog.domain.like;
package com.jiwon.mylog.domain.like.entity;

import com.jiwon.mylog.domain.post.entity.Post;
import com.jiwon.mylog.domain.user.entity.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -13,47 +11,35 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.AllArgsConstructor;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(
name = "likes",
name = "post_like",
uniqueConstraints = @UniqueConstraint(
name = "like_uk",
name = "post_like_uk",
columnNames = {"user_id", "post_id"}
)
)
public class Like {

public class PostLike extends BaseLike {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false)
private Post post;

@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdAt;
PostLike(User user, Post post) {
super(user);
this.post = post;
}

public static Like toLike(User user, Post post) {
Like like = new Like();
like.user = user;
like.post = post;
return like;
public static PostLike toPostLike(User user, Post post) {
return new PostLike(user, post);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.jiwon.mylog.domain.like.repository;

import com.jiwon.mylog.domain.like.entity.PostLike;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

@Query("select pl from PostLike pl "
+ "join fetch pl.user "
+ "join fetch pl.post "
+ "where pl.user.id = :userId and pl.post.id = :postId")
Optional<PostLike> findWithDetails(@Param("userId") Long userId, @Param("postId") Long postId);

boolean existsByUserIdAndPostId(Long userId, Long postId);

@Query(value = "select count(*) from post_like where post_id = :postId", nativeQuery = true)
long countByPostId(@Param("postId") Long postId);
}
12 changes: 12 additions & 0 deletions src/main/java/com/jiwon/mylog/domain/like/service/LikeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.jiwon.mylog.domain.like.service;

public interface LikeService {

boolean isLiked(Long userId, Long targetId);

void like(Long userId, Long targetId);

void unlike(Long userId, Long targetId);

Long countLikes(Long targetId);
}
Loading