일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 인프런
- spring
- 김영한
- Spring Boot
- Greedy
- java
- jpa
- Exception
- 스프링 핵심 기능
- Proxy
- Servlet
- 알고리즘
- springdatajpa
- AOP
- QueryDSL
- JDBC
- http
- Thymeleaf
- pointcut
- SpringBoot
- JPQL
- kotlin
- 스프링 핵심 원리
- 자바
- db
- 백준
- transaction
- 그리디
- 스프링
- Android
- Today
- Total
개발자되기 프로젝트
['21.07.14] Service class생성, api test 본문
1. API controller
기존 APi controller의 각 method를 보면 아래와 같이 Api controller의 기능 외에 실제 service로직까지 포함되어 있다.
Service에 해당하는 class를 별도로 만들어서 apicontroller는 정말로 controller의 역할만 하게 수정해보자.
@GetMapping("/search")
public SearchMovieRes movieSearch(@RequestParam String query){
System.out.println(query);
SearchMovieReq searchMovieReq = new SearchMovieReq();
searchMovieReq.setQuery(query);
var result = naverClient.searchMovie(searchMovieReq);
return result;
}
<MovieListService class> 해당 클래스에, CRUD, Naver에서 영화 정보 가져오는 기능을 모아놨다.
@Service
@Data
public class MovieListService {
private final MovieRepository movieRepository;
private final NaverClient naverClient;
//add to list
public Movie addToMovieList(Movie movie){
movieRepository.save(movie);
return movie;
}
public List<Movie> getMovieList() {
return movieRepository.findAll();
}
public SearchMovieRes searhMovie(String query){
SearchMovieReq searchMovieReq = new SearchMovieReq();
searchMovieReq.setQuery(query);
return naverClient.searchMovie(searchMovieReq);
}
public void deleteMovie(int id){
movieRepository.deleteById(id);
}
}
<ApiController> api controller는 순전히 controller의 역할만 하도록 수정.
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class ApiController {
private final MovieListService movieListService;
@GetMapping("/search")
public SearchMovieRes movieSearch(@RequestParam String query){
return movieListService.searhMovie(query);
}
@PostMapping
public Movie addMovie(@RequestBody Movie movie){
return movieListService.addToMovieList(movie);
}
@GetMapping("/get")
public List<Movie> getFindMovie(){
return movieListService.getMovieList();
}
@DeleteMapping("/{id}")
public void deleteMovie(@PathVariable int id){
movieListService.deleteMovie(id);
}
}
<Test> 수정한 코드가 정상적으로 동작하는지 test
@Test
void listTest(){
makeMovie();
movieRepository.findAll().forEach(System.out::println);
System.out.println(movieRepository.findById(1));
movieRepository.deleteById(1);
movieRepository.findAll().forEach(System.out::println);
}
public void makeMovie(){
Movie movie1 = new Movie();
movie1.setTitle("블랙위도우");
movie1.setDirector("케이트 쇼트랜드");
movie1.setActor("스칼렛요한슨");
movieRepository.save(movie1);
Movie movie2 = new Movie();
movie2.setTitle("날씨의아이");
movie2.setDirector("신카이 마코토");
movie2.setActor("히나");
movieRepository.save(movie2);
}
2. DDL-AUTO : Create-drop
그런데 test를 매 번 실행할 때 마다 repository에 추가되는 movie entity의 id가 계속 증가한다..
hibernate설정이 잘못된 것 같다. DDL 실행 후 Test종료 시 삭제가 되지 않은 것 같다.
Test종료 후 바로 삭제될 수 있도록 DDL-AUTO를 Create-drop으로 설정해주자.
<application.yml>
jpa:
show-sql: true
properties:
hibernate:
format_sql: true
generate-ddl: true
hibernate:
ddl-auto: create-drop
의도한 결과가 나왔다.
Movie(id=1, title=블랙위도우, link=null, image=null, subtitle=null, pubDate=null, director=케이트 쇼트랜드, actor=스칼렛요한슨, userRating=null)
Movie(id=2, title=날씨의아이, link=null, image=null, subtitle=null, pubDate=null, director=신카이 마코토, actor=히나, userRating=null)
test를 작성하고보니, MovieListServie에 대한 test가 아니다.. 다시!
<Test>
@Test
void listTest(){
makeMovie();
movieListService.addToMovieList(new Movie());
movieListService.getMovieList().forEach(System.out::println);
movieListService.deleteMovie(1);
movieListService.getMovieList().forEach(System.out::println);
}
public void makeMovie(){
Movie movie1 = new Movie();
movie1.setTitle("블랙위도우");
movie1.setDirector("케이트 쇼트랜드");
movie1.setActor("스칼렛요한슨");
movieRepository.save(movie1);
Movie movie2 = new Movie();
movie2.setTitle("날씨의아이");
movie2.setDirector("신카이 마코토");
movie2.setActor("히나");
movieRepository.save(movie2);
}
3. @GeneratedValue의 Strategy
의도하는대로 동작했다. 음 그런데, 의문은 hibernate_sequence에 대한 update query가 실행된다.
Hibernate:
select
next_val as id_val
from
hibernate_sequence for update
Hibernate:
update
hibernate_sequence
set
next_val= ?
where
next_val=?
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
select
next_val as id_val
from
hibernate_sequence for update
Hibernate:
update
hibernate_sequence
set
next_val= ?
where
next_val=?
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
select
next_val as id_val
from
hibernate_sequence for update
Hibernate:
update
hibernate_sequence
set
next_val= ?
where
next_val=?
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
select
movie0_.id as id1_0_,
movie0_.actor as actor2_0_,
movie0_.director as director3_0_,
movie0_.image as image4_0_,
movie0_.link as link5_0_,
movie0_.pub_date as pub_date6_0_,
movie0_.subtitle as subtitle7_0_,
movie0_.title as title8_0_,
movie0_.user_rating as user_rat9_0_
from
movie movie0_
Movie(id=1, title=블랙위도우, link=null, image=null, subtitle=null, pubDate=null, director=케이트 쇼트랜드, actor=스칼렛요한슨, userRating=null)
Movie(id=2, title=날씨의아이, link=null, image=null, subtitle=null, pubDate=null, director=신카이 마코토, actor=히나, userRating=null)
Movie(id=3, title=null, link=null, image=null, subtitle=null, pubDate=null, director=null, actor=null, userRating=null)
Hibernate:
select
movie0_.id as id1_0_0_,
movie0_.actor as actor2_0_0_,
movie0_.director as director3_0_0_,
movie0_.image as image4_0_0_,
movie0_.link as link5_0_0_,
movie0_.pub_date as pub_date6_0_0_,
movie0_.subtitle as subtitle7_0_0_,
movie0_.title as title8_0_0_,
movie0_.user_rating as user_rat9_0_0_
from
movie movie0_
where
movie0_.id=?
Hibernate:
delete
from
movie
where
id=?
Hibernate:
select
movie0_.id as id1_0_,
movie0_.actor as actor2_0_,
movie0_.director as director3_0_,
movie0_.image as image4_0_,
movie0_.link as link5_0_,
movie0_.pub_date as pub_date6_0_,
movie0_.subtitle as subtitle7_0_,
movie0_.title as title8_0_,
movie0_.user_rating as user_rat9_0_
from
movie movie0_
Movie(id=2, title=날씨의아이, link=null, image=null, subtitle=null, pubDate=null, director=신카이 마코토, actor=히나, userRating=null)
Movie(id=3, title=null, link=null, image=null, subtitle=null, pubDate=null, director=null, actor=null, userRating=null)
아마 @GeneratedValue의 strategy가 default로 설정되어서 실행된 것 같다.
AUTO로 설정하면 DB 의존성 없이 자동으로 Hiberante가 생성 전략을 결정한다.
지금은 MySQL을 사용하기 때문에, 명확하게 IDENTITY로 수정하자.
<Movie class>
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String title;
private String link;
private String image;
private String subtitle;
private LocalDate pubDate;
private String director;
private String actor;
private String userRating;
}
<result>
정확히 의도한대로 쿼리가 동작한다.
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating)
values
(?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating)
values
(?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating)
values
(?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
select
movie0_.id as id1_0_,
movie0_.actor as actor2_0_,
movie0_.director as director3_0_,
movie0_.image as image4_0_,
movie0_.link as link5_0_,
movie0_.pub_date as pub_date6_0_,
movie0_.subtitle as subtitle7_0_,
movie0_.title as title8_0_,
movie0_.user_rating as user_rat9_0_
from
movie movie0_
Movie(id=1, title=블랙위도우, link=null, image=null, subtitle=null, pubDate=null, director=케이트 쇼트랜드, actor=스칼렛요한슨, userRating=null)
Movie(id=2, title=날씨의아이, link=null, image=null, subtitle=null, pubDate=null, director=신카이 마코토, actor=히나, userRating=null)
Movie(id=3, title=null, link=null, image=null, subtitle=null, pubDate=null, director=null, actor=null, userRating=null)
Hibernate:
select
movie0_.id as id1_0_0_,
movie0_.actor as actor2_0_0_,
movie0_.director as director3_0_0_,
movie0_.image as image4_0_0_,
movie0_.link as link5_0_0_,
movie0_.pub_date as pub_date6_0_0_,
movie0_.subtitle as subtitle7_0_0_,
movie0_.title as title8_0_0_,
movie0_.user_rating as user_rat9_0_0_
from
movie movie0_
where
movie0_.id=?
Hibernate:
delete
from
movie
where
id=?
Hibernate:
select
movie0_.id as id1_0_,
movie0_.actor as actor2_0_,
movie0_.director as director3_0_,
movie0_.image as image4_0_,
movie0_.link as link5_0_,
movie0_.pub_date as pub_date6_0_,
movie0_.subtitle as subtitle7_0_,
movie0_.title as title8_0_,
movie0_.user_rating as user_rat9_0_
from
movie movie0_
Movie(id=2, title=날씨의아이, link=null, image=null, subtitle=null, pubDate=null, director=신카이 마코토, actor=히나, userRating=null)
Movie(id=3, title=null, link=null, image=null, subtitle=null, pubDate=null, director=null, actor=null, userRating=null)
4. API controller와 Test
1) POST : addToMovieList
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating)
values
(?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating)
values
(?, ?, ?, ?, ?, ?, ?, ?)
2)GET : getMovieList
Hibernate:
select
movie0_.id as id1_0_,
movie0_.actor as actor2_0_,
movie0_.director as director3_0_,
movie0_.image as image4_0_,
movie0_.link as link5_0_,
movie0_.pub_date as pub_date6_0_,
movie0_.subtitle as subtitle7_0_,
movie0_.title as title8_0_,
movie0_.user_rating as user_rat9_0_
from
movie movie0_
Hibernate:
select
movie0_.id as id1_0_0_,
movie0_.actor as actor2_0_0_,
movie0_.director as director3_0_0_,
movie0_.image as image4_0_0_,
movie0_.link as link5_0_0_,
movie0_.pub_date as pub_date6_0_0_,
movie0_.subtitle as subtitle7_0_0_,
movie0_.title as title8_0_0_,
movie0_.user_rating as user_rat9_0_0_
from
movie movie0_
where
movie0_.id=?
3) DELETE : deleteMovie
Hibernate:
delete
from
movie
where
id=?
4) GET : getMovieList
Hibernate:
select
movie0_.id as id1_0_,
movie0_.actor as actor2_0_,
movie0_.director as director3_0_,
movie0_.image as image4_0_,
movie0_.link as link5_0_,
movie0_.pub_date as pub_date6_0_,
movie0_.subtitle as subtitle7_0_,
movie0_.title as title8_0_,
movie0_.user_rating as user_rat9_0_
from
movie movie0_
5)GET : searchMovie는 아직.. 서버오류..
5. GitHub : 210714, service class, api test
'Project > 영화리뷰 관리' 카테고리의 다른 글
['21.07.17] Null check, ENum (0) | 2021.07.17 |
---|---|
['21.07.15] Naver 영화검색 API (0) | 2021.07.15 |
['21.07.14] Entity, DTO 분리 (0) | 2021.07.15 |
['21.07.13] Naver API 연동 (0) | 2021.07.13 |
['21.07.12] Movie Entity, Repository, API Controller (0) | 2021.07.12 |