Skip to content

Commit

Permalink
update driver & BE
Browse files Browse the repository at this point in the history
  • Loading branch information
ChouMean committed Mar 27, 2024
2 parents 26821d6 + 3342134 commit 6540fe8
Show file tree
Hide file tree
Showing 15 changed files with 290 additions and 87 deletions.
7 changes: 7 additions & 0 deletions bsm-desk-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<junit.version>5.10.0</junit.version> </properties>

<dependencies>

</dependency> <dependency>
</dependency> <groupId>org.postgresql</groupId>
</dependency> <artifactId>postgresql</artifactId>
</dependency> <version>42.7.3</version>
</dependency> </dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
Expand Down
30 changes: 0 additions & 30 deletions bsm-desk-app/src/main/java/main/backend/list/AuthorList.java

This file was deleted.

35 changes: 0 additions & 35 deletions bsm-desk-app/src/main/java/main/backend/list/BookList.java

This file was deleted.

49 changes: 49 additions & 0 deletions bsm-desk-app/src/main/java/main/backend/lists/AuthorList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main.backend.lists;

import java.sql.ResultSet;
import java.util.ArrayList;

import main.backend.objects.Author;
import main.backend.utils.DBconnect;

public class AuthorList {
private ArrayList<Author> authors;

public AuthorList() { authors = new ArrayList<>(); }
public AuthorList(AuthorList other) { authors = new ArrayList<>(other.authors); }

public void add(Author author) { authors.add(author); }
public void clear() { authors.clear(); }
public Author getAuthorByID(int id) {
for (Author author : authors)
if (author.getId() == id) return author;
return null;
}

public boolean loadAuthors_fromDatabase(String name) {
String condition = name == null ? null : ("WHERE Name = " + name);
try (
DBconnect db = new DBconnect();
ResultSet rs = db.view("AUTHOR", condition);
) {
authors.clear();
while (rs.next())
authors.add(new Author(rs.getInt(0), rs.getString(1), rs.getString(2)));
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}

@Override
public String toString() {
String str = "There are " + authors.size() + " authors in the list.\n\n";

for (Author author : authors)
str += author.toString() + "\n";

return str;
}
}
72 changes: 72 additions & 0 deletions bsm-desk-app/src/main/java/main/backend/lists/BookList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main.backend.lists;

import java.sql.ResultSet;
import java.util.ArrayList;

import main.backend.objects.Book;
import main.backend.utils.DBconnect;

public class BookList {
private ArrayList<Book> books;

public BookList() { books = new ArrayList<>(); }
public BookList(BookList other) { books = new ArrayList<>(other.books); }

public void add(Book book) { books.add(book); }
public void clear() { books.clear(); }
public Book getBookById(int id) {
for (Book book : books)
if (book.getId() == id) return book;
return null;
}

public boolean loadBooks_fromDatabase(String name, PublisherList pl, AuthorList au, CategoryList ca) {
String condition = name == null ? null : ("WHERE Name = " + name);
try (
DBconnect db = new DBconnect();
ResultSet rs = db.view("AUTHOR", condition);
) {
books.clear();
while (rs.next()) {
int id = rs.getInt(0);
String title = rs.getString(1);
String isbn = rs.getString(2);
String language = rs.getString(3);
int numberOfPages = rs.getInt(4);
int publisherID = rs.getInt(5);
int authorID = rs.getInt(6);
boolean enabled = rs.getBoolean(8);

CategoryList categories = new CategoryList();
String condition1 = "WHERE BookID = " + String.valueOf(id);
try (ResultSet rs1 = db.view("CATEGORY_BOOK", condition1);) {
while (rs1.next())
categories.add(ca.getCategoryByID(rs1.getInt(0)));
} catch (Exception e) {
System.err.println("Categories");
e.printStackTrace();
return false;
}

Book book = new Book(id, title, isbn, language, numberOfPages, pl.getPublisherByID(publisherID), au.getAuthorByID(authorID), categories, enabled);
books.add(book);
}
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}

@Override
public String toString() {
String str = "There are " + books.size() + " books in the list.\n\n";

for (Book book : books)
str += book.toString() + "\n";

return str;
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
package main.backend.list;
package main.backend.lists;

import java.sql.ResultSet;
import java.util.ArrayList;

import main.backend.objects.Category;
import main.backend.utils.DBconnect;

public class CategoryList {
private ArrayList<Category> categories;

public CategoryList() { categories = new ArrayList<Category>(); }
public CategoryList(CategoryList other) { categories = new ArrayList<>(other.categories); }

public void addCategory(Category category) { categories.add(category); }

public void add(Category category) { categories.add(category); }
public void clear() { categories.clear(); }
public Category getCategoryByID(int id) {
for (Category category : categories)
if (category.getId() == id) return category;
return null;
}

public boolean loadCategories_fromDatabase(String name) {
String condition = name == null ? null : ("WHERE Name = " + name);
try (
DBconnect db = new DBconnect();
ResultSet rs = db.view("AUTHOR", condition);
) {
categories.clear();
while (rs.next())
categories.add(new Category(rs.getInt(0), rs.getString(1), rs.getString(2)));
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}

@Override
public String toString() {
String str = "There are " + categories.size() + " publishers in the list.\n\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
package main.backend.list;
package main.backend.lists;

import java.sql.ResultSet;
import java.util.ArrayList;

import main.backend.objects.Publisher;
import main.backend.utils.DBconnect;

public class PublisherList {
private ArrayList<Publisher> publishers;

public PublisherList() { publishers = new ArrayList<>(); }
public PublisherList(PublisherList other) { publishers = new ArrayList<>(other.publishers); }

public void addPublisher(Publisher publisher) { publishers.add(publisher); }

public void add(Publisher publisher) { publishers.add(publisher); }
public void clear() { publishers.clear(); }
public Publisher getPublisherByID(int id) {
for (Publisher publisher : publishers)
if (publisher.getId() == id) return publisher;
return null;
}

public boolean loadPublishers_fromDatabase(String name) {
String condition = name == null ? null : ("WHERE Name = " + name);
try (
DBconnect db = new DBconnect();
ResultSet rs = db.view("AUTHOR", condition);
) {
publishers.clear();
while (rs.next())
publishers.add(new Publisher(rs.getInt(0), rs.getString(1), rs.getString(2)));
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}

@Override
public String toString() {
String str = "There are " + publishers.size() + " publishers in the list.\n\n";
Expand Down
22 changes: 21 additions & 1 deletion bsm-desk-app/src/main/java/main/backend/objects/Author.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main.backend.objects;

import java.sql.SQLException;

import main.backend.utils.DBconnect;

public class Author {
private int id;
private String name, description;
private boolean enabled;


public Author() {}
public Author(int id, String name, String description, boolean enabled) {
this.id = id;
this.name = name;
Expand All @@ -26,6 +31,21 @@ public void changeInfo(int id, String name, String description, boolean enabled)
this.enabled = enabled;
}

public boolean addAuthor_toDatabase() {
boolean rs;
try (DBconnect db = new DBconnect();) {
rs = db.add("AUTHOR", "");
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return rs;
}
public boolean updateAuthor_toDatabase() {

return true;
}

@Override
public String toString() {
String idStr = "\tID: " + String.valueOf(id) + "\n";
Expand Down
Loading

0 comments on commit 6540fe8

Please sign in to comment.