Created
July 24, 2023 18:00
-
-
Save danirod/6ac5d6d42bb93145547788907b1165ee to your computer and use it in GitHub Desktop.
Código del capítulo de Quarkus donde hablo de Active Record. Las notas del episodio están en https://www.makigas.es/series/acceso-a-datos-con-quarkus/active-record-con-panacheentity y el vídeo se puede ver en https://www.youtube.com/watch?v=Z9PVz5XBTdA
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
package es.danirod.quarkus.bookshelf; | |
import jakarta.persistence.Entity; | |
import io.quarkus.hibernate.orm.panache.PanacheEntity; | |
import java.time.LocalDate; | |
import java.util.Objects; | |
@Entity | |
public class Book extends PanacheEntity { | |
private String title; | |
private String description; | |
private int numPages; | |
private LocalDate pubDate; | |
public Book() { | |
// Sigue siendo necesario tener un constructor vacío. | |
// Si es el único constructor, entonces no es obligatorio | |
// ponerlo de acuerdo con las reglas del lenguaje Java. | |
} | |
public Book(String title, String description) { | |
this.title = title; | |
this.description = description; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
public int getNumPages() { | |
return numPages; | |
} | |
public void setNumPages(int numPages) { | |
this.numPages = numPages; | |
} | |
public LocalDate getPubDate() { | |
return pubDate; | |
} | |
public void setPubDate(LocalDate pubDate) { | |
this.pubDate = pubDate; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof Book book)) return false; | |
return id == book.id && numPages == book.numPages && Objects.equals(title, book.title) && Objects.equals(description, book.description) && Objects.equals(pubDate, book.pubDate); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(id, title, description, numPages, pubDate); | |
} | |
@Override | |
public String toString() { | |
return "Book{" + | |
"title='" + title + '\'' + | |
", description='" + description + '\'' + | |
", numPages=" + numPages + | |
", pubDate=" + pubDate + | |
", id=" + id + | |
'}'; | |
} | |
} |
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
package es.danirod.quarkus.bookshelf; | |
import jakarta.transaction.Transactional; | |
import jakarta.ws.rs.GET; | |
import jakarta.ws.rs.POST; | |
import jakarta.ws.rs.Path; | |
import java.util.List; | |
@Path("/books") | |
public class BookResource { | |
@GET | |
public List<Book> listBooks() { | |
return Book.listAll(); | |
} | |
@POST | |
@Transactional | |
public Book insertBook(Book givenBook) { | |
// Esto es solo para jugar con los datos y mostrar que son POJOs normales. | |
// Probablemente podríamos usar el givenBook directamente a continuación. | |
Book b = new Book(); | |
b.setTitle(givenBook.getTitle()); | |
b.setDescription(givenBook.getDescription()); | |
b.setNumPages(givenBook.getNumPages()); | |
b.setPubDate(givenBook.getPubDate()); | |
b.persist(); | |
return b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment