Last active
February 11, 2021 20:02
-
-
Save SimonHarmonicMinor/61ee914bf185ced6e8cb28fbff9bd86c to your computer and use it in GitHub Desktop.
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
@Entity | |
@Setter | |
@Getter | |
public class Book { | |
@Id | |
@GeneratedValue | |
private Long id; | |
private String name; | |
private String genre; | |
} | |
public interface BookRepository extends JpaRepository<Book, Long> { } | |
@RestController | |
@RequestMapping("/rest") | |
public class Controller { | |
private BookRepository bookRepository; | |
@Autowired | |
public void setBookRepository(BookRepository bookRepository) { | |
this.bookRepository = bookRepository; | |
} | |
@GetMapping("/books") | |
public List<Book> getBooks() { | |
return bookRepository.findAll(); | |
} | |
@GetMapping("/book/{id}") | |
public Book getBookById(@PathVariable int id) { | |
return bookRepository.findById((long) id) | |
.orElse(null); | |
} | |
@GetMapping("/editBook/{id}") | |
public void editBook(@PathVariable int id, @RequestParam String name, @RequestParam String genre) { | |
Book book = getBookById(id); | |
book.setGenre(genre); | |
book.setName(name); | |
bookRepository.save(book); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment