Skip to content

Commit

Permalink
Merge pull request #18 from syyling/feat/13
Browse files Browse the repository at this point in the history
[feat] 보고싶어요 도메인 추가, 생성/취소 기능 추가
  • Loading branch information
syyling authored Mar 18, 2024
2 parents fc70c3a + 671379a commit 8d09b56
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Heart, Long> {

void deleteByUserIdAndMovieId(Long userId, Long movieId);

boolean existsByUserIdAndMovieId(Long userId, Long movieId);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 8d09b56

Please sign in to comment.