-
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.
- Loading branch information
1 parent
1122cca
commit 51eaaf6
Showing
5 changed files
with
129 additions
and
1 deletion.
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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/webtoonchat/toonchat/domain/like/controller/LikeController.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,34 @@ | ||
package com.webtoonchat.toonchat.domain.like.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.webtoonchat.toonchat.domain.like.service.LikeService; | ||
import com.webtoonchat.toonchat.domain.member.entity.Member; | ||
import com.webtoonchat.toonchat.domain.member.service.MemberService; | ||
import com.webtoonchat.toonchat.resolver.annotation.Login; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import jakarta.validation.constraints.Positive; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/api/likes") | ||
@RequiredArgsConstructor | ||
public class LikeController { | ||
|
||
private final LikeService likeService; | ||
private final MemberService memberService; | ||
|
||
@PostMapping("/{boardId}") | ||
public ResponseEntity addLike(@PathVariable("boardId")@Positive Long boardId, @Login Claims claims) { | ||
Long userId = claims.get("userId", Long.class); | ||
Member member = memberService.findByMemberId(userId); | ||
likeService.addLike(boardId, member); | ||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/webtoonchat/toonchat/domain/like/entity/Like.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,39 @@ | ||
package com.webtoonchat.toonchat.domain.like.entity; | ||
|
||
import com.webtoonchat.toonchat.domain.board.entity.Board; | ||
import com.webtoonchat.toonchat.domain.member.entity.Member; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Table(name = "\"like\"") //이거 이름 안바꿔 주면 충돌남 SQL 예약어라서.. | ||
@Setter | ||
@Getter | ||
@Entity | ||
@NoArgsConstructor | ||
public class Like { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long likeId; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "board_id") | ||
private Board board; | ||
public Like(Member member, Board board) { | ||
this.member = member; | ||
this.board = board; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/webtoonchat/toonchat/domain/like/repository/LikeRepository.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,20 @@ | ||
package com.webtoonchat.toonchat.domain.like.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.webtoonchat.toonchat.domain.board.entity.Board; | ||
import com.webtoonchat.toonchat.domain.like.entity.Like; | ||
import com.webtoonchat.toonchat.domain.member.entity.Member; | ||
|
||
@Repository | ||
public interface LikeRepository extends JpaRepository<Like, Long> { | ||
|
||
//있는지 없는지 검토 | ||
boolean existsByMemberAndBoard(Member member, Board board); | ||
|
||
//삭제 | ||
void deleteByMemberAndBoard(Member member, Board board); | ||
|
||
} | ||
|
32 changes: 32 additions & 0 deletions
32
src/main/java/com/webtoonchat/toonchat/domain/like/service/LikeService.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,32 @@ | ||
package com.webtoonchat.toonchat.domain.like.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.webtoonchat.toonchat.domain.board.entity.Board; | ||
import com.webtoonchat.toonchat.domain.board.service.BoardService; | ||
import com.webtoonchat.toonchat.domain.like.entity.Like; | ||
import com.webtoonchat.toonchat.domain.like.repository.LikeRepository; | ||
import com.webtoonchat.toonchat.domain.member.entity.Member; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class LikeService { | ||
private final BoardService boardService; | ||
private final LikeRepository likeRepository; | ||
|
||
public void addLike(Long boardId, Member member) { | ||
Board board = boardService.findById(boardId); | ||
if (!likeRepository.existsByMemberAndBoard(member, board)) { | ||
board.setLikeCount(board.getLikeCount() + 1); | ||
likeRepository.save(new Like(member, board)); | ||
} else { | ||
board.setLikeCount(board.getLikeCount() - 1); | ||
likeRepository.deleteByMemberAndBoard(member, board); | ||
} | ||
} | ||
} |