react-springboot : test api server

This commit is contained in:
kim
2021-02-05 16:39:16 +09:00
parent 1181ef4948
commit 97508ae5d3
2 changed files with 32 additions and 5 deletions

View File

@@ -33,7 +33,8 @@ public class BookService {
public Book 수정하기(Long id, Book book){
Book bookEntity = bookRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("id를 확인해 주세요"));
bookEntity.setTitle(book.getTitle())
bookEntity
.setTitle(book.getTitle())
.setAuthor(book.getAuthor());
return bookEntity;
} // 함수 종료 => 트랜잭션 종료 => 영속화 되어있는 데이터를 DB로 갱신(flush) => commit : 더티체킹

View File

@@ -1,15 +1,41 @@
package com.example.book.web;
import com.example.book.domain.Book;
import com.example.book.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
@RestController
@RequiredArgsConstructor
public class BookController {
@GetMapping("/")
private final BookService bookService;
@GetMapping("/book")
public ResponseEntity<?> findAll() {
return new ResponseEntity<String>("ok", HttpStatus.OK);
return new ResponseEntity<>(bookService.모두가져오기(), HttpStatus.OK);
}
@PostMapping ("/book")
public ResponseEntity<?> save(@RequestBody Book book) {
return new ResponseEntity<>(bookService.저장하기(book), HttpStatus.CREATED);
}
@GetMapping("/book/{id}")
public ResponseEntity<?> findById(@PathVariable Long id) {
return new ResponseEntity<>(bookService.한건가져오기(id), HttpStatus.OK);
}
@PutMapping ("/book/{id}")
public ResponseEntity<?> updateById(@PathVariable Long id, @RequestBody Book book) {
return new ResponseEntity<>(bookService.수정하기(id, book), HttpStatus.OK);
}
@DeleteMapping ("/book/{id}")
public ResponseEntity<?> updateById(@PathVariable Long id) {
return new ResponseEntity<>(bookService.삭제하기(id), HttpStatus.OK);
}
}