diff --git a/projekt_2/README.md b/projekt_2/README.md index e21b689..65aee68 100644 --- a/projekt_2/README.md +++ b/projekt_2/README.md @@ -1 +1,8 @@ # Projekt 2 + +## Jak Rozpocząć? +1. Pobierz repozytorium. +2. Przejdź do głównego katalogu repozytorium. +3. W terminalu wpisz `./start.sh`. +4. Zostanie utworzony kontener Docker z bazą danych PostgreSQL, która zostanie zainicjalizowana danymi z pliku `init_db.sql`. +5. Teraz można uruchomić plik `main.java`, który wykona operacje SELECT i INSERT na bazie danych. \ No newline at end of file diff --git a/projekt_2/init_db.sql b/projekt_2/init_db.sql index 15ea99c..0701348 100644 --- a/projekt_2/init_db.sql +++ b/projekt_2/init_db.sql @@ -139,6 +139,7 @@ CREATE TABLE IF NOT EXISTS reviews ( rating INT, review_text TEXT, author_name VARCHAR(100), + created_at TIMESTAMP WITH TIME ZONE, FOREIGN KEY (book_id) REFERENCES books(id) ); diff --git a/projekt_2/src/main/java/org/example/Main.java b/projekt_2/src/main/java/org/example/Main.java index 3687d75..d6bafba 100644 --- a/projekt_2/src/main/java/org/example/Main.java +++ b/projekt_2/src/main/java/org/example/Main.java @@ -5,9 +5,6 @@ import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; -import java.util.List; -import java.util.Scanner; - public class Main { public static void main(String[] args) { @@ -22,11 +19,11 @@ public class Main { MyQuery myQuery = new MyQuery(session); // here, replace "test" with the title of the book you want to query - String title1 = "test"; + String title1 = "1984"; myQuery.getBookByExactTitle(title1); // here, replace "test" with the title of the book you want to query - String title2 = "test"; + String title2 = "Pride and Prejudice"; myQuery.getBookDetailsWithAuthorByTitle(title2); // here, set the book id you want to query @@ -40,17 +37,29 @@ public class Main { try { tx = session.beginTransaction(); // create a book - Book book = MyQuery.createBook("test title 3"); + String bookTitle = "test book title"; + Book book = MyQuery.createBook(bookTitle); session.save(book); // create an Author - Author author = MyQuery.createAuthor("adam 3"); + String authorName = "Adam Gulczynski"; + Author author = MyQuery.createAuthor(authorName); session.save(author); // // create book details - BookDetails bookDetails = MyQuery.createBookDetails("333123123", 123, book, author); + + String isbn = "1234-0000332"; + int pageCount = 123; + BookDetails bookDetails = MyQuery.createBookDetails(isbn, pageCount, book, author); session.save(bookDetails); -// + + // create a book review + int rating = 5; + String reviewText = "Great book!"; + String reviewerName = "Adam"; + Review review = MyQuery.createReview(rating, reviewText, book, reviewerName); + session.save(review); + tx.commit(); } catch (Exception e) { if (tx != null) { diff --git a/projekt_2/src/main/java/org/example/MyQuery.java b/projekt_2/src/main/java/org/example/MyQuery.java index 362d264..37aeb3d 100644 --- a/projekt_2/src/main/java/org/example/MyQuery.java +++ b/projekt_2/src/main/java/org/example/MyQuery.java @@ -102,8 +102,6 @@ public class MyQuery { Book book = new Book(); book.setTitle(bookTitle); -// ZonedDateTime currentTimestamp = ZonedDateTime.now(); - return book; } @@ -122,5 +120,19 @@ public class MyQuery { bookDetails.setPageCount(pageCount); return bookDetails; } + + public static Review createReview(int rating, String reviewText, Book book, String authorName) { + Review review = new Review(); + review.setBook(book); + review.setRating(rating); + review.setReviewText(reviewText); + review.setAuthorName(authorName); + + ZonedDateTime currentTime = ZonedDateTime.now(); + review.setCreatedAt(currentTime); + + return review; + } + // ======================== END of INSERTing ================================= } diff --git a/projekt_2/src/main/java/org/example/Review.java b/projekt_2/src/main/java/org/example/Review.java index cb86518..d13592a 100644 --- a/projekt_2/src/main/java/org/example/Review.java +++ b/projekt_2/src/main/java/org/example/Review.java @@ -2,11 +2,13 @@ package org.example; import jakarta.persistence.*; +import java.time.ZonedDateTime; + @Entity @Table(name = "reviews") public class Review { -@Id + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "review_id") private Long id; @@ -23,6 +25,9 @@ public class Review { @Column(name = "author_name") private String authorName; + @Column(name = "created_at", columnDefinition = "TIMESTAMP WITH TIME ZONE") + private ZonedDateTime createdAt; + public Review() { } @@ -66,6 +71,18 @@ public class Review { this.authorName = authorName; } + public ZonedDateTime getCreatedAt() { + if (createdAt == null) { + createdAt = ZonedDateTime.now(); + } + + return createdAt; + } + + public void setCreatedAt(ZonedDateTime createdAt) { + this.createdAt = createdAt; + } + @Override public String toString() { return "Review{" + diff --git a/projekt_2/target/classes/org/example/Main.class b/projekt_2/target/classes/org/example/Main.class index 5810a99..af241c1 100644 Binary files a/projekt_2/target/classes/org/example/Main.class and b/projekt_2/target/classes/org/example/Main.class differ diff --git a/projekt_2/target/classes/org/example/MyQuery.class b/projekt_2/target/classes/org/example/MyQuery.class index 7c54eb2..f13ba52 100644 Binary files a/projekt_2/target/classes/org/example/MyQuery.class and b/projekt_2/target/classes/org/example/MyQuery.class differ diff --git a/projekt_2/target/classes/org/example/Review.class b/projekt_2/target/classes/org/example/Review.class index 7c471ae..d2e2a1c 100644 Binary files a/projekt_2/target/classes/org/example/Review.class and b/projekt_2/target/classes/org/example/Review.class differ