shopping cart done

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2019-06-06 19:03:37 +02:00
parent 7d443a1aeb
commit 41d62898c1
2 changed files with 30 additions and 3 deletions

View File

@ -52,6 +52,26 @@ public class ShoppingCartDB {
return list;
}
public int updateItem(@NotNull ShoppingCartItem item) {
Connection connection = database.getConnection();
assert connection != null;
int alterCount = 0;
try {
PreparedStatement statement = connection.prepareStatement("UPDATE cart_books SET count = ? WHERE user = ? AND book = ?");
statement.setInt(1, item.getCount());
statement.setLong(2, item.getUser().getId());
statement.setLong(3, item.getArticle().getId());
alterCount += statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return alterCount;
}
/**
* insert new item to users shopping cart.
*

View File

@ -3,11 +3,13 @@ package de.hsel.itech.jsf;
import de.hsel.itech.db.Database;
import de.hsel.itech.db.pojo.*;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import javax.faces.annotation.ManagedProperty;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.faces.event.ValueChangeEvent;
import java.sql.Date;
import java.util.ArrayList;
@ -72,10 +74,15 @@ public class CartBean {
return items;
}
public void change(ValueChangeEvent event) {
System.out.println("änderung");
public void change(@NotNull ValueChangeEvent event) {
if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();
return;
}
for(ShoppingCartItem item : getItems()) {
System.out.println(item.getCount());
Database.getInstance().shoppingCart().updateItem(item);
}
}