Skip to content

Commit

Permalink
Feat: like 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
resource777 committed Oct 31, 2023
1 parent 1122cca commit 51eaaf6
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import lombok.Setter;

@EntityListeners(AuditingEntityListener.class)
@Getter
@Setter
@NoArgsConstructor
@Entity
public class Board {
Expand Down Expand Up @@ -61,6 +62,8 @@ public class Board {
@OrderBy("id asc") // 댓글 정렬
private List<Comment> comments;

private Long likeCount = 0L;

@Builder // 빌더 패턴으로 객체 생성
public Board(
String writer, String title, String content, Long writerId,
Expand Down
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();
}
}
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;
}
}
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);

}

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);
}
}
}

0 comments on commit 51eaaf6

Please sign in to comment.