projekt 2 finished

This commit is contained in:
Adam Gulczyński 2024-01-17 20:43:59 +01:00
parent cc75c784b2
commit 5503971716
8 changed files with 58 additions and 12 deletions

View File

@ -1 +1,8 @@
# Projekt 2 # 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.

View File

@ -139,6 +139,7 @@ CREATE TABLE IF NOT EXISTS reviews (
rating INT, rating INT,
review_text TEXT, review_text TEXT,
author_name VARCHAR(100), author_name VARCHAR(100),
created_at TIMESTAMP WITH TIME ZONE,
FOREIGN KEY (book_id) REFERENCES books(id) FOREIGN KEY (book_id) REFERENCES books(id)
); );

View File

@ -5,9 +5,6 @@ import org.hibernate.SessionFactory;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import java.util.List;
import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
@ -22,11 +19,11 @@ public class Main {
MyQuery myQuery = new MyQuery(session); MyQuery myQuery = new MyQuery(session);
// here, replace "test" with the title of the book you want to query // here, replace "test" with the title of the book you want to query
String title1 = "test"; String title1 = "1984";
myQuery.getBookByExactTitle(title1); myQuery.getBookByExactTitle(title1);
// here, replace "test" with the title of the book you want to query // here, replace "test" with the title of the book you want to query
String title2 = "test"; String title2 = "Pride and Prejudice";
myQuery.getBookDetailsWithAuthorByTitle(title2); myQuery.getBookDetailsWithAuthorByTitle(title2);
// here, set the book id you want to query // here, set the book id you want to query
@ -40,17 +37,29 @@ public class Main {
try { try {
tx = session.beginTransaction(); tx = session.beginTransaction();
// create a book // create a book
Book book = MyQuery.createBook("test title 3"); String bookTitle = "test book title";
Book book = MyQuery.createBook(bookTitle);
session.save(book); session.save(book);
// create an Author // create an Author
Author author = MyQuery.createAuthor("adam 3"); String authorName = "Adam Gulczynski";
Author author = MyQuery.createAuthor(authorName);
session.save(author); session.save(author);
// //
// create book details // 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); 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(); tx.commit();
} catch (Exception e) { } catch (Exception e) {
if (tx != null) { if (tx != null) {

View File

@ -102,8 +102,6 @@ public class MyQuery {
Book book = new Book(); Book book = new Book();
book.setTitle(bookTitle); book.setTitle(bookTitle);
// ZonedDateTime currentTimestamp = ZonedDateTime.now();
return book; return book;
} }
@ -122,5 +120,19 @@ public class MyQuery {
bookDetails.setPageCount(pageCount); bookDetails.setPageCount(pageCount);
return bookDetails; 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 ================================= // ======================== END of INSERTing =================================
} }

View File

@ -2,11 +2,13 @@ package org.example;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.time.ZonedDateTime;
@Entity @Entity
@Table(name = "reviews") @Table(name = "reviews")
public class Review { public class Review {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "review_id") @Column(name = "review_id")
private Long id; private Long id;
@ -23,6 +25,9 @@ public class Review {
@Column(name = "author_name") @Column(name = "author_name")
private String authorName; private String authorName;
@Column(name = "created_at", columnDefinition = "TIMESTAMP WITH TIME ZONE")
private ZonedDateTime createdAt;
public Review() { public Review() {
} }
@ -66,6 +71,18 @@ public class Review {
this.authorName = authorName; this.authorName = authorName;
} }
public ZonedDateTime getCreatedAt() {
if (createdAt == null) {
createdAt = ZonedDateTime.now();
}
return createdAt;
}
public void setCreatedAt(ZonedDateTime createdAt) {
this.createdAt = createdAt;
}
@Override @Override
public String toString() { public String toString() {
return "Review{" + return "Review{" +