-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMovieService.java
More file actions
38 lines (31 loc) · 1.17 KB
/
MovieService.java
File metadata and controls
38 lines (31 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.ceos23.spring_boot.service;
import com.ceos23.spring_boot.domain.Movie;
import com.ceos23.spring_boot.exception.CustomException;
import com.ceos23.spring_boot.global.exception.ErrorCode;
import com.ceos23.spring_boot.repository.MovieRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class MovieService {
private final MovieRepository movieRepository;
// 영화 생성
@CacheEvict(value = "movieAll", allEntries = true)
public Movie create(String title, String director) {
return movieRepository.save(new Movie(title, director));
}
// 전체 조회
@Cacheable(value = "movieAll", key = "'all'")
public List<Movie> findAll() {
return movieRepository.findAll();
}
// 단건 조회
@Cacheable(value = "movie", key = "#id")
public Movie findById(Long id) {
return movieRepository.findById(id)
.orElseThrow(() -> new CustomException(ErrorCode.MOVIE_NOT_FOUND));
}
}