From bc7c118e353b8e1418056fdf760ad1786f50040e Mon Sep 17 00:00:00 2001 From: syyling Date: Mon, 18 Mar 2024 19:59:42 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[feat]=20=EB=B3=B4=EA=B3=A0=EC=8B=B6?= =?UTF-8?q?=EC=96=B4=EC=9A=94=20=EC=97=94=ED=8B=B0=ED=8B=B0,=20repository?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../heart/domain/entity/Heart.java | 32 +++++++++++++++++++ .../domain/repository/HeartRepository.java | 11 +++++++ 2 files changed, 43 insertions(+) create mode 100644 src/main/java/com/mookive/mookive_backend/heart/domain/entity/Heart.java create mode 100644 src/main/java/com/mookive/mookive_backend/heart/domain/repository/HeartRepository.java diff --git a/src/main/java/com/mookive/mookive_backend/heart/domain/entity/Heart.java b/src/main/java/com/mookive/mookive_backend/heart/domain/entity/Heart.java new file mode 100644 index 0000000..3af5d90 --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/heart/domain/entity/Heart.java @@ -0,0 +1,32 @@ +package com.mookive.mookive_backend.heart.domain.entity; + +import com.mookive.mookive_backend.movie.domain.entity.Movie; +import com.mookive.mookive_backend.user.domain.entity.User; +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Heart { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name = "movie_id") + private Movie movie; + + @ManyToOne + @JoinColumn(name = "user_id") + private User user; + + @Builder + public Heart(Movie movie, User user) { + this.movie = movie; + this.user = user; + } +} diff --git a/src/main/java/com/mookive/mookive_backend/heart/domain/repository/HeartRepository.java b/src/main/java/com/mookive/mookive_backend/heart/domain/repository/HeartRepository.java new file mode 100644 index 0000000..88a320c --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/heart/domain/repository/HeartRepository.java @@ -0,0 +1,11 @@ +package com.mookive.mookive_backend.heart.domain.repository; + +import com.mookive.mookive_backend.heart.domain.entity.Heart; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface HeartRepository extends JpaRepository { + + void deleteByUserIdAndMovieId(Long userId, Long movieId); + + boolean existsByUserIdAndMovieId(Long userId, Long movieId); +} From 671379ad4097f6eb8ade3be26ab62386298e50c4 Mon Sep 17 00:00:00 2001 From: syyling Date: Mon, 18 Mar 2024 20:00:04 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[feat]=20=EB=B3=B4=EA=B3=A0=EC=8B=B6?= =?UTF-8?q?=EC=96=B4=EC=9A=94=20=EC=83=9D=EC=84=B1,=EC=B7=A8=EC=86=8C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/HeartCreateService.java | 39 +++++++++++++++++++ .../domain/service/HeartDeleteService.java | 19 +++++++++ .../domain/service/HeartQueryService.java | 19 +++++++++ .../domain/service/HeartSaveService.java | 20 ++++++++++ .../heart/presentation/HeartController.java | 19 +++++++++ 5 files changed, 116 insertions(+) create mode 100644 src/main/java/com/mookive/mookive_backend/heart/application/service/HeartCreateService.java create mode 100644 src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartDeleteService.java create mode 100644 src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartQueryService.java create mode 100644 src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartSaveService.java create mode 100644 src/main/java/com/mookive/mookive_backend/heart/presentation/HeartController.java diff --git a/src/main/java/com/mookive/mookive_backend/heart/application/service/HeartCreateService.java b/src/main/java/com/mookive/mookive_backend/heart/application/service/HeartCreateService.java new file mode 100644 index 0000000..ed144bc --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/heart/application/service/HeartCreateService.java @@ -0,0 +1,39 @@ +package com.mookive.mookive_backend.heart.application.service; + +import com.mookive.mookive_backend.heart.domain.entity.Heart; +import com.mookive.mookive_backend.heart.domain.service.HeartDeleteService; +import com.mookive.mookive_backend.heart.domain.service.HeartQueryService; +import com.mookive.mookive_backend.heart.domain.service.HeartSaveService; +import com.mookive.mookive_backend.movie.domain.entity.Movie; +import com.mookive.mookive_backend.movie.domain.service.MovieQueryService; +import com.mookive.mookive_backend.user.domain.entity.User; +import com.mookive.mookive_backend.user.domain.service.UserQueryService; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +public class HeartCreateService { + + private final HeartQueryService heartQueryService; + private final HeartSaveService heartSaveService; + private final HeartDeleteService heartDeleteService; + private final UserQueryService userQueryService; + private final MovieQueryService movieQueryService; + + public void createHeart(Long userId, Long movieId) { + if (heartQueryService.existsByUserIdAndMovieId(userId, movieId)) { + heartDeleteService.deleteByUserIdAndMovieId(userId, movieId); + } else { + User user = userQueryService.findById(userId); + Movie movie = movieQueryService.findById(movieId); + Heart heart = Heart.builder() + .user(user) + .movie(movie) + .build(); + heartSaveService.save(heart); + } + } +} diff --git a/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartDeleteService.java b/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartDeleteService.java new file mode 100644 index 0000000..a218a26 --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartDeleteService.java @@ -0,0 +1,19 @@ +package com.mookive.mookive_backend.heart.domain.service; + + +import com.mookive.mookive_backend.heart.domain.repository.HeartRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +public class HeartDeleteService { + + private final HeartRepository heartRepository; + + public void deleteByUserIdAndMovieId(Long userId, Long movieId) { + heartRepository.deleteByUserIdAndMovieId(userId, movieId); + } +} diff --git a/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartQueryService.java b/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartQueryService.java new file mode 100644 index 0000000..4592d95 --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartQueryService.java @@ -0,0 +1,19 @@ +package com.mookive.mookive_backend.heart.domain.service; + + +import com.mookive.mookive_backend.heart.domain.repository.HeartRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +public class HeartQueryService { + + private final HeartRepository heartRepository; + + public boolean existsByUserIdAndMovieId(Long userId, Long movieId) { + return heartRepository.existsByUserIdAndMovieId(userId, movieId); + } +} diff --git a/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartSaveService.java b/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartSaveService.java new file mode 100644 index 0000000..94440b4 --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/heart/domain/service/HeartSaveService.java @@ -0,0 +1,20 @@ +package com.mookive.mookive_backend.heart.domain.service; + +import com.mookive.mookive_backend.heart.domain.entity.Heart; +import com.mookive.mookive_backend.heart.domain.repository.HeartRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +public class HeartSaveService { + + private final HeartRepository heartRepository; + + public void save(Heart heart) { + heartRepository.save(heart); + } + +} diff --git a/src/main/java/com/mookive/mookive_backend/heart/presentation/HeartController.java b/src/main/java/com/mookive/mookive_backend/heart/presentation/HeartController.java new file mode 100644 index 0000000..7075e5b --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/heart/presentation/HeartController.java @@ -0,0 +1,19 @@ +package com.mookive.mookive_backend.heart.presentation; + +import com.mookive.mookive_backend.heart.application.service.HeartCreateService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class HeartController { + + private final HeartCreateService heartCreateService; + + @PostMapping("/heart") + public void createHeart(@RequestParam Long userId, @RequestParam Long movieId) { + heartCreateService.createHeart(userId, movieId); + } +}