Skip to content

Instantly share code, notes, and snippets.

@SimonHarmonicMinor
Last active February 11, 2021 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonHarmonicMinor/61ee914bf185ced6e8cb28fbff9bd86c to your computer and use it in GitHub Desktop.
Save SimonHarmonicMinor/61ee914bf185ced6e8cb28fbff9bd86c to your computer and use it in GitHub Desktop.
@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