changed booklist entries
added value validation to addbook form added simple Error massages
This commit is contained in:
parent
61e9b4df54
commit
5849812afd
|
@ -300,6 +300,7 @@ public class Database {
|
|||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, book);
|
||||
assert entry != null;
|
||||
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
Book book = null;
|
||||
try {
|
||||
|
|
|
@ -6,6 +6,14 @@ import javax.servlet.http.HttpServletResponse;
|
|||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
/**
|
||||
* Servlet: form to add a book to the database
|
||||
*
|
||||
*
|
||||
* @author Julian Hinxlage
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
*/
|
||||
public class AddBook extends HttpServlet {
|
||||
|
||||
|
||||
|
@ -18,20 +26,19 @@ public class AddBook extends HttpServlet {
|
|||
Utillity.insertFile(out, "template_head.html");
|
||||
|
||||
out.println("<form class=\"m-container\" action=\"booklist\" method=post>");
|
||||
Utillity.addInput(out, "ISBN", "isbn");
|
||||
Utillity.addValidationInput(out, "ISBN", "isbn", "text", "0", "[0-9]*");
|
||||
Utillity.addInput(out,"Titel", "title");
|
||||
Utillity.addInput(out,"Author", "author");
|
||||
Utillity.addInput(out,"Preis", "price", "0");
|
||||
Utillity.addInput(out,"Jahr", "year", "2019");
|
||||
Utillity.addValidationInput(out,"Preis", "price", "text", "0", "[0-9]*(,[0-9]{1,2})?");
|
||||
Utillity.addValidationInput(out,"Jahr", "year", "text", "2019", "[0-9]*");
|
||||
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");
|
||||
Utillity.addSubmitButton(out, "Hinzufügen");
|
||||
out.println("</form>");
|
||||
|
||||
|
||||
//Utillity.addButton(out,"Hinzufügen", "/itech/booklist");
|
||||
Utillity.addButton(out,"Zurück zur Buchliste", "booklist");
|
||||
|
||||
Utillity.insertFile(out, "template_footer.html");
|
||||
|
|
|
@ -16,6 +16,14 @@ import java.time.Year;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Servlet: list of all books and remove function
|
||||
*
|
||||
*
|
||||
* @author Julian Hinxlage
|
||||
* @version 0.1
|
||||
* @since 0.1
|
||||
*/
|
||||
public class BookList extends HttpServlet {
|
||||
|
||||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
|
||||
|
@ -25,12 +33,11 @@ public class BookList extends HttpServlet {
|
|||
//get database object
|
||||
Database db = Database.getInstance();
|
||||
|
||||
//print parameters (debug)
|
||||
/*Enumeration<String> names = req.getParameterNames();
|
||||
while (names.hasMoreElements()) {
|
||||
String name = names.nextElement();
|
||||
System.out.println(name + "=" + req.getParameter(name));
|
||||
}*/
|
||||
resp.setCharacterEncoding("utf-8");
|
||||
PrintWriter out = resp.getWriter();
|
||||
|
||||
//header template
|
||||
Utillity.insertFile(out, "template_head.html");
|
||||
|
||||
//remove book by id
|
||||
String param = req.getParameter("removeid");
|
||||
|
@ -40,45 +47,76 @@ public class BookList extends HttpServlet {
|
|||
resp.sendRedirect("booklist");
|
||||
}
|
||||
|
||||
//add book to database
|
||||
if (req.getParameter("title") != null) {
|
||||
long isbn = Long.parseLong(req.getParameter("isbn"));
|
||||
String title = req.getParameter("title");
|
||||
String authorName = req.getParameter("author");
|
||||
int price = Integer.parseInt(req.getParameter("price"));
|
||||
int year = Integer.parseInt(req.getParameter("year"));
|
||||
String categoryName = req.getParameter("category");
|
||||
String description = req.getParameter("description");
|
||||
String publisher = req.getParameter("publisher");
|
||||
String image = req.getParameter("image");
|
||||
try {
|
||||
|
||||
long isbn = Long.parseLong(req.getParameter("isbn"));
|
||||
String title = req.getParameter("title");
|
||||
String authorName = req.getParameter("author");
|
||||
String priceString = req.getParameter("price");
|
||||
String[] priceParts = priceString.split(",");
|
||||
int price = Integer.parseInt(priceParts[0]) * 100;
|
||||
if (priceParts.length >= 2) {
|
||||
if (priceParts[1].length() == 1) {
|
||||
price += Integer.parseInt(priceParts[1]) * 10;
|
||||
} else {
|
||||
price += Integer.parseInt(priceParts[1]);
|
||||
}
|
||||
}
|
||||
int year = Integer.parseInt(req.getParameter("year"));
|
||||
String categoryName = req.getParameter("category");
|
||||
String description = req.getParameter("description");
|
||||
String publisher = req.getParameter("publisher");
|
||||
String image = req.getParameter("image");
|
||||
|
||||
ArrayList<Author> authors = new ArrayList<>();
|
||||
String[] authorNames = authorName.split(",");
|
||||
for (String name : authorNames) {
|
||||
authors.add(new Author(name));
|
||||
//check isbn
|
||||
List<Book> books = db.getBooks();
|
||||
boolean found = false;
|
||||
if (books != null) {
|
||||
for(Book b : books){
|
||||
if(b.getIsbn() == isbn){
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
out.println("<aside class =\"m-note m-danger\">");
|
||||
out.println("<h3>Es gibt breits ein buch mit dieser ISBN </h3>");
|
||||
out.println("</aside>");
|
||||
} else {
|
||||
ArrayList<Author> authors = new ArrayList<>();
|
||||
String[] authorNames = authorName.split(",");
|
||||
for (String name : authorNames) {
|
||||
authors.add(new Author(name));
|
||||
}
|
||||
Category category = new Category();
|
||||
category.setName(categoryName);
|
||||
Book book = new Book(isbn,
|
||||
authors,
|
||||
new Publisher(publisher),
|
||||
category,
|
||||
title, Year.of(year), price, description, image
|
||||
);
|
||||
db.insert(book);
|
||||
resp.sendRedirect("booklist");
|
||||
}
|
||||
}
|
||||
catch (Exception e){
|
||||
e.printStackTrace();
|
||||
out.println("<aside class =\"m-note m-danger\">");
|
||||
out.println("<h3>Es gibt ein Fehler in der Eingabe </h3>");
|
||||
out.println("</aside>");
|
||||
}
|
||||
Category category = new Category();
|
||||
category.setName(categoryName);
|
||||
Book book = new Book(isbn,
|
||||
authors,
|
||||
new Publisher(publisher),
|
||||
category,
|
||||
title, Year.of(year), price, description, image
|
||||
);
|
||||
db.insert(book);
|
||||
|
||||
resp.sendRedirect("booklist");
|
||||
}
|
||||
|
||||
resp.setCharacterEncoding("utf-8");
|
||||
PrintWriter out = resp.getWriter();
|
||||
|
||||
//header template
|
||||
Utillity.insertFile(out, "template_head.html");
|
||||
|
||||
|
||||
//list all books
|
||||
List<Book> books = db.getBooks();
|
||||
|
||||
if (books != null) {
|
||||
|
||||
//book entries
|
||||
|
@ -92,18 +130,16 @@ public class BookList extends HttpServlet {
|
|||
out.println(" </div>");
|
||||
|
||||
out.println(" <div class=\"m-col-l-4\">");
|
||||
out.println("<h1>" + book.getTitle() + "</h1>");
|
||||
out.println(" <br>");
|
||||
out.println("<h2>" + book.getTitle() + "</h2>");
|
||||
out.println("ISBN: " + book.getIsbn() + "</br>");
|
||||
|
||||
for (int i = 0; i < book.getAuthors().size(); i++) {
|
||||
out.println(" <h2>" + book.getAuthors().get(0).getName() + "</h2>");
|
||||
out.println("von " + book.getAuthors().get(0).getName());
|
||||
}
|
||||
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("</br>Veröffentlicht von " + book.getPublisher().getName() + "");
|
||||
out.println("</br></br>" + book.getDescription() + "");
|
||||
|
||||
out.println(" </div>");
|
||||
|
||||
out.println(" <div class=\"m-col-l-4\">");
|
||||
|
@ -113,6 +149,16 @@ public class BookList extends HttpServlet {
|
|||
out.println("Entfernen");
|
||||
out.println("</a>");
|
||||
|
||||
out.println(" <br>");
|
||||
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(" </div>");
|
||||
out.println(" </div>");
|
||||
out.println(" </div>");
|
||||
|
|
|
@ -30,6 +30,15 @@ public class Utillity {
|
|||
out.println("</div>");
|
||||
}
|
||||
|
||||
static public void addValidationInput(PrintWriter out, String label, String name, String type, String value, String pattern){
|
||||
out.println(" <div class=\"m-row\">");
|
||||
out.println(" <div class=\"m-col-l-2 m-push-l-5\">");
|
||||
out.println(label);
|
||||
out.println("<input type=\"" + type + "\" name=\"" + name + "\" value=\"" + value + "\"required pattern=\"" + pattern + "\">");
|
||||
out.println("</div>");
|
||||
out.println("</div>");
|
||||
}
|
||||
|
||||
static public void addButton(PrintWriter out, String label, String url){
|
||||
out.println(" <div class=\"m-row\">");
|
||||
out.println(" <div class=\"m-col-l-4 m-push-l-4\">");
|
||||
|
@ -42,23 +51,10 @@ public class Utillity {
|
|||
out.println("</div>");
|
||||
}
|
||||
|
||||
static public void addTypeButton(PrintWriter out, String label, String type){
|
||||
/*
|
||||
out.println(" <div class=\"m-row\">");
|
||||
out.println(" <div class=\"m-col-l-4 m-push-l-4\">");
|
||||
out.println(" <div class=\"m-button m-success\">");
|
||||
out.println("<a href=\"" + "" + "\">");
|
||||
out.println(label);
|
||||
out.println("<input class=\"m-button m-success\" type=\"" + type + "\" value=\"" + label + "\">");
|
||||
out.println("</a>");
|
||||
out.println(" </div>");
|
||||
out.println("</div>");
|
||||
out.println("</div>");
|
||||
*/
|
||||
|
||||
static public void addSubmitButton(PrintWriter out, String label){
|
||||
out.println(" <div class=\"m-row\">");
|
||||
out.println(" <div class=\"m-col-l-4 m-push-l-5\">");
|
||||
out.println(" <input type=\"" + type + "\" value=\"" + label + "\">");
|
||||
out.println(" <input type=\"" + "submit" + "\" value=\"" + label + "\">");
|
||||
out.println(" </div>");
|
||||
out.println("</div>");
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
CREATE TABLE IF NOT EXISTS test(id bigint primary key auto_increment, hello varchar(25));
|
||||
INSERT IGNORE INTO test(hello) values ('Welt');
|
||||
CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, isbn bigint unique, title varchar(50), description varchar(500), price int, year year, publisher bigint, category bigint, image varchar(11) unique);
|
||||
CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, isbn bigint unique, title varchar(50), description varchar(500), price int, year year, publisher bigint, category bigint, image varchar(11));
|
||||
CREATE TABLE IF NOT EXISTS publisher(id bigint primary key auto_increment, name varchar(50) unique);
|
||||
CREATE TABLE IF NOT EXISTS author(id bigint primary key auto_increment, name varchar(50) unique);
|
||||
CREATE TABLE IF NOT EXISTS author_book(author bigint, book bigint, primary key(author, book), foreign key (author) REFERENCES author(id), foreign key (book) references book(id));
|
||||
|
|
Loading…
Reference in New Issue