-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from NOW-SOPT-CDSP-WEB5/feat/6
[feat] 영화 좋아요 (클릭했을 때) api 구현
- Loading branch information
Showing
6 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
17 changes: 13 additions & 4 deletions
17
src/main/java/org/sopt/cgv/controller/MovieController.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 |
---|---|---|
@@ -1,25 +1,34 @@ | ||
package org.sopt.cgv.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.cgv.service.HeartsService; | ||
import org.sopt.cgv.service.MovieService; | ||
import org.sopt.cgv.service.dto.MovieDetailRequestDto; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1") | ||
public class MovieController implements MovieControllerSwagger { | ||
|
||
private final MovieService movieService; | ||
private final HeartsService likeService; | ||
|
||
@GetMapping("/movies/{movieId}") | ||
public ResponseEntity<MovieDetailRequestDto> getMovieDetail( | ||
@PathVariable Long movieId | ||
) { | ||
return ResponseEntity.ok(movieService.findMovieDetailById(movieId)); | ||
} | ||
|
||
@PostMapping("/{movieId}/hearts") | ||
public ResponseEntity likeMovie( | ||
@PathVariable Long movieId | ||
) { | ||
return ResponseEntity.status(HttpStatus.CREATED) | ||
.header("Location", likeService.addHearts(movieId)) | ||
.build(); | ||
} | ||
} |
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
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,30 @@ | ||
package org.sopt.cgv.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
public class hearts { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@OneToOne | ||
private Movie movie; | ||
|
||
@Builder | ||
private hearts(Movie movie) { | ||
this.movie = movie; | ||
} | ||
|
||
public static hearts create(Movie movie) { | ||
return hearts.builder() | ||
.movie(movie) | ||
.build(); | ||
} | ||
} |
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,7 @@ | ||
package org.sopt.cgv.repository; | ||
|
||
import org.sopt.cgv.domain.hearts; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface HeartsRepository extends JpaRepository<hearts, Long> { | ||
} |
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,27 @@ | ||
package org.sopt.cgv.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.cgv.domain.hearts; | ||
import org.sopt.cgv.domain.Movie; | ||
import org.sopt.cgv.repository.HeartsRepository; | ||
import org.sopt.cgv.repository.MovieRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class HeartsService { | ||
|
||
private final HeartsRepository likeRepository; | ||
private final MovieRepository movieRepository; | ||
|
||
@Transactional | ||
public String addHearts(Long movieId) { | ||
Movie movie = movieRepository.findById(movieId) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 영화를 찾을 수 없습니다.") | ||
); | ||
hearts like = hearts.create(movie); | ||
likeRepository.save(like); | ||
return like.getId().toString(); | ||
} | ||
} |
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