parent
b2ed3d6a03
commit
1742ee4c14
2
pom.xml
2
pom.xml
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
<groupId>de.hsel</groupId>
|
<groupId>de.hsel</groupId>
|
||||||
<artifactId>itech</artifactId>
|
<artifactId>itech</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.5.0-SNAPSHOT</version>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
|
@ -14,6 +14,8 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* handles all database stuff for addresses of users.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.5
|
* @since 0.5
|
||||||
|
@ -26,6 +28,12 @@ public class AddressDB {
|
||||||
this.database = database;
|
this.database = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get all addresses of specified user.
|
||||||
|
*
|
||||||
|
* @param user user to get addresses from
|
||||||
|
* @return list of users addresses
|
||||||
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public List<Address> getAll(@NotNull User user) {
|
public List<Address> getAll(@NotNull User user) {
|
||||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tableUserAddress, "user", String.valueOf(user.getId()));
|
Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tableUserAddress, "user", String.valueOf(user.getId()));
|
||||||
|
@ -47,6 +55,12 @@ public class AddressDB {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get specific address.
|
||||||
|
*
|
||||||
|
* @param id address id
|
||||||
|
* @return specific address
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Address get(long id) {
|
public Address get(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableAddress, id);
|
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableAddress, id);
|
||||||
|
@ -64,6 +78,12 @@ public class AddressDB {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get user from address id.
|
||||||
|
*
|
||||||
|
* @param id address id
|
||||||
|
* @return User
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private User getUserFromId(long id) {
|
private User getUserFromId(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetByValue(database.tableUserAddress, "address", id);
|
Map.Entry<ResultSet, Connection> entry = database.getResultSetByValue(database.tableUserAddress, "address", id);
|
||||||
|
@ -79,11 +99,16 @@ public class AddressDB {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* insert address into database.
|
||||||
|
*
|
||||||
|
* @param address address to insert
|
||||||
|
* @return insert count
|
||||||
|
*/
|
||||||
public int insert(@NotNull Address address) {
|
public int insert(@NotNull Address address) {
|
||||||
|
|
||||||
Connection connection = database.getConnection();
|
Connection connection = database.getConnection();
|
||||||
int updateCount = 0;
|
int insertCount = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
assert connection != null;
|
assert connection != null;
|
||||||
|
@ -94,21 +119,27 @@ public class AddressDB {
|
||||||
statement.setInt(4, address.getZipCode());
|
statement.setInt(4, address.getZipCode());
|
||||||
statement.setString(5, address.getCity());
|
statement.setString(5, address.getCity());
|
||||||
|
|
||||||
updateCount += statement.executeUpdate();
|
insertCount += statement.executeUpdate();
|
||||||
|
|
||||||
PreparedStatement relationStatement = connection.prepareStatement("INSERT INTO user_address (user, address) VALUES (?, ?)");
|
PreparedStatement relationStatement = connection.prepareStatement("INSERT INTO user_address (user, address) VALUES (?, ?)");
|
||||||
relationStatement.setLong(1, address.getUser().getId());
|
relationStatement.setLong(1, address.getUser().getId());
|
||||||
relationStatement.setLong(2, address.getId());
|
relationStatement.setLong(2, address.getId());
|
||||||
|
|
||||||
updateCount += statement.executeUpdate();
|
insertCount += statement.executeUpdate();
|
||||||
|
|
||||||
connection.close();
|
connection.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return updateCount;
|
return insertCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* delete address from database.
|
||||||
|
*
|
||||||
|
* @param address address to delete
|
||||||
|
* @return deletion count
|
||||||
|
*/
|
||||||
public int delete(@NotNull Address address) {
|
public int delete(@NotNull Address address) {
|
||||||
return database.delete(address.getId(), database.tableUserAddress);
|
return database.delete(address.getId(), database.tableUserAddress);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ public class Database {
|
||||||
final String tableUser = "user";
|
final String tableUser = "user";
|
||||||
final String tableUserAddress = "user_address";
|
final String tableUserAddress = "user_address";
|
||||||
final String tableAddress = "address";
|
final String tableAddress = "address";
|
||||||
|
final String tableCartBooks = "cart_books";
|
||||||
|
|
||||||
private AuthorDB authorDB;
|
private AuthorDB authorDB;
|
||||||
private BookDB bookDB;
|
private BookDB bookDB;
|
||||||
|
@ -43,6 +44,7 @@ public class Database {
|
||||||
private PublisherDB publisherDB;
|
private PublisherDB publisherDB;
|
||||||
private UserDB userDB;
|
private UserDB userDB;
|
||||||
private AddressDB addressDB;
|
private AddressDB addressDB;
|
||||||
|
private ShoppingCartDB shoppingCartDB;
|
||||||
|
|
||||||
private static Database instance;
|
private static Database instance;
|
||||||
|
|
||||||
|
@ -149,6 +151,13 @@ public class Database {
|
||||||
return addressDB;
|
return addressDB;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Contract
|
||||||
|
public ShoppingCartDB shoppingCart() {
|
||||||
|
if(shoppingCartDB == null)
|
||||||
|
shoppingCartDB = new ShoppingCartDB(this);
|
||||||
|
return shoppingCartDB;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,106 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get all items from users shopping cart.
|
||||||
|
*
|
||||||
|
* @param user which users shopping cart to get.
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -63,7 +63,7 @@ public class UserDB {
|
||||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO user (name, email, password, type) values (?, ?, ?, ?)");
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO user (name, email, password, type) values (?, ?, ?, ?)");
|
||||||
statement.setString(1, user.getName());
|
statement.setString(1, user.getName());
|
||||||
statement.setString(2, user.getEmail());
|
statement.setString(2, user.getEmail());
|
||||||
statement.setString(3, user.getEmail());
|
statement.setString(3, user.getPassword());
|
||||||
statement.setShort(4, user.getType());
|
statement.setShort(4, user.getType());
|
||||||
|
|
||||||
updateCount += statement.executeUpdate();
|
updateCount += statement.executeUpdate();
|
||||||
|
|
|
@ -16,9 +16,8 @@ import lombok.RequiredArgsConstructor;
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ShoppingCart {
|
public class ShoppingCartItem {
|
||||||
|
|
||||||
private long id;
|
|
||||||
@NonNull private User user;
|
@NonNull private User user;
|
||||||
@NonNull private Book article;
|
@NonNull private Book article;
|
||||||
@NonNull private int count;
|
@NonNull private int count;
|
|
@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS publisher(id bigint primary key auto_increment, name
|
||||||
CREATE TABLE IF NOT EXISTS author(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) on delete cascade);
|
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) on delete cascade);
|
||||||
CREATE TABLE IF NOT EXISTS category(id bigint primary key auto_increment, name varchar(25), image varchar(11));
|
CREATE TABLE IF NOT EXISTS category(id bigint primary key auto_increment, name varchar(25), image varchar(11));
|
||||||
CREATE TABLE IF NOT EXISTS user(id bigint primary key auto_increment, name varchar(50), email varchar(255), password binary(40), type tinyint);
|
CREATE TABLE IF NOT EXISTS user(id bigint primary key auto_increment, name varchar(50), email varchar(255), password binary(60), type tinyint);
|
||||||
CREATE TABLE IF NOT EXISTS address(id bigint primary key auto_increment, name varchar(100), street varchar(100), number smallint, zipCode smallint, city varchar(50));
|
CREATE TABLE IF NOT EXISTS address(id bigint primary key auto_increment, name varchar(100), street varchar(100), number smallint, zipCode smallint, city varchar(50));
|
||||||
CREATE TABLE IF NOT EXISTS user_address(id bigint primary key auto_increment, user bigint, address bigint, foreign key (user) REFERENCES user(id), foreign key (address) references address(id) on delete cascade);
|
CREATE TABLE IF NOT EXISTS user_address(id bigint primary key auto_increment, user bigint, address bigint, foreign key (user) REFERENCES user(id), foreign key (address) references address(id) on delete cascade);
|
||||||
CREATE TABLE IF NOT EXISTS payment_type(id bigint primary key auto_increment, name varchar(25) unique);
|
CREATE TABLE IF NOT EXISTS payment_type(id bigint primary key auto_increment, name varchar(25) unique);
|
||||||
|
@ -19,5 +19,4 @@ CREATE TABLE IF NOT EXISTS payment_invoice(id bigint primary key auto_increment)
|
||||||
CREATE TABLE IF NOT EXISTS payment_paypal(id bigint primary key auto_increment, mail varchar(255), auth varchar(255));
|
CREATE TABLE IF NOT EXISTS payment_paypal(id bigint primary key auto_increment, mail varchar(255), auth varchar(255));
|
||||||
CREATE TABLE IF NOT EXISTS `order`(id bigint primary key auto_increment, user bigint, price bigint);
|
CREATE TABLE IF NOT EXISTS `order`(id bigint primary key auto_increment, user bigint, price bigint);
|
||||||
CREATE TABLE IF NOT EXISTS order_book(`order` bigint, book bigint, count tinyint, primary key (`order`, book, count), foreign key (`order`) references `order`(id), foreign key (book) references `order`(id));
|
CREATE TABLE IF NOT EXISTS order_book(`order` bigint, book bigint, count tinyint, primary key (`order`, book, count), foreign key (`order`) references `order`(id), foreign key (book) references `order`(id));
|
||||||
CREATE TABLE IF NOT EXISTS shopping_cart(id bigint primary key auto_increment);
|
|
||||||
CREATE TABLE IF NOT EXISTS cart_books(user bigint, book bigint, count tinyint, primary key(book, user), foreign key (book) REFERENCES book(id), foreign key (user) REFERENCES user(id));
|
CREATE TABLE IF NOT EXISTS cart_books(user bigint, book bigint, count tinyint, primary key(book, user), foreign key (book) REFERENCES book(id), foreign key (user) REFERENCES user(id));
|
Loading…
Reference in New Issue