edit book servlet now interacts with database
This commit is contained in:
parent
15dddd5d30
commit
c57fe04916
|
@ -146,6 +146,28 @@ public class Database {
|
|||
return author;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Author getAuthor(String name) {
|
||||
Author author = null;
|
||||
|
||||
Map.Entry<List<ResultSet>, Connection> set = getResultSets(this.author, "name", name);
|
||||
|
||||
if(set == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
if (set.getKey().size() > 0) {
|
||||
try {
|
||||
author = new Author(set.getKey().get(0).getLong("id"), set.getKey().get(0).getString("name"));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets category by id.
|
||||
*
|
||||
|
@ -562,6 +584,32 @@ public class Database {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets specific entries from table.
|
||||
*
|
||||
* @param table table name
|
||||
* @param column column to match
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<List<ResultSet>, Connection> getResultSets(@NotNull String table, @NotNull String column, @NotNull String value) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?");
|
||||
statement.setString(1, table);
|
||||
statement.setString(2, column);
|
||||
statement.setString(3, value);
|
||||
|
||||
System.out.println(statement);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all specified columns from table.
|
||||
*
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
package de.hsel.itech.servlet;
|
||||
|
||||
import de.hsel.itech.db.pojo.Book;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AddBook extends HttpServlet {
|
||||
|
||||
|
@ -20,7 +17,7 @@ public class AddBook extends HttpServlet {
|
|||
|
||||
Utillity.insertFile(out, "template_head.html");
|
||||
de.hsel.itech.db.Database db = de.hsel.itech.db.Database.getInstance();
|
||||
ArrayList<Book> books = db.getBooks();
|
||||
//List<Book> books = db.getBooks();
|
||||
|
||||
out.println("<form class=\"m-container\" action=\"booklist\" method=post>");
|
||||
Utillity.addInput(out,"Titel", "title");
|
||||
|
@ -29,6 +26,7 @@ public class AddBook extends HttpServlet {
|
|||
Utillity.addInput(out,"Jahr", "year", "2019");
|
||||
Utillity.addInput(out,"Beschreibung", "description");
|
||||
Utillity.addInput(out,"Verlag", "publisher");
|
||||
Utillity.addInput(out,"Kategorie", "category");
|
||||
Utillity.addInput(out,"Bild", "image", "DxAzOKSiPoE");
|
||||
Utillity.addTypeButton(out, "Hinzufügen", "submit");
|
||||
out.println("</form>");
|
||||
|
|
|
@ -13,7 +13,7 @@ import java.io.PrintWriter;
|
|||
import java.time.Year;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Random;
|
||||
import java.util.List;
|
||||
|
||||
public class BookList extends HttpServlet {
|
||||
|
||||
|
@ -34,13 +34,13 @@ public class BookList extends HttpServlet {
|
|||
String param = req.getParameter("removeid");
|
||||
if(param != null){
|
||||
long isbn = Long.parseLong(param);
|
||||
db.removeBook(isbn);
|
||||
db.deleteBook(isbn);
|
||||
resp.sendRedirect("booklist");
|
||||
}
|
||||
|
||||
if(req.getParameter("title") != null){
|
||||
String title = req.getParameter("title");
|
||||
String author = req.getParameter("author");
|
||||
String authorName = req.getParameter("author");
|
||||
int price = Integer.parseInt(req.getParameter("price"));
|
||||
int year = Integer.parseInt(req.getParameter("year"));
|
||||
String category = req.getParameter("category");
|
||||
|
@ -48,14 +48,24 @@ public class BookList extends HttpServlet {
|
|||
String publisher = req.getParameter("publisher");
|
||||
String image = req.getParameter("image");
|
||||
|
||||
|
||||
ArrayList<Author> authors = new ArrayList<Author>();
|
||||
Author author = null;
|
||||
|
||||
author = db.getAuthor(authorName);
|
||||
|
||||
if(author == null){
|
||||
author = new Author(authorName);
|
||||
db.insert(author);
|
||||
}
|
||||
authors.add(author);
|
||||
Book book = new Book(
|
||||
new Random().nextInt(),
|
||||
new Author(0,author),
|
||||
new Publisher(0,publisher),
|
||||
new Category(0,category, "DxAzOKSiPoE"),
|
||||
authors,
|
||||
new Publisher(publisher),
|
||||
new Category(category, "DxAzOKSiPoE"),
|
||||
title, Year.of(year),price,description, image
|
||||
);
|
||||
db.addBook(book);
|
||||
db.insert(book);
|
||||
|
||||
resp.sendRedirect("booklist");
|
||||
}
|
||||
|
@ -67,44 +77,48 @@ public class BookList extends HttpServlet {
|
|||
Utillity.insertFile(out, "template_head.html");
|
||||
|
||||
|
||||
ArrayList<Book> books = db.getBooks();
|
||||
List<Book> books = db.getBooks();
|
||||
|
||||
//book entries
|
||||
out.println("<div class=\"m-container\">");
|
||||
for (Book book : books) {
|
||||
if(books != null) {
|
||||
|
||||
out.println(" <div class=\"m-row\">");
|
||||
//book entries
|
||||
out.println("<div class=\"m-container\">");
|
||||
for (Book book : books) {
|
||||
|
||||
out.println(" <div class=\"m-col-l-4\">");
|
||||
out.println(" <img class=\"m-image\" src=\"https://source.unsplash.com/" + book.getImage() + "/300x300\" alt=\"Buchcover\">");
|
||||
out.println(" </div>");
|
||||
out.println(" <div class=\"m-row\">");
|
||||
|
||||
out.println(" <div class=\"m-col-l-4\">");
|
||||
out.println("<h1>" + book.getTitle() + "</h1>");
|
||||
out.println(" <br>");
|
||||
out.println(" <h2>" + book.getAuthor().getName() + "</h2>");
|
||||
out.println(" <br>");
|
||||
out.print(" <h3>" + book.getPrice() / 100 + ",");
|
||||
if(book.getPrice() % 100 < 10){
|
||||
out.print("0");
|
||||
out.println(" <div class=\"m-col-l-4\">");
|
||||
out.println(" <img class=\"m-image\" src=\"https://source.unsplash.com/" + book.getImage() + "/300x300\" alt=\"Buchcover\">");
|
||||
out.println(" </div>");
|
||||
|
||||
out.println(" <div class=\"m-col-l-4\">");
|
||||
out.println("<h1>" + book.getTitle() + "</h1>");
|
||||
out.println(" <br>");
|
||||
for (int i = 0; i < book.getAuthors().size(); i++) {
|
||||
out.println(" <h2>" + book.getAuthors().get(0).getName() + "</h2>");
|
||||
}
|
||||
out.println(" <br>");
|
||||
out.print(" <h3>" + book.getPrice() / 100 + ",");
|
||||
if (book.getPrice() % 100 < 10) {
|
||||
out.print("0");
|
||||
}
|
||||
out.println(book.getPrice() % 100 + " €</h3>");
|
||||
out.println(" <br>");
|
||||
out.println(" </div>");
|
||||
|
||||
out.println(" <div class=\"m-col-l-4\">");
|
||||
out.println(" <div class=\"m-button m-danger\">");
|
||||
|
||||
out.println("<a href=\"/itech/booklist?removeid=" + book.getIsbn() + "\">");
|
||||
out.println("Entfernen");
|
||||
out.println("</a>");
|
||||
|
||||
out.println(" </div>");
|
||||
out.println(" </div>");
|
||||
out.println(" </div>");
|
||||
}
|
||||
out.println(book.getPrice() % 100 +" €</h3>");
|
||||
out.println(" <br>");
|
||||
out.println(" </div>");
|
||||
|
||||
out.println(" <div class=\"m-col-l-4\">");
|
||||
out.println(" <div class=\"m-button m-danger\">");
|
||||
|
||||
out.println("<a href=\"/itech/booklist?removeid=" + book.getIsbn() + "\">");
|
||||
out.println("Entfernen");
|
||||
out.println("</a>");
|
||||
|
||||
out.println(" </div>");
|
||||
out.println(" </div>");
|
||||
out.println(" </div>");
|
||||
out.println("</div>");
|
||||
}
|
||||
out.println("</div>");
|
||||
|
||||
|
||||
//add book button
|
||||
Utillity.addButton(out,"Neues Buch Hinzufügen", "addbook");
|
||||
|
|
Loading…
Reference in New Issue