126 lines
3.6 KiB
Java
126 lines
3.6 KiB
Java
package de.hsel.itech.db;
|
|
|
|
import de.hsel.itech.db.pojo.ShoppingCartItem;
|
|
import de.hsel.itech.db.pojo.User;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* handles all database stuff for the shopping cart.
|
|
*
|
|
* @author Johannes Theiner
|
|
* @version 0.1
|
|
* @since 0.5
|
|
*/
|
|
public class ShoppingCartDB {
|
|
|
|
private Database database;
|
|
|
|
ShoppingCartDB(Database database) {
|
|
this.database = database;
|
|
}
|
|
|
|
/**
|
|
* getAll all items from users shopping cart.
|
|
*
|
|
* @param user which users shopping cart to getAll.
|
|
* @return list of shopping cart items.
|
|
*/
|
|
@NotNull
|
|
public List<ShoppingCartItem> get(@NotNull User user) {
|
|
Map.Entry<ResultSet, Connection> entry = database.getResultSetsById(database.tableCartBooks, "user", user.getId());
|
|
assert entry != null;
|
|
|
|
List<ShoppingCartItem> list = new ArrayList<>();
|
|
|
|
try {
|
|
ResultSet rs = entry.getKey();
|
|
while(rs.next()) {
|
|
list.add(new ShoppingCartItem(user, database.book().get(rs.getLong("book")), rs.getInt("count")));
|
|
}
|
|
entry.getValue().close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
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.
|
|
*
|
|
* @param item item to insert
|
|
* @return insert count
|
|
*/
|
|
public int insert(@NotNull ShoppingCartItem item) {
|
|
Connection connection = database.getConnection();
|
|
int insertCount = 0;
|
|
|
|
try {
|
|
assert connection != null;
|
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO cart_books (user, book, count) VALUES (?, ?, ?)");
|
|
statement.setLong(1, item.getUser().getId());
|
|
statement.setLong(2, item.getArticle().getId());
|
|
statement.setInt(3, item.getCount());
|
|
|
|
insertCount += statement.executeUpdate();
|
|
|
|
connection.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return insertCount;
|
|
}
|
|
|
|
/**
|
|
* delete item from shopping cart.
|
|
*
|
|
* @param item item to delete
|
|
* @return deletion count
|
|
*/
|
|
public int delete(@NotNull ShoppingCartItem item) {
|
|
Connection connection = database.getConnection();
|
|
int deleteCount = 0;
|
|
|
|
try {
|
|
assert connection != null;
|
|
PreparedStatement statement = connection.prepareStatement("DELETE FROM cart_books WHERE user = ? AND book = ?");
|
|
statement.setLong(1, item.getUser().getId());
|
|
statement.setLong(2, item.getArticle().getId());
|
|
|
|
deleteCount += statement.executeUpdate();
|
|
connection.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return deleteCount;
|
|
}
|
|
} |