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