Newer
Older
//Implements the users repository using JDBC
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.object.SqlQuery;
import org.springframework.stereotype.Repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

Gabriel Copat
committed
import java.util.Objects;
@Repository
public class UserRepositoryJDBC implements UserRepository{
private final JdbcTemplate jdbc;
public UserRepositoryJDBC(JdbcTemplate aJdbc){
this.jdbc = aJdbc;
private void setUserMapper(){
userMapper = (rs, i) -> new User(
rs.getString("email"),
return jdbc.query(sql, userMapper);
}

Gabriel Copat
committed
Connor Brock
committed

Gabriel Copat
committed
@Override
public List<Long> getUserStickersFromPack(String username, int packID) {
/* Returns a list with the stickerIDs of stickers that the specified user has unlocked from the given pack */
String sql = "SELECT stickerID FROM stickerprogress WHERE (username, packID)= (?,?)";
return jdbc.queryForList(sql, Long.class, username, packID);

Gabriel Copat
committed
}
@Override
public boolean unlockSticker(String username, int packID, int stickerID){
/* Adds entry in the stickerprogress database, effectively unlocking the sticker for the user
* Returns false if no sticker is unlocked */
String query = "SELECT COUNT(id) FROM stickers WHERE (stickerID, packID) = (stickerID, packID)";
if (jdbc.queryForObject(query, Integer.class) == 1){ //Checks if sticker exists
String sql = "INSERT INTO stickerprogress (username, packID, stickerID) VALUES (?,?,?)";
jdbc.update(sql, username, packID, stickerID);
return true;
}
return false;
@Override
public boolean addUser(String username, String email, String password){
/* Adds new user to the database */
String query = "INSERT INTO users (username, email, password) VALUES (?, ?, ?);";
String query2= "INSERT INTO authorities (username, authority) VALUES (?,?);";

Gabriel Copat
committed
jdbc.update(query, username, email, password);
jdbc.update(query2, username, "USER");

Gabriel Copat
committed
return true;

Gabriel Copat
committed
@Override
public boolean doesUserExist(String email){
/* Returns true if a user with given email already exists in the database */
String query = "SELECT COUNT(email) FROM users WHERE (email) = (?)";
return !(jdbc.queryForObject(query, Integer.class, email) == 0);
}

Gabriel Copat
committed
@Override
public User findUserByEmail(String email) {
/* Finds user matching given email */
String query = "SELECT * FROM users WHERE (email) = (?)";
List<User> result = jdbc.query(query, userMapper, email);
return result.isEmpty() ? null : result.get(0);
}
@Override
public User findUserByName(String name) {
/* Finds user matching given name */
String query = "SELECT * FROM users WHERE (username) = (?)";
List<User> result = jdbc.query(query, userMapper, name);
return result.isEmpty() ? null : result.get(0);

Gabriel Copat
committed
}