-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KKUMI-118 [FEATURE] #73 포스트 좋아요 기능 추가
좋아요 누르는 기능만 추가
- Loading branch information
Showing
8 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/swmarastro/mykkumiserver/like/Likes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.swmarastro.mykkumiserver.like; | ||
|
||
import com.swmarastro.mykkumiserver.global.BaseEntity; | ||
import com.swmarastro.mykkumiserver.post.domain.Post; | ||
import com.swmarastro.mykkumiserver.user.User; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Builder | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Likes extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "likes_id") | ||
private Long id; | ||
|
||
private Boolean isDeleted; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "post_id") | ||
private Post post; | ||
|
||
@JoinColumn(name = "user_id") | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
private User user; | ||
|
||
public static Likes of(Post post, User user) { | ||
return Likes.builder() | ||
.post(post) | ||
.user(user) | ||
.isDeleted(false) | ||
.build(); | ||
} | ||
|
||
public Boolean like() { | ||
isDeleted = false; | ||
return true; | ||
} | ||
|
||
public Boolean unlike() { | ||
isDeleted = true; | ||
return true; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/swmarastro/mykkumiserver/like/LikesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.swmarastro.mykkumiserver.like; | ||
|
||
import com.swmarastro.mykkumiserver.auth.annotation.Login; | ||
import com.swmarastro.mykkumiserver.auth.annotation.RequiresLogin; | ||
import com.swmarastro.mykkumiserver.like.dto.LikesRequest; | ||
import com.swmarastro.mykkumiserver.like.dto.LikesResponse; | ||
import com.swmarastro.mykkumiserver.user.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1") | ||
@RequiredArgsConstructor | ||
public class LikesController { | ||
|
||
private final LikesService likeService; | ||
|
||
@RequiresLogin | ||
@PostMapping("/likes") | ||
public ResponseEntity<LikesResponse> like(@Login User user, @RequestBody LikesRequest request) { | ||
LikesResponse response = LikesResponse.of(likeService.like(user, request.getPostId())); | ||
return ResponseEntity.ok(response); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/swmarastro/mykkumiserver/like/LikesRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.swmarastro.mykkumiserver.like; | ||
|
||
import com.swmarastro.mykkumiserver.post.domain.Post; | ||
import com.swmarastro.mykkumiserver.user.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.Optional; | ||
|
||
public interface LikesRepository extends JpaRepository<Likes, Long> { | ||
|
||
@Query("SELECT l from Likes l WHERE l.post = :post AND l.user = :user") | ||
Optional<Likes> findByPostAndUser(Post post, User user); //TODO postid, userid로 하는게 나을지??? | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.swmarastro.mykkumiserver.like; | ||
|
||
import com.swmarastro.mykkumiserver.global.exception.CommonException; | ||
import com.swmarastro.mykkumiserver.global.exception.ErrorCode; | ||
import com.swmarastro.mykkumiserver.post.domain.Post; | ||
import com.swmarastro.mykkumiserver.post.service.PostService; | ||
import com.swmarastro.mykkumiserver.user.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class LikesService { | ||
|
||
private final LikesRepository likeRepository; | ||
private final PostService postService; | ||
|
||
public Boolean like(User user, Long postId) { | ||
|
||
Post post = postService.findById(postId); | ||
|
||
//like가 테이블에 있는지 확인 | ||
Optional<Likes> optionalLike = likeRepository.findByPostAndUser(post, user); | ||
//없음 -> 생성하고 좋아요 눌림 | ||
if (optionalLike.isEmpty()) { | ||
Likes like = Likes.of(post, user); | ||
likeRepository.save(like); | ||
return true; | ||
} | ||
Likes like = optionalLike.get(); | ||
//있는데 안눌린 상태 -> 좋아요 눌림 | ||
if (like.getIsDeleted()) { | ||
return like.like(); | ||
} | ||
//있는데 이미 눌린상태 -> 이미 좋아요 누른 포스트입니다. | ||
throw new CommonException(ErrorCode.ALREADY_EXISTS, "이미 좋아요를 누른 포스트입니다.", "이미 좋아요를 누른 포스트입니다."); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/swmarastro/mykkumiserver/like/dto/LikesRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.swmarastro.mykkumiserver.like.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class LikesRequest { | ||
|
||
private Long postId; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/swmarastro/mykkumiserver/like/dto/LikesResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.swmarastro.mykkumiserver.like.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class LikesResponse { | ||
|
||
private Boolean success; | ||
|
||
public static LikesResponse of(Boolean success) { | ||
return LikesResponse.builder() | ||
.success(success) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters