Compare commits
2 commits
a0cf7989b4
...
0bfba6d99c
Author | SHA1 | Date | |
---|---|---|---|
0bfba6d99c | |||
35b97d1a25 |
37 changed files with 1555 additions and 1018 deletions
|
@ -11,6 +11,7 @@ import java.util.regex.Matcher;
|
|||
import dev.peerat.backend.bonus.extract.RouteExtracter;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.ConnectionManager;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.DependencyInjector;
|
||||
|
@ -39,7 +40,7 @@ public class Main{
|
|||
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
DatabaseRepository repo = new DatabaseRepository(config);
|
||||
DatabaseRepository repo = new DatabaseRepository(new ConnectionManager(config), config);
|
||||
Router<PeerAtUser> router = new Router<PeerAtUser>()
|
||||
.activeReOrdering().
|
||||
addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *").
|
||||
|
@ -101,7 +102,7 @@ public class Main{
|
|||
public boolean intercept(Matcher matcher, Context context, HttpReader reader, HttpWriter writer, Method method){
|
||||
if(method.getDeclaringClass().getPackage().getName().contains(".admins")){
|
||||
try {
|
||||
Group group = repo.getPlayerGroup(context.<PeerAtUser>getUser().getId(), 1);
|
||||
Group group = repo.getGroupRepository().getPlayerGroup(context.<PeerAtUser>getUser().getId(), 1);
|
||||
if(!group.getName().equalsIgnoreCase("Quarter-Master - Battles PAC x CEI")){
|
||||
context.response(401);
|
||||
return false;
|
||||
|
@ -130,7 +131,7 @@ public class Main{
|
|||
locker.lock(key);
|
||||
Context instance = locker.getValue(key);
|
||||
if(instance == null) continue;
|
||||
System.out.println("["+((instance.isLogged()) ? repo.getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo() : "?")+"] "+instance.getType()+" "+instance.getPath()+" -> "+instance.getResponseCode());
|
||||
System.out.println("["+((instance.isLogged()) ? repo.getPlayerRepository().getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo() : "?")+"] "+instance.getType()+" "+instance.getPath()+" -> "+instance.getResponseCode());
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -38,7 +38,7 @@ public class Bot extends Thread{
|
|||
|
||||
locker.init(key);
|
||||
|
||||
List<Group> groups = this.repo.getAllGroups();
|
||||
List<Group> groups = this.repo.getGroupRepository().getAllGroups();
|
||||
for(Group group : groups){
|
||||
Integer chapter = group.getLinkToChapter();
|
||||
// Integer puzzle = group.getLinkToPuzzle();
|
||||
|
|
114
src/dev/peerat/backend/repository/BaseDatabaseQuery.java
Normal file
114
src/dev/peerat/backend/repository/BaseDatabaseQuery.java
Normal file
|
@ -0,0 +1,114 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Badge;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class BaseDatabaseQuery{
|
||||
|
||||
private ConnectionManager con;
|
||||
private Configuration config;
|
||||
|
||||
public BaseDatabaseQuery(ConnectionManager con){
|
||||
this.con = con;
|
||||
}
|
||||
|
||||
public Connection ensureConnection() throws SQLException{
|
||||
return this.con.ensureConnection();
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(String request) throws SQLException{
|
||||
return this.con.ensureConnection().prepareStatement(request);
|
||||
}
|
||||
|
||||
public Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
||||
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
|
||||
puzzleResult.getString("content"), puzzleResult.getBytes("soluce"), puzzleResult.getString("verify"),
|
||||
puzzleResult.getInt("score_max"), puzzleResult.getString("tags"),
|
||||
hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1,
|
||||
puzzleResult.getTimestamp("start_date"));
|
||||
}
|
||||
|
||||
public Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
||||
return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name"),
|
||||
chapterResult.getTimestamp("start_date"), chapterResult.getTimestamp("end_date"));
|
||||
}
|
||||
|
||||
public Completion makeCompletion(ResultSet completionResult) throws SQLException {
|
||||
String fileName = null;
|
||||
if (hasColumn(completionResult, "fileName"))
|
||||
fileName = completionResult.getString("fileName");
|
||||
String puzzleName = null;
|
||||
if (hasColumn(completionResult, "name"))
|
||||
puzzleName = completionResult.getString("name");
|
||||
|
||||
return new Completion(completionResult.getInt("fk_player"), completionResult.getInt("fk_puzzle"), completionResult.getInt("tries"),
|
||||
fileName, completionResult.getInt("score"), puzzleName);
|
||||
}
|
||||
|
||||
public Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||
playerResult.getString("firstName"), playerResult.getString("lastName"));
|
||||
if (hasColumn(playerResult, "avatar")) {
|
||||
p.setAvatar(playerResult.getBytes("avatar"));
|
||||
}
|
||||
if (hasColumn(playerResult, "score")) {
|
||||
p.addCompletion(new Completion(playerResult.getInt("tries"), playerResult.getInt("score")));
|
||||
for (int ct = 1; ct < playerResult.getInt("completions"); ct++)
|
||||
{ // TODO refactor for V3
|
||||
p.addCompletion(new Completion(0, 0));
|
||||
}
|
||||
}
|
||||
if (hasColumn(playerResult, "name")) {
|
||||
// Manage groups
|
||||
String groupName = playerResult.getString("name");
|
||||
if (groupName != null) {
|
||||
p.addGroup(makeGroup(playerResult));
|
||||
}
|
||||
}
|
||||
|
||||
// ADD rank
|
||||
PreparedStatement completionsStmt = con.ensureConnection().prepareStatement(DatabasePlayerRepository.GET_PLAYER_RANK());
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
p.setRank(result.getInt("rank"));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
public Group makeGroup(ResultSet result) throws SQLException {
|
||||
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"), ((hasColumn(result, "countPlayer")) ? result.getInt("countPlayer") : 0));
|
||||
}
|
||||
|
||||
public Player makeGroupPlayer(ResultSet result) throws SQLException {
|
||||
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
||||
}
|
||||
|
||||
public Badge makeBadge(ResultSet rs) throws SQLException {
|
||||
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
||||
}
|
||||
|
||||
public boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
|
||||
// Found on StackOverflow
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int columns = rsmd.getColumnCount();
|
||||
for (int x = 1; x <= columns; x++) {
|
||||
if (columnName.equals(rsmd.getColumnName(x)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
26
src/dev/peerat/backend/repository/ConnectionManager.java
Normal file
26
src/dev/peerat/backend/repository/ConnectionManager.java
Normal file
|
@ -0,0 +1,26 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
|
||||
public class ConnectionManager {
|
||||
|
||||
private Connection con;
|
||||
private Configuration config;
|
||||
|
||||
public ConnectionManager(Configuration config){
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public Connection ensureConnection() throws SQLException {
|
||||
if (con == null || (!con.isValid(5))) {
|
||||
this.con = DriverManager.getConnection(
|
||||
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
||||
config.getDbUser(), config.getDbPassword());
|
||||
}
|
||||
return this.con;
|
||||
}
|
||||
}
|
195
src/dev/peerat/backend/repository/DatabaseAdminRepository.java
Normal file
195
src/dev/peerat/backend/repository/DatabaseAdminRepository.java
Normal file
|
@ -0,0 +1,195 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
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 dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
import dev.peerat.backend.model.Tag;
|
||||
|
||||
public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
//ADMIN
|
||||
ADD_CHAPTER("INSERT INTO chapters (name, start_date, end_date) VALUES (?,?,?)"),
|
||||
DELETE_CHAPTER("DELETE FROM chapters WHERE id_chapter = ?"),
|
||||
EDIT_CHAPTER("UPDATE chapters SET name = ?, start_date = ?, end_date = ? WHERE id_chapter = ?"),
|
||||
GET_CHAPTER("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||
|
||||
ADD_PUZZLE("INSERT INTO puzzles (name, content, soluce, verify, score_max, fk_chapter) VALUES (?,?,?,?,?,?)"),
|
||||
DELETE_PUZZLE("DELETE FROM puzzles WHERE id_puzzle = ?"),
|
||||
EDIT_PUZZLE("UPDATE puzzles SET name = ?, content = ?, soluce = ?, verify = ?, score_max = ?, fk_chapter = ? WHERE id_puzzle = ?"),
|
||||
GET_PUZZLE("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||
GET_PUZZLES("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag GROUP BY p.id_puzzle"),
|
||||
|
||||
ADD_TAG("INSERT INTO tags (name) VALUES (?)"),
|
||||
DELETE_TAG("DELETE FROM tags WHERE id_tag = ?"),
|
||||
EDIT_TAG("UPDATE tags SET name = ? WHERE id_tag = ?"),
|
||||
GET_TAGS("SELECT * FROM tags");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseAdminRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
//ADMIN
|
||||
public Chapter getAdminChapter(int id){
|
||||
try {
|
||||
PreparedStatement chapterStmt = Query.GET_CHAPTER.prepare(this);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Puzzle getAdminPuzzle(int id){
|
||||
try {
|
||||
PreparedStatement chapterStmt = Query.GET_PUZZLE.prepare(this);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
return makePuzzle(chapterResult);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Puzzle> getAdminPuzzles(){
|
||||
try {
|
||||
PreparedStatement chapterStmt = Query.GET_PUZZLES.prepare(this);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
List<Puzzle> list = new ArrayList<>();
|
||||
while(chapterResult.next()){
|
||||
list.add(makePuzzle(chapterResult));
|
||||
}
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Tag> getAdminTags(){
|
||||
try {
|
||||
PreparedStatement chapterStmt = Query.GET_TAGS.prepare(this);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
List<Tag> list = new ArrayList<>();
|
||||
while(chapterResult.next()){
|
||||
list.add(new Tag(chapterResult.getInt("id_tag"), chapterResult.getString("name")));
|
||||
}
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean adminAddChapter(Chapter chapter) throws SQLException {
|
||||
PreparedStatement statement = Query.ADD_CHAPTER.prepare(this);
|
||||
statement.setString(1, chapter.getName());
|
||||
statement.setTimestamp(2, chapter.getStartDate());
|
||||
statement.setTimestamp(3, chapter.getEndDate());
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminAddPuzzle(Puzzle puzzle, int chapter) throws SQLException {
|
||||
PreparedStatement statement = Query.ADD_PUZZLE.prepare(this);
|
||||
statement.setString(1, puzzle.getName());
|
||||
statement.setString(2, puzzle.getContent());
|
||||
statement.setBytes(3, puzzle.getSoluce());
|
||||
statement.setString(4, "");
|
||||
statement.setInt(5, puzzle.getScoreMax());
|
||||
statement.setInt(6, chapter);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminAddTag(String name) throws SQLException {
|
||||
PreparedStatement statement = Query.ADD_TAG.prepare(this);
|
||||
statement.setString(1, name);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdateChapter(int id, Chapter chapter) throws SQLException {
|
||||
PreparedStatement statement = Query.EDIT_CHAPTER.prepare(this);
|
||||
statement.setString(1, chapter.getName());
|
||||
statement.setTimestamp(2, chapter.getStartDate());
|
||||
statement.setTimestamp(3, chapter.getEndDate());
|
||||
statement.setInt(4, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdatePuzzle(int id, Puzzle puzzle, int chapter) throws SQLException {
|
||||
PreparedStatement statement = Query.EDIT_PUZZLE.prepare(this);
|
||||
statement.setString(1, puzzle.getName());
|
||||
statement.setString(2, puzzle.getContent());
|
||||
statement.setBytes(3, puzzle.getSoluce());
|
||||
statement.setString(4, "");
|
||||
statement.setInt(5, puzzle.getScoreMax());
|
||||
statement.setInt(6, chapter);
|
||||
statement.setInt(7, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdateTag(Tag tag) throws SQLException {
|
||||
PreparedStatement statement = Query.EDIT_TAG.prepare(this);
|
||||
statement.setString(1, tag.getName());
|
||||
statement.setInt(2, tag.getId());
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeleteChapter(int id) throws SQLException {
|
||||
PreparedStatement statement = Query.DELETE_CHAPTER.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeletePuzzle(int id) throws SQLException {
|
||||
PreparedStatement statement = Query.DELETE_PUZZLE.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeleteTag(int id) throws SQLException {
|
||||
PreparedStatement statement = Query.DELETE_TAG.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
}
|
167
src/dev/peerat/backend/repository/DatabaseAuthRepository.java
Normal file
167
src/dev/peerat/backend/repository/DatabaseAuthRepository.java
Normal file
|
@ -0,0 +1,167 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.password4j.Password;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
|
||||
public class DatabaseAuthRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// REGISTER
|
||||
CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"),
|
||||
CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
||||
REGISTER_QUERY(
|
||||
"INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, avatar) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
||||
REGISTER_PLAYER_IN_EXISTING_GROUP(
|
||||
"INSERT INTO containsGroups (fk_player, fk_group) VALUES (?, (SELECT id_group FROM groups WHERE name = ?));"),
|
||||
|
||||
// LOGIN
|
||||
CHECK_PASSWORD("SELECT id_player, passwd FROM players WHERE pseudo=?");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseAuthRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a pseudo is available
|
||||
*
|
||||
* @param pseudo The pseudo to check
|
||||
* @return True if the pseudo is available, false if it's already taken
|
||||
*/
|
||||
public boolean checkPseudoAvailability(String pseudo) {
|
||||
return checkAvailability(pseudo, Query.CHECK_PSEUDO_AVAILABLE_QUERY.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an email is available
|
||||
*
|
||||
* @param email The email to check
|
||||
* @return True if the email is available, false if it's already taken
|
||||
*/
|
||||
public boolean checkEmailAvailability(String email) {
|
||||
return checkAvailability(email, Query.CHECK_EMAIL_AVAILABLE_QUERY.toString());
|
||||
}
|
||||
|
||||
private boolean checkAvailability(String queriedString, String correspondingQuery) {
|
||||
try {
|
||||
PreparedStatement statement = prepare(correspondingQuery);
|
||||
statement.setString(1, queriedString);
|
||||
ResultSet result = statement.executeQuery();
|
||||
return !result.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new user
|
||||
*
|
||||
* @param pseudo The pseudo of the user
|
||||
* @param email The email of the user
|
||||
* @param password The password of the user
|
||||
* @param firstname The firstname of the user
|
||||
* @param lastname The lastname of the user
|
||||
* @param description The description of the user
|
||||
* @param sgroup The group of the user
|
||||
* @param avatar The avatar of the user
|
||||
* @return True if the user was registered, false if an error occurred
|
||||
*/
|
||||
public int register(String pseudo, String email, String password, String firstname, String lastname,
|
||||
String description, String sgroup, String avatar) {
|
||||
try {
|
||||
String pass = Password.hash(password).withArgon2().getResult();
|
||||
System.out.println("pass("+pass.length()+") "+pass);
|
||||
Connection con = ensureConnection();
|
||||
con.setAutoCommit(false);
|
||||
try (PreparedStatement playerStatement = con.prepareStatement(Query.REGISTER_QUERY.toString(),
|
||||
Statement.RETURN_GENERATED_KEYS)) {
|
||||
playerStatement.setString(1, pseudo);
|
||||
playerStatement.setString(2, email);
|
||||
playerStatement.setString(3, Password.hash(password).withArgon2().getResult());
|
||||
playerStatement.setString(4, firstname);
|
||||
playerStatement.setString(5, lastname);
|
||||
playerStatement.setString(6, description);
|
||||
playerStatement.setString(7, avatar);
|
||||
if (playerStatement.executeUpdate() == 1) {
|
||||
ResultSet inserted = playerStatement.getGeneratedKeys();
|
||||
if (inserted.next()) {
|
||||
int newPlayerId = inserted.getInt(1);
|
||||
if (!sgroup.isEmpty()) {
|
||||
try (PreparedStatement containsGroupsStatement = con
|
||||
.prepareStatement(Query.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
||||
containsGroupsStatement.setInt(1, newPlayerId);
|
||||
containsGroupsStatement.setString(2, sgroup);
|
||||
containsGroupsStatement.executeUpdate();
|
||||
}
|
||||
}
|
||||
con.commit();
|
||||
con.setAutoCommit(true);
|
||||
return newPlayerId;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
con.rollback();
|
||||
con.setAutoCommit(true);
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login a user
|
||||
*
|
||||
* @param username The username of the user
|
||||
* @param password The password of the user
|
||||
* @return id the id of the user, -1 if not login successefuly
|
||||
*/
|
||||
public int login(String username, String password) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = prepare(Query.CHECK_PASSWORD.toString());
|
||||
statement.setString(1, username);
|
||||
ResultSet result = statement.executeQuery();
|
||||
if (result.next()) {
|
||||
String hashedPassword = result.getString("passwd");
|
||||
if (Password.check(password, hashedPassword).withArgon2())
|
||||
return result.getInt("id_player");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
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 dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Badge;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabaseBadgeRepository extends BaseDatabaseQuery{
|
||||
|
||||
public static String GET_BADGES_OF_PLAYER(){
|
||||
return Query.GET_BADGES_OF_PLAYER.request;
|
||||
}
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// BADGES
|
||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"),
|
||||
GET_BADGES_OF_PLAYER(
|
||||
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?");
|
||||
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseBadgeRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public Badge getBadge(int badgeId) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt = Query.GET_BADGE.prepare(this);
|
||||
completionsStmt.setInt(1, badgeId);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makeBadge(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
122
src/dev/peerat/backend/repository/DatabaseChapterRepository.java
Normal file
122
src/dev/peerat/backend/repository/DatabaseChapterRepository.java
Normal file
|
@ -0,0 +1,122 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
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 dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabaseChapterRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// CHAPTERS
|
||||
SPECIFIC_CHAPTER_QUERY("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||
CHAPTER_FROM_PUZZLE("SELECT c.*\r\n"
|
||||
+ "FROM chapters c\r\n"
|
||||
+ "JOIN puzzles p ON p.fk_chapter = c.id_chapter\r\n"
|
||||
+ "WHERE p.id_puzzle = ?"),
|
||||
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0");
|
||||
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
private DatabasePuzzleRepository puzzleRepo;
|
||||
|
||||
public DatabaseChapterRepository(ConnectionManager con, Configuration config, DatabasePuzzleRepository puzzleRepo){
|
||||
super(con);
|
||||
this.config = config;
|
||||
this.puzzleRepo = puzzleRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific chapter
|
||||
*
|
||||
* @param id The id of the chapter
|
||||
* @return The chapter or null if an error occurred
|
||||
*/
|
||||
public Chapter getChapter(int id) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.SPECIFIC_CHAPTER_QUERY.prepare(this);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
List<Puzzle> puzzles = puzzleRepo.getPuzzlesInChapter(id);
|
||||
chapter.setPuzzles(puzzles);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Chapter getChapter(Puzzle puzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.CHAPTER_FROM_PUZZLE.prepare(this);
|
||||
chapterStmt.setInt(1, puzzle.getId());
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
List<Puzzle> puzzles = puzzleRepo.getPuzzlesInChapter(chapter.getId());
|
||||
chapter.setPuzzles(puzzles);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all chapters in the database
|
||||
*
|
||||
* @return List of all chapters or null if an error occurred
|
||||
*/
|
||||
public List<Chapter> getAllChapters() {
|
||||
try {
|
||||
List<Chapter> chapterList = new ArrayList<>();
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.ALL_CHAPTERS_QUERY.prepare(this);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
while (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
chapter.setPuzzles(puzzleRepo.getPuzzlesInChapter(chapter.getId()));
|
||||
chapterList.add(chapter);
|
||||
}
|
||||
return chapterList;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,162 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
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 dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabaseCompletionRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// COMPLETIONS
|
||||
GET_COMPLETION(
|
||||
"SELECT * FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
GET_COMPLETION_GROUP("SELECT c.*\r\n"
|
||||
+ "FROM completions c\r\n"
|
||||
+ "WHERE c.fk_puzzle = ? AND c.fk_player IN\r\n"
|
||||
+ "(select f.fk_player FROM containsGroups cgs JOIN containsGroups f ON f.fk_group = cgs.fk_group JOIN groups g ON g.id_group = cgs.fk_group WHERE g.fk_chapter = 12 AND cgs.fk_player = ?)"),
|
||||
INSERT_COMPLETION(
|
||||
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
||||
UPDATE_COMPLETION(
|
||||
"UPDATE completions SET tries = ?, score = ?, fk_player = ? WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
||||
SCORE_GROUP("SELECT c.score\r\n"
|
||||
+ "FROM completions c\r\n"
|
||||
+ "WHERE c.fk_puzzle = ? AND c.fk_player IN\r\n"
|
||||
+ "(select f.fk_player FROM containsGroups cgs JOIN containsGroups f ON f.fk_group = cgs.fk_group JOIN groups g ON g.id_group = cgs.fk_group WHERE g.fk_chapter = 12 AND cgs.fk_player = ?)");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
|
||||
public DatabaseCompletionRepository(ConnectionManager con, Configuration config, DatabaseChapterRepository chapterRepo){
|
||||
super(con);
|
||||
this.config = config;
|
||||
this.chapterRepo = chapterRepo;
|
||||
}
|
||||
|
||||
public Completion getCompletionGroup(int user, int puzzle) {
|
||||
try {
|
||||
PreparedStatement stmt = Query.GET_COMPLETION_GROUP.prepare(this);
|
||||
stmt.setInt(1, puzzle);
|
||||
stmt.setInt(2, user);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeCompletion(result);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getCompletion(user, puzzle);
|
||||
}
|
||||
|
||||
public Completion getCompletion(int playerId, int puzzleId) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = Query.GET_COMPLETION.prepare(this);
|
||||
completionsStmt.setInt(1, puzzleId);
|
||||
completionsStmt.setInt(2, playerId);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makeCompletion(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Completion insertOrUpdatePuzzleResponse(int puzzleId, int userId, String fileName, byte[] code,
|
||||
byte[] response, Puzzle currentPuzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
Completion completion = getCompletionGroup(userId, puzzleId);
|
||||
if (completion == null){
|
||||
System.out.println("Completion is null");
|
||||
completion = new Completion(userId, puzzleId, fileName, code, response, currentPuzzle);
|
||||
insertCompletion(completion);
|
||||
} else {
|
||||
System.out.println(completion);
|
||||
completion.addTry(currentPuzzle, response, chapterRepo.getChapter(currentPuzzle).getId());
|
||||
int lastUserId = completion.getPlayerId();
|
||||
completion.updatePlayer(userId);
|
||||
updateCompletion(completion, lastUserId);
|
||||
}
|
||||
return completion;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void insertCompletion(Completion newCompletion) throws SQLException {
|
||||
PreparedStatement statement = Query.INSERT_COMPLETION.prepare(this);
|
||||
statement.setInt(1, newCompletion.getPuzzleId());
|
||||
statement.setInt(2, newCompletion.getPlayerId());
|
||||
statement.setInt(3, newCompletion.getTries());
|
||||
statement.setBytes(4, newCompletion.getCode());
|
||||
statement.setString(5, newCompletion.getFileName());
|
||||
statement.setInt(6, newCompletion.getScore());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
|
||||
private void updateCompletion(Completion completionToUpdate, int user) throws SQLException{
|
||||
System.out.println("update "+completionToUpdate);
|
||||
PreparedStatement statement = Query.UPDATE_COMPLETION.prepare(this);
|
||||
statement.setInt(1, completionToUpdate.getTries());
|
||||
statement.setInt(2, completionToUpdate.getScore());
|
||||
statement.setInt(3, completionToUpdate.getPlayerId());
|
||||
statement.setInt(4, completionToUpdate.getPuzzleId());
|
||||
statement.setInt(5, user);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
public int getScore(int user, int puzzle) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = Query.SCORE_GROUP.prepare(this);
|
||||
stmt.setInt(1, puzzle);
|
||||
stmt.setInt(2, user);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
|
||||
stmt = Query.SCORE.prepare(this);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, puzzle);
|
||||
|
||||
result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
166
src/dev/peerat/backend/repository/DatabaseGroupRepository.java
Normal file
166
src/dev/peerat/backend/repository/DatabaseGroupRepository.java
Normal file
|
@ -0,0 +1,166 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
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 dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
|
||||
public class DatabaseGroupRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// GROUPS
|
||||
ALL_GROUPS("SELECT * FROM groups"),
|
||||
ALL_GROUPS_BY_CHAPTER("select g.*, count(cg.fk_player) as countPlayer from groups g left join containsGroups cg on cg.fk_group = g.id_group where g.fk_chapter = ? group by g.id_group"),
|
||||
GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = g.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ?"), // AND g.fk_puzzle = ?
|
||||
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ?)"), // OR fk_puzzle = ?
|
||||
GET_GROUP_USERS_COUNT("SELECT count(*) as howmany FROM containsGroups WHERE fk_group = ?"),
|
||||
INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"),
|
||||
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
||||
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseGroupRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public List<Group> getAllGroups() {
|
||||
try {
|
||||
ensureConnection();
|
||||
List<Group> list = new ArrayList<>();
|
||||
PreparedStatement stmt = Query.ALL_GROUPS.prepare(this);
|
||||
ResultSet groupResult = stmt.executeQuery();
|
||||
while (groupResult.next())
|
||||
list.add(makeGroup(groupResult));
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Group> getAllGroupsByChapter(int chapter){
|
||||
try {
|
||||
ensureConnection();
|
||||
List<Group> list = new ArrayList<>();
|
||||
PreparedStatement stmt = Query.ALL_GROUPS_BY_CHAPTER.prepare(this);
|
||||
stmt.setInt(1, chapter);
|
||||
ResultSet groupResult = stmt.executeQuery();
|
||||
while (groupResult.next())
|
||||
list.add(makeGroup(groupResult));
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean insertGroup(Group group, PeerAtUser creator) throws SQLException {
|
||||
Integer groupId = getGroupId(group);
|
||||
if (groupId == null)
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.INSERT_GROUP.prepare(this);
|
||||
statement.setString(1, group.getName());
|
||||
statement.setObject(2, group.getLinkToChapter());
|
||||
// statement.setObject(3, group.getLinkToPuzzle());
|
||||
if (statement.executeUpdate() >= 0)
|
||||
return insertUserInGroup(group, creator);
|
||||
return false;
|
||||
}
|
||||
|
||||
public Group getPlayerGroup(int user, Integer chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = Query.GET_GROUP_FOR_PLAYER.prepare(this);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setObject(2, chapter);
|
||||
// stmt.setObject(3, puzzle);
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeGroup(result);
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer getGroupId(Group group) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = Query.GET_GROUP_ID_BY_DATA.prepare(this);
|
||||
stmt.setString(1, group.getName());
|
||||
stmt.setObject(2, group.getLinkToChapter());
|
||||
// stmt.setObject(3, group.getLinkToPuzzle());
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("id_group");
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException{
|
||||
Integer id = getGroupId(group);
|
||||
if(id != null){
|
||||
int howmany = numberInGroup(id);
|
||||
System.out.println("join group, already have "+howmany);
|
||||
if(howmany > config.getGroupMaxPlayers()) return false;
|
||||
}
|
||||
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
if (id != null && alreadyInGroup == null) {
|
||||
PreparedStatement stmt = Query.INSERT_PLAYER_IN_GROUP.prepare(this);
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int numberInGroup(int group) throws SQLException{
|
||||
PreparedStatement stmt = Query.GET_GROUP_USERS_COUNT.prepare(this);
|
||||
stmt.setInt(1, group);
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if(result.next()) return result.getInt("howmany");
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean leaveGroup(Group group, PeerAtUser user) throws SQLException {
|
||||
Integer id = getGroupId(group);
|
||||
if (id != null) {
|
||||
PreparedStatement stmt = Query.LEAVE_GROUP.prepare(this);
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
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.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.Player;
|
||||
|
||||
public class DatabaseLeaderboardRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// LEADERBOARD
|
||||
ALL_PLAYERS_FOR_LEADERBOARD(
|
||||
"select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c LEFT JOIN puzzles puzz on puzz.id_puzzle = c.fk_puzzle LEFT JOIN chapters chap on chap.id_chapter = puzz.fk_chapter WHERE chap.id_chapter > 1 GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter"),
|
||||
ALL_GROUP_FOR_CHAPTER_LEADERBOARD(
|
||||
"SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE cg.fk_player IS NOT NULL AND fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseLeaderboardRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public SortedSet<Player> getAllPlayerForLeaderboard() {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement playersStmt = Query.ALL_PLAYERS_FOR_LEADERBOARD.prepare(this);
|
||||
ResultSet result = playersStmt.executeQuery();
|
||||
ArrayList<Player> players = new ArrayList<Player>();
|
||||
Player tmpPlayer;
|
||||
while (result.next()) {
|
||||
tmpPlayer = makePlayer(result, result.getInt("id_player"));
|
||||
if (!players.contains(tmpPlayer)) {
|
||||
players.add(tmpPlayer);
|
||||
} else {
|
||||
players.get(players.indexOf(tmpPlayer)).addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
return new TreeSet<Player>(players);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SortedSet<Group> getAllGroupForChapterLeaderboard(int chapterId){
|
||||
try{
|
||||
ensureConnection();
|
||||
PreparedStatement groupsStmt = Query.ALL_GROUP_FOR_CHAPTER_LEADERBOARD.prepare(this);
|
||||
groupsStmt.setInt(1, chapterId);
|
||||
ResultSet result = groupsStmt.executeQuery();
|
||||
List<Group> groups = new ArrayList<Group>();
|
||||
Group tmpGroup;
|
||||
while (result.next()) {
|
||||
tmpGroup = makeGroup(result);
|
||||
if (tmpGroup != null) {
|
||||
int gPosition = groups.indexOf(tmpGroup);
|
||||
if (gPosition < 0) {
|
||||
tmpGroup.addPlayer(makeGroupPlayer(result));
|
||||
groups.add(tmpGroup);
|
||||
} else {
|
||||
groups.get(gPosition).addPlayer(makeGroupPlayer(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TreeSet<Group>(groups);
|
||||
}catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
191
src/dev/peerat/backend/repository/DatabasePlayerRepository.java
Normal file
191
src/dev/peerat/backend/repository/DatabasePlayerRepository.java
Normal file
|
@ -0,0 +1,191 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
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 com.password4j.Password;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabasePlayerRepository extends BaseDatabaseQuery{
|
||||
|
||||
public static String GET_PLAYER_RANK(){
|
||||
return Query.GET_PLAYER_RANK.request;
|
||||
}
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// PLAYERS
|
||||
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
||||
GET_PLAYER_EMAIL("SELECT id_player FROM players WHERE email = ?"),
|
||||
GET_PLAYER_PSEUDO("SELECT * FROM players WHERE pseudo = ?"),
|
||||
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
||||
+ "FROM players p\r\n"
|
||||
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
||||
+ "LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group\r\n"
|
||||
+ "LEFT OUTER JOIN completions c on p.id_player = c.fk_player\r\n"
|
||||
+ "WHERE "),
|
||||
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"),
|
||||
GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN puzzles puzz on puzz.id_puzzle = c.fk_puzzle LEFT JOIN chapters chap on chap.id_chapter = puzz.fk_chapter LEFT JOIN players p ON p.id_player = c.fk_player WHERE chap.id_chapter > 1 GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"),
|
||||
UPDATE_PLAYER_INFO("UPDATE players SET pseudo = ?, email = ?, firstname = ?, lastname = ? WHERE id_player = ?"),
|
||||
UPDATE_PLAYER_PASSWORD("UPDATE players SET passwd = ? WHERE id_player = ?");
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabasePlayerRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public Player getPlayer(int idPlayer) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = Query.GET_PLAYER_SIMPLE.prepare(this);
|
||||
completionsStmt.setInt(1, idPlayer);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makePlayer(result, idPlayer);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getPlayerId(String email){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt = Query.GET_PLAYER_EMAIL.prepare(this);
|
||||
completionsStmt.setString(1, email);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return result.getInt("id_player");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean updatePseudo(int id, Player player, String pseudo){
|
||||
try{
|
||||
PreparedStatement statment = Query.GET_PLAYER_PSEUDO.prepare(this);
|
||||
statment.setString(1, pseudo);
|
||||
ResultSet result = statment.executeQuery();
|
||||
if(result.next()) return false;
|
||||
statment = Query.UPDATE_PLAYER_INFO.prepare(this);
|
||||
statment.setString(1, pseudo);
|
||||
statment.setString(2, player.getEmail());
|
||||
statment.setString(3, player.getFirstname());
|
||||
statment.setString(4, player.getLastname());
|
||||
statment.setInt(5, id);
|
||||
return statment.executeUpdate() > 0;
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void updateProfile(int id, Player player, String lastname, String firstname){
|
||||
try{
|
||||
PreparedStatement statment = Query.UPDATE_PLAYER_INFO.prepare(this);
|
||||
statment.setString(1, player.getPseudo());
|
||||
statment.setString(2, player.getEmail());
|
||||
statment.setString(3, firstname);
|
||||
statment.setString(4, lastname);
|
||||
statment.setInt(5, id);
|
||||
statment.executeUpdate();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void updatePassword(int id, String password){
|
||||
try{
|
||||
PreparedStatement statment = Query.UPDATE_PLAYER_PASSWORD.prepare(this);
|
||||
statment.setString(1, Password.hash(password).withArgon2().getResult());
|
||||
statment.setInt(2, id);
|
||||
statment.executeUpdate();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Player getPlayerDetails(int idPlayer) {
|
||||
return getPlayerDetails(idPlayer, null);
|
||||
}
|
||||
|
||||
public Player getPlayerDetails(String pseudoPlayer) {
|
||||
return getPlayerDetails(-1, pseudoPlayer);
|
||||
}
|
||||
|
||||
private Player getPlayerDetails(int id, String pseudo) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt;
|
||||
if (pseudo != null) {
|
||||
completionsStmt = Query.GET_PLAYER_DETAILS_BY_PSEUDO.prepare(this);
|
||||
completionsStmt.setString(1, pseudo);
|
||||
} else {
|
||||
completionsStmt = Query.GET_PLAYER_DETAILS_BY_ID.prepare(this);
|
||||
completionsStmt.setInt(1, id);
|
||||
}
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
Player player = null;
|
||||
while (result.next()) {
|
||||
if (player == null) {
|
||||
id = result.getInt("id_player");
|
||||
player = makePlayer(result, id);
|
||||
completionsStmt = prepare(DatabaseBadgeRepository.GET_BADGES_OF_PLAYER());
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||
while (resultBadges.next()) {
|
||||
player.addBadge(makeBadge(resultBadges));
|
||||
}
|
||||
} else {
|
||||
player.addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
// ADD completions
|
||||
completionsStmt = Query.GET_PLAYER_COMPLETIONS.prepare(this);
|
||||
completionsStmt.setInt(1, id);
|
||||
result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
player.addCompletion(makeCompletion(result));
|
||||
}
|
||||
|
||||
return player;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
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 dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabasePuzzleRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
SPECIFIC_PUZZLE_QUERY(
|
||||
"SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||
PUZZLES_IN_CHAPTER_QUERY(
|
||||
"SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabasePuzzleRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public List<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
|
||||
List<Puzzle> puzzles = new ArrayList<>();
|
||||
ensureConnection();
|
||||
PreparedStatement puzzleStmt = Query.PUZZLES_IN_CHAPTER_QUERY.prepare(this);
|
||||
puzzleStmt.setInt(1, id);
|
||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||
while (puzzleResult.next()) {
|
||||
puzzles.add(makePuzzle(puzzleResult));
|
||||
}
|
||||
return puzzles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific puzzle
|
||||
*
|
||||
* @param id The id of the puzzle
|
||||
* @return The puzzle or null if an error occurred
|
||||
*/
|
||||
public Puzzle getPuzzle(int id) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement puzzleStmt = Query.SPECIFIC_PUZZLE_QUERY.prepare(this);
|
||||
puzzleStmt.setInt(1, id);
|
||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||
if (puzzleResult.next()) {
|
||||
return makePuzzle(puzzleResult);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -5,101 +5,6 @@ import java.sql.PreparedStatement;
|
|||
import java.sql.SQLException;
|
||||
|
||||
public enum DatabaseQuery {
|
||||
// PUZZLES
|
||||
SPECIFIC_PUZZLE_QUERY(
|
||||
"SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||
PUZZLES_IN_CHAPTER_QUERY(
|
||||
"SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle"),
|
||||
|
||||
// CHAPTERS
|
||||
SPECIFIC_CHAPTER_QUERY("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||
CHAPTER_FROM_PUZZLE("SELECT c.*\r\n"
|
||||
+ "FROM chapters c\r\n"
|
||||
+ "JOIN puzzles p ON p.fk_chapter = c.id_chapter\r\n"
|
||||
+ "WHERE p.id_puzzle = ?"),
|
||||
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0"),
|
||||
|
||||
// GROUPS
|
||||
ALL_GROUPS("SELECT * FROM groups"),
|
||||
ALL_GROUPS_BY_CHAPTER("select g.*, count(cg.fk_player) as countPlayer from groups g left join containsGroups cg on cg.fk_group = g.id_group where g.fk_chapter = ? group by g.id_group"),
|
||||
GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = g.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ?"), // AND g.fk_puzzle = ?
|
||||
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ?)"), // OR fk_puzzle = ?
|
||||
GET_GROUP_USERS_COUNT("SELECT count(*) as howmany FROM containsGroups WHERE fk_group = ?"),
|
||||
INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"),
|
||||
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
||||
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"),
|
||||
|
||||
// LEADERBOARD
|
||||
ALL_PLAYERS_FOR_LEADERBOARD(
|
||||
"select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c LEFT JOIN puzzles puzz on puzz.id_puzzle = c.fk_puzzle LEFT JOIN chapters chap on chap.id_chapter = puzz.fk_chapter WHERE chap.id_chapter > 1 GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter"),
|
||||
ALL_GROUP_FOR_CHAPTER_LEADERBOARD(
|
||||
"SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE cg.fk_player IS NOT NULL AND fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);"),
|
||||
|
||||
// REGISTER
|
||||
CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"),
|
||||
CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
||||
REGISTER_QUERY(
|
||||
"INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, avatar) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
||||
REGISTER_PLAYER_IN_EXISTING_GROUP(
|
||||
"INSERT INTO containsGroups (fk_player, fk_group) VALUES (?, (SELECT id_group FROM groups WHERE name = ?));"),
|
||||
|
||||
// LOGIN
|
||||
CHECK_PASSWORD("SELECT id_player, passwd FROM players WHERE pseudo=?"),
|
||||
|
||||
// COMPLETIONS
|
||||
GET_COMPLETION(
|
||||
"SELECT * FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
GET_COMPLETION_GROUP("SELECT c.*\r\n"
|
||||
+ "FROM completions c\r\n"
|
||||
+ "WHERE c.fk_puzzle = ? AND c.fk_player IN\r\n"
|
||||
+ "(select f.fk_player FROM containsGroups cgs JOIN containsGroups f ON f.fk_group = cgs.fk_group JOIN groups g ON g.id_group = cgs.fk_group WHERE g.fk_chapter = 12 AND cgs.fk_player = ?)"),
|
||||
INSERT_COMPLETION(
|
||||
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
||||
UPDATE_COMPLETION(
|
||||
"UPDATE completions SET tries = ?, score = ?, fk_player = ? WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
||||
SCORE_GROUP("SELECT c.score\r\n"
|
||||
+ "FROM completions c\r\n"
|
||||
+ "WHERE c.fk_puzzle = ? AND c.fk_player IN\r\n"
|
||||
+ "(select f.fk_player FROM containsGroups cgs JOIN containsGroups f ON f.fk_group = cgs.fk_group JOIN groups g ON g.id_group = cgs.fk_group WHERE g.fk_chapter = 12 AND cgs.fk_player = ?)"),
|
||||
|
||||
// PLAYERS
|
||||
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
||||
GET_PLAYER_EMAIL("SELECT id_player FROM players WHERE email = ?"),
|
||||
GET_PLAYER_PSEUDO("SELECT * FROM players WHERE pseudo = ?"),
|
||||
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
||||
+ "FROM players p\r\n"
|
||||
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
||||
+ "LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group\r\n"
|
||||
+ "LEFT OUTER JOIN completions c on p.id_player = c.fk_player\r\n"
|
||||
+ "WHERE "),
|
||||
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"),
|
||||
GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN puzzles puzz on puzz.id_puzzle = c.fk_puzzle LEFT JOIN chapters chap on chap.id_chapter = puzz.fk_chapter LEFT JOIN players p ON p.id_player = c.fk_player WHERE chap.id_chapter > 1 GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"),
|
||||
UPDATE_PLAYER_INFO("UPDATE players SET pseudo = ?, email = ?, firstname = ?, lastname = ? WHERE id_player = ?"),
|
||||
UPDATE_PLAYER_PASSWORD("UPDATE players SET passwd = ? WHERE id_player = ?"),
|
||||
|
||||
// BADGES
|
||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
||||
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"),
|
||||
|
||||
//ADMIN
|
||||
ADD_CHAPTER("INSERT INTO chapters (name, start_date, end_date) VALUES (?,?,?)"),
|
||||
DELETE_CHAPTER("DELETE FROM chapters WHERE id_chapter = ?"),
|
||||
EDIT_CHAPTER("UPDATE chapters SET name = ?, start_date = ?, end_date = ? WHERE id_chapter = ?"),
|
||||
GET_CHAPTER("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||
|
||||
ADD_PUZZLE("INSERT INTO puzzles (name, content, soluce, verify, score_max, fk_chapter) VALUES (?,?,?,?,?,?)"),
|
||||
DELETE_PUZZLE("DELETE FROM puzzles WHERE id_puzzle = ?"),
|
||||
EDIT_PUZZLE("UPDATE puzzles SET name = ?, content = ?, soluce = ?, verify = ?, score_max = ?, fk_chapter = ? WHERE id_puzzle = ?"),
|
||||
GET_PUZZLE("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||
GET_PUZZLES("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag GROUP BY p.id_puzzle"),
|
||||
|
||||
ADD_TAG("INSERT INTO tags (name) VALUES (?)"),
|
||||
DELETE_TAG("DELETE FROM tags WHERE id_tag = ?"),
|
||||
EDIT_TAG("UPDATE tags SET name = ? WHERE id_tag = ?"),
|
||||
GET_TAGS("SELECT * FROM tags"),
|
||||
|
||||
//TRIGGER
|
||||
FIRST_TRY("CREATE OR REPLACE TRIGGER FirstTry\r\n"
|
||||
|
|
|
@ -1,40 +1,82 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import com.password4j.Password;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Badge;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
import dev.peerat.backend.model.Tag;
|
||||
|
||||
public class DatabaseRepository {
|
||||
public class DatabaseRepository extends BaseDatabaseQuery{
|
||||
|
||||
private Connection con;
|
||||
private Connection con; //refractor chain
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseRepository(Configuration config) {
|
||||
private DatabasePuzzleRepository puzzleRepo;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
private DatabaseAdminRepository adminRepo;
|
||||
private DatabaseAuthRepository authRepo;
|
||||
private DatabaseBadgeRepository badgeRepo;
|
||||
private DatabaseCompletionRepository completionRepo;
|
||||
private DatabaseGroupRepository groupRepo;
|
||||
private DatabaseLeaderboardRepository leaderboardRepo;
|
||||
private DatabasePlayerRepository playerRepo;
|
||||
|
||||
public DatabaseRepository(ConnectionManager con, Configuration config) {
|
||||
super(con);
|
||||
this.config = config;
|
||||
|
||||
this.puzzleRepo = new DatabasePuzzleRepository(con, config);
|
||||
this.chapterRepo = new DatabaseChapterRepository(con, config, puzzleRepo);
|
||||
this.adminRepo = new DatabaseAdminRepository(con, config);
|
||||
this.authRepo = new DatabaseAuthRepository(con, config);
|
||||
this.badgeRepo = new DatabaseBadgeRepository(con, config);
|
||||
this.completionRepo = new DatabaseCompletionRepository(con, config, chapterRepo);
|
||||
this.groupRepo = new DatabaseGroupRepository(con, config);
|
||||
this.leaderboardRepo = new DatabaseLeaderboardRepository(con, config);
|
||||
this.playerRepo = new DatabasePlayerRepository(con, config);
|
||||
}
|
||||
|
||||
public DatabasePuzzleRepository getPuzzleRepository(){
|
||||
return this.puzzleRepo;
|
||||
}
|
||||
|
||||
public DatabaseChapterRepository getChapterRepository(){
|
||||
return this.chapterRepo;
|
||||
}
|
||||
|
||||
public DatabaseAdminRepository getAdminRepository(){
|
||||
return this.adminRepo;
|
||||
}
|
||||
|
||||
public DatabaseAuthRepository getAuthRepository(){
|
||||
return this.authRepo;
|
||||
}
|
||||
|
||||
public DatabaseBadgeRepository getBadgeRepository(){
|
||||
return this.badgeRepo;
|
||||
}
|
||||
|
||||
public DatabaseCompletionRepository getCompletionRepository(){
|
||||
return this.completionRepo;
|
||||
}
|
||||
|
||||
public DatabaseGroupRepository getGroupRepository(){
|
||||
return this.groupRepo;
|
||||
}
|
||||
|
||||
public DatabaseLeaderboardRepository getLeaderboardRepository(){
|
||||
return this.leaderboardRepo;
|
||||
}
|
||||
|
||||
public DatabasePlayerRepository getPlayerRepository(){
|
||||
return this.playerRepo;
|
||||
}
|
||||
|
||||
|
||||
public void hotfix() throws Exception{
|
||||
ensureConnection();
|
||||
|
||||
|
@ -85,842 +127,4 @@ public class DatabaseRepository {
|
|||
|
||||
}
|
||||
|
||||
private void ensureConnection() throws SQLException {
|
||||
if (con == null || (!con.isValid(5))) {
|
||||
this.con = DriverManager.getConnection(
|
||||
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
||||
config.getDbUser(), config.getDbPassword());
|
||||
}
|
||||
}
|
||||
|
||||
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
||||
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
|
||||
puzzleResult.getString("content"), puzzleResult.getBytes("soluce"), puzzleResult.getString("verify"),
|
||||
puzzleResult.getInt("score_max"), puzzleResult.getString("tags"),
|
||||
hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1,
|
||||
puzzleResult.getTimestamp("start_date"));
|
||||
}
|
||||
|
||||
private Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
||||
return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name"),
|
||||
chapterResult.getTimestamp("start_date"), chapterResult.getTimestamp("end_date"));
|
||||
}
|
||||
|
||||
private Completion makeCompletion(ResultSet completionResult) throws SQLException {
|
||||
String fileName = null;
|
||||
if (hasColumn(completionResult, "fileName"))
|
||||
fileName = completionResult.getString("fileName");
|
||||
String puzzleName = null;
|
||||
if (hasColumn(completionResult, "name"))
|
||||
puzzleName = completionResult.getString("name");
|
||||
|
||||
return new Completion(completionResult.getInt("fk_player"), completionResult.getInt("fk_puzzle"), completionResult.getInt("tries"),
|
||||
fileName, completionResult.getInt("score"), puzzleName);
|
||||
}
|
||||
|
||||
private Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||
playerResult.getString("firstName"), playerResult.getString("lastName"));
|
||||
if (hasColumn(playerResult, "avatar")) {
|
||||
p.setAvatar(playerResult.getBytes("avatar"));
|
||||
}
|
||||
if (hasColumn(playerResult, "score")) {
|
||||
p.addCompletion(new Completion(playerResult.getInt("tries"), playerResult.getInt("score")));
|
||||
for (int ct = 1; ct < playerResult.getInt("completions"); ct++)
|
||||
{ // TODO refactor for V3
|
||||
p.addCompletion(new Completion(0, 0));
|
||||
}
|
||||
}
|
||||
if (hasColumn(playerResult, "name")) {
|
||||
// Manage groups
|
||||
String groupName = playerResult.getString("name");
|
||||
if (groupName != null) {
|
||||
p.addGroup(makeGroup(playerResult));
|
||||
}
|
||||
}
|
||||
|
||||
// ADD rank
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_RANK.prepare(con);
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
p.setRank(result.getInt("rank"));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
private Group makeGroup(ResultSet result) throws SQLException {
|
||||
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"), ((hasColumn(result, "countPlayer")) ? result.getInt("countPlayer") : 0));
|
||||
}
|
||||
|
||||
private Player makeGroupPlayer(ResultSet result) throws SQLException {
|
||||
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
||||
}
|
||||
|
||||
private Badge makeBadge(ResultSet rs) throws SQLException {
|
||||
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
||||
}
|
||||
|
||||
private boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
|
||||
// Found on StackOverflow
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int columns = rsmd.getColumnCount();
|
||||
for (int x = 1; x <= columns; x++) {
|
||||
if (columnName.equals(rsmd.getColumnName(x)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
|
||||
List<Puzzle> puzzles = new ArrayList<>();
|
||||
ensureConnection();
|
||||
PreparedStatement puzzleStmt = DatabaseQuery.PUZZLES_IN_CHAPTER_QUERY.prepare(this.con);
|
||||
puzzleStmt.setInt(1, id);
|
||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||
while (puzzleResult.next()) {
|
||||
puzzles.add(makePuzzle(puzzleResult));
|
||||
}
|
||||
return puzzles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific puzzle
|
||||
*
|
||||
* @param id The id of the puzzle
|
||||
* @return The puzzle or null if an error occurred
|
||||
*/
|
||||
public Puzzle getPuzzle(int id) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement puzzleStmt = DatabaseQuery.SPECIFIC_PUZZLE_QUERY.prepare(this.con);
|
||||
puzzleStmt.setInt(1, id);
|
||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||
if (puzzleResult.next()) {
|
||||
return makePuzzle(puzzleResult);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getScore(int user, int puzzle) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.SCORE_GROUP.prepare(this.con);
|
||||
stmt.setInt(1, puzzle);
|
||||
stmt.setInt(2, user);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
|
||||
stmt = DatabaseQuery.SCORE.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, puzzle);
|
||||
|
||||
result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Completion getCompletionGroup(int user, int puzzle) {
|
||||
try {
|
||||
PreparedStatement stmt = DatabaseQuery.GET_COMPLETION_GROUP.prepare(this.con);
|
||||
stmt.setInt(1, puzzle);
|
||||
stmt.setInt(2, user);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeCompletion(result);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getCompletion(user, puzzle);
|
||||
}
|
||||
|
||||
public Completion getCompletion(int playerId, int puzzleId) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_COMPLETION.prepare(this.con);
|
||||
completionsStmt.setInt(1, puzzleId);
|
||||
completionsStmt.setInt(2, playerId);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makeCompletion(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Player getPlayer(int idPlayer) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_SIMPLE.prepare(this.con);
|
||||
completionsStmt.setInt(1, idPlayer);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makePlayer(result, idPlayer);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getPlayerId(String email){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_EMAIL.prepare(this.con);
|
||||
completionsStmt.setString(1, email);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return result.getInt("id_player");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean updatePseudo(int id, Player player, String pseudo){
|
||||
try{
|
||||
PreparedStatement statment = DatabaseQuery.GET_PLAYER_PSEUDO.prepare(this.con);
|
||||
statment.setString(1, pseudo);
|
||||
ResultSet result = statment.executeQuery();
|
||||
if(result.next()) return false;
|
||||
statment = DatabaseQuery.UPDATE_PLAYER_INFO.prepare(this.con);
|
||||
statment.setString(1, pseudo);
|
||||
statment.setString(2, player.getEmail());
|
||||
statment.setString(3, player.getFirstname());
|
||||
statment.setString(4, player.getLastname());
|
||||
statment.setInt(5, id);
|
||||
return statment.executeUpdate() > 0;
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void updateProfile(int id, Player player, String lastname, String firstname){
|
||||
try{
|
||||
PreparedStatement statment = DatabaseQuery.UPDATE_PLAYER_INFO.prepare(this.con);
|
||||
statment.setString(1, player.getPseudo());
|
||||
statment.setString(2, player.getEmail());
|
||||
statment.setString(3, firstname);
|
||||
statment.setString(4, lastname);
|
||||
statment.setInt(5, id);
|
||||
statment.executeUpdate();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void updatePassword(int id, String password){
|
||||
try{
|
||||
PreparedStatement statment = DatabaseQuery.UPDATE_PLAYER_PASSWORD.prepare(this.con);
|
||||
statment.setString(1, Password.hash(password).withArgon2().getResult());
|
||||
statment.setInt(2, id);
|
||||
statment.executeUpdate();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Player getPlayerDetails(int idPlayer) {
|
||||
return getPlayerDetails(idPlayer, null);
|
||||
}
|
||||
|
||||
public Player getPlayerDetails(String pseudoPlayer) {
|
||||
return getPlayerDetails(-1, pseudoPlayer);
|
||||
}
|
||||
|
||||
private Player getPlayerDetails(int id, String pseudo) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt;
|
||||
if (pseudo != null) {
|
||||
completionsStmt = DatabaseQuery.GET_PLAYER_DETAILS_BY_PSEUDO.prepare(this.con);
|
||||
completionsStmt.setString(1, pseudo);
|
||||
} else {
|
||||
completionsStmt = DatabaseQuery.GET_PLAYER_DETAILS_BY_ID.prepare(this.con);
|
||||
completionsStmt.setInt(1, id);
|
||||
}
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
Player player = null;
|
||||
while (result.next()) {
|
||||
if (player == null) {
|
||||
id = result.getInt("id_player");
|
||||
player = makePlayer(result, id);
|
||||
completionsStmt = DatabaseQuery.GET_BADGES_OF_PLAYER.prepare(this.con);
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||
while (resultBadges.next()) {
|
||||
player.addBadge(makeBadge(resultBadges));
|
||||
}
|
||||
} else {
|
||||
player.addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
// ADD completions
|
||||
completionsStmt = DatabaseQuery.GET_PLAYER_COMPLETIONS.prepare(con);
|
||||
completionsStmt.setInt(1, id);
|
||||
result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
player.addCompletion(makeCompletion(result));
|
||||
}
|
||||
|
||||
return player;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SortedSet<Player> getAllPlayerForLeaderboard() {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement playersStmt = DatabaseQuery.ALL_PLAYERS_FOR_LEADERBOARD.prepare(this.con);
|
||||
ResultSet result = playersStmt.executeQuery();
|
||||
ArrayList<Player> players = new ArrayList<Player>();
|
||||
Player tmpPlayer;
|
||||
while (result.next()) {
|
||||
tmpPlayer = makePlayer(result, result.getInt("id_player"));
|
||||
if (!players.contains(tmpPlayer)) {
|
||||
players.add(tmpPlayer);
|
||||
} else {
|
||||
players.get(players.indexOf(tmpPlayer)).addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
return new TreeSet<Player>(players);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SortedSet<Group> getAllGroupForChapterLeaderboard(int chapterId){
|
||||
try{
|
||||
ensureConnection();
|
||||
PreparedStatement groupsStmt = DatabaseQuery.ALL_GROUP_FOR_CHAPTER_LEADERBOARD.prepare(this.con);
|
||||
groupsStmt.setInt(1, chapterId);
|
||||
ResultSet result = groupsStmt.executeQuery();
|
||||
List<Group> groups = new ArrayList<Group>();
|
||||
Group tmpGroup;
|
||||
while (result.next()) {
|
||||
tmpGroup = makeGroup(result);
|
||||
if (tmpGroup != null) {
|
||||
int gPosition = groups.indexOf(tmpGroup);
|
||||
if (gPosition < 0) {
|
||||
tmpGroup.addPlayer(makeGroupPlayer(result));
|
||||
groups.add(tmpGroup);
|
||||
} else {
|
||||
groups.get(gPosition).addPlayer(makeGroupPlayer(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TreeSet<Group>(groups);
|
||||
}catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Badge getBadge(int badgeId) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_BADGE.prepare(this.con);
|
||||
completionsStmt.setInt(1, badgeId);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makeBadge(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific chapter
|
||||
*
|
||||
* @param id The id of the chapter
|
||||
* @return The chapter or null if an error occurred
|
||||
*/
|
||||
public Chapter getChapter(int id) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.SPECIFIC_CHAPTER_QUERY.prepare(this.con);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
List<Puzzle> puzzles = getPuzzlesInChapter(id);
|
||||
chapter.setPuzzles(puzzles);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Chapter getChapter(Puzzle puzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.CHAPTER_FROM_PUZZLE.prepare(this.con);
|
||||
chapterStmt.setInt(1, puzzle.getId());
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
List<Puzzle> puzzles = getPuzzlesInChapter(chapter.getId());
|
||||
chapter.setPuzzles(puzzles);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all chapters in the database
|
||||
*
|
||||
* @return List of all chapters or null if an error occurred
|
||||
*/
|
||||
public List<Chapter> getAllChapters() {
|
||||
try {
|
||||
List<Chapter> chapterList = new ArrayList<>();
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.ALL_CHAPTERS_QUERY.prepare(this.con);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
while (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
chapter.setPuzzles(getPuzzlesInChapter(chapter.getId()));
|
||||
chapterList.add(chapter);
|
||||
}
|
||||
return chapterList;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Group> getAllGroups() {
|
||||
try {
|
||||
ensureConnection();
|
||||
List<Group> list = new ArrayList<>();
|
||||
PreparedStatement stmt = DatabaseQuery.ALL_GROUPS.prepare(this.con);
|
||||
ResultSet groupResult = stmt.executeQuery();
|
||||
while (groupResult.next())
|
||||
list.add(makeGroup(groupResult));
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Group> getAllGroupsByChapter(int chapter){
|
||||
try {
|
||||
ensureConnection();
|
||||
List<Group> list = new ArrayList<>();
|
||||
PreparedStatement stmt = DatabaseQuery.ALL_GROUPS_BY_CHAPTER.prepare(this.con);
|
||||
stmt.setInt(1, chapter);
|
||||
ResultSet groupResult = stmt.executeQuery();
|
||||
while (groupResult.next())
|
||||
list.add(makeGroup(groupResult));
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a pseudo is available
|
||||
*
|
||||
* @param pseudo The pseudo to check
|
||||
* @return True if the pseudo is available, false if it's already taken
|
||||
*/
|
||||
public boolean checkPseudoAvailability(String pseudo) {
|
||||
return checkAvailability(pseudo, DatabaseQuery.CHECK_PSEUDO_AVAILABLE_QUERY.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an email is available
|
||||
*
|
||||
* @param email The email to check
|
||||
* @return True if the email is available, false if it's already taken
|
||||
*/
|
||||
public boolean checkEmailAvailability(String email) {
|
||||
return checkAvailability(email, DatabaseQuery.CHECK_EMAIL_AVAILABLE_QUERY.toString());
|
||||
}
|
||||
|
||||
private boolean checkAvailability(String queriedString, String correspondingQuery) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = con.prepareStatement(correspondingQuery);
|
||||
statement.setString(1, queriedString);
|
||||
ResultSet result = statement.executeQuery();
|
||||
return !result.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new user
|
||||
*
|
||||
* @param pseudo The pseudo of the user
|
||||
* @param email The email of the user
|
||||
* @param password The password of the user
|
||||
* @param firstname The firstname of the user
|
||||
* @param lastname The lastname of the user
|
||||
* @param description The description of the user
|
||||
* @param sgroup The group of the user
|
||||
* @param avatar The avatar of the user
|
||||
* @return True if the user was registered, false if an error occurred
|
||||
*/
|
||||
public int register(String pseudo, String email, String password, String firstname, String lastname,
|
||||
String description, String sgroup, String avatar) {
|
||||
try {
|
||||
String pass = Password.hash(password).withArgon2().getResult();
|
||||
System.out.println("pass("+pass.length()+") "+pass);
|
||||
ensureConnection();
|
||||
con.setAutoCommit(false);
|
||||
try (PreparedStatement playerStatement = con.prepareStatement(DatabaseQuery.REGISTER_QUERY.toString(),
|
||||
Statement.RETURN_GENERATED_KEYS)) {
|
||||
playerStatement.setString(1, pseudo);
|
||||
playerStatement.setString(2, email);
|
||||
playerStatement.setString(3, Password.hash(password).withArgon2().getResult());
|
||||
playerStatement.setString(4, firstname);
|
||||
playerStatement.setString(5, lastname);
|
||||
playerStatement.setString(6, description);
|
||||
playerStatement.setString(7, avatar);
|
||||
if (playerStatement.executeUpdate() == 1) {
|
||||
ResultSet inserted = playerStatement.getGeneratedKeys();
|
||||
if (inserted.next()) {
|
||||
int newPlayerId = inserted.getInt(1);
|
||||
if (!sgroup.isEmpty()) {
|
||||
try (PreparedStatement containsGroupsStatement = con
|
||||
.prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
||||
containsGroupsStatement.setInt(1, newPlayerId);
|
||||
containsGroupsStatement.setString(2, sgroup);
|
||||
containsGroupsStatement.executeUpdate();
|
||||
}
|
||||
}
|
||||
con.commit();
|
||||
con.setAutoCommit(true);
|
||||
return newPlayerId;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
con.rollback();
|
||||
con.setAutoCommit(true);
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login a user
|
||||
*
|
||||
* @param username The username of the user
|
||||
* @param password The password of the user
|
||||
* @return id the id of the user, -1 if not login successefuly
|
||||
*/
|
||||
public int login(String username, String password) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = con.prepareStatement(DatabaseQuery.CHECK_PASSWORD.toString());
|
||||
DatabaseQuery.PUZZLES_IN_CHAPTER_QUERY.prepare(this.con);
|
||||
statement.setString(1, username);
|
||||
ResultSet result = statement.executeQuery();
|
||||
if (result.next()) {
|
||||
String hashedPassword = result.getString("passwd");
|
||||
if (Password.check(password, hashedPassword).withArgon2())
|
||||
return result.getInt("id_player");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Completion insertOrUpdatePuzzleResponse(int puzzleId, int userId, String fileName, byte[] code,
|
||||
byte[] response, Puzzle currentPuzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
Completion completion = getCompletionGroup(userId, puzzleId);
|
||||
if (completion == null){
|
||||
System.out.println("Completion is null");
|
||||
completion = new Completion(userId, puzzleId, fileName, code, response, currentPuzzle);
|
||||
insertCompletion(completion);
|
||||
} else {
|
||||
System.out.println(completion);
|
||||
completion.addTry(currentPuzzle, response, getChapter(currentPuzzle).getId());
|
||||
int lastUserId = completion.getPlayerId();
|
||||
completion.updatePlayer(userId);
|
||||
updateCompletion(completion, lastUserId);
|
||||
}
|
||||
return completion;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void insertCompletion(Completion newCompletion) throws SQLException {
|
||||
PreparedStatement statement = DatabaseQuery.INSERT_COMPLETION.prepare(this.con);
|
||||
statement.setInt(1, newCompletion.getPuzzleId());
|
||||
statement.setInt(2, newCompletion.getPlayerId());
|
||||
statement.setInt(3, newCompletion.getTries());
|
||||
statement.setBytes(4, newCompletion.getCode());
|
||||
statement.setString(5, newCompletion.getFileName());
|
||||
statement.setInt(6, newCompletion.getScore());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
public boolean insertGroup(Group group, PeerAtUser creator) throws SQLException {
|
||||
Integer groupId = getGroupId(group);
|
||||
if (groupId == null)
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
|
||||
statement.setString(1, group.getName());
|
||||
statement.setObject(2, group.getLinkToChapter());
|
||||
// statement.setObject(3, group.getLinkToPuzzle());
|
||||
if (statement.executeUpdate() >= 0)
|
||||
return insertUserInGroup(group, creator);
|
||||
return false;
|
||||
}
|
||||
|
||||
public Group getPlayerGroup(int user, Integer chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_FOR_PLAYER.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setObject(2, chapter);
|
||||
// stmt.setObject(3, puzzle);
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeGroup(result);
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer getGroupId(Group group) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con);
|
||||
stmt.setString(1, group.getName());
|
||||
stmt.setObject(2, group.getLinkToChapter());
|
||||
// stmt.setObject(3, group.getLinkToPuzzle());
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("id_group");
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException{
|
||||
Integer id = getGroupId(group);
|
||||
if(id != null){
|
||||
int howmany = numberInGroup(id);
|
||||
System.out.println("join group, already have "+howmany);
|
||||
if(howmany > config.getGroupMaxPlayers()) return false;
|
||||
}
|
||||
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
if (id != null && alreadyInGroup == null) {
|
||||
PreparedStatement stmt = DatabaseQuery.INSERT_PLAYER_IN_GROUP.prepare(this.con);
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int numberInGroup(int group) throws SQLException{
|
||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_USERS_COUNT.prepare(this.con);
|
||||
stmt.setInt(1, group);
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if(result.next()) return result.getInt("howmany");
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean leaveGroup(Group group, PeerAtUser user) throws SQLException {
|
||||
Integer id = getGroupId(group);
|
||||
if (id != null) {
|
||||
PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con);
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void updateCompletion(Completion completionToUpdate, int user) throws SQLException{
|
||||
System.out.println("update "+completionToUpdate);
|
||||
PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con);
|
||||
statement.setInt(1, completionToUpdate.getTries());
|
||||
statement.setInt(2, completionToUpdate.getScore());
|
||||
statement.setInt(3, completionToUpdate.getPlayerId());
|
||||
statement.setInt(4, completionToUpdate.getPuzzleId());
|
||||
statement.setInt(5, user);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
|
||||
//ADMIN
|
||||
public Chapter getAdminChapter(int id){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.GET_CHAPTER.prepare(this.con);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Puzzle getAdminPuzzle(int id){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.GET_PUZZLE.prepare(this.con);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
return makePuzzle(chapterResult);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Puzzle> getAdminPuzzles(){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.GET_PUZZLES.prepare(this.con);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
List<Puzzle> list = new ArrayList<>();
|
||||
while(chapterResult.next()){
|
||||
list.add(makePuzzle(chapterResult));
|
||||
}
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Tag> getAdminTags(){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.GET_TAGS.prepare(this.con);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
List<Tag> list = new ArrayList<>();
|
||||
while(chapterResult.next()){
|
||||
list.add(new Tag(chapterResult.getInt("id_tag"), chapterResult.getString("name")));
|
||||
}
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean adminAddChapter(Chapter chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.ADD_CHAPTER.prepare(this.con);
|
||||
statement.setString(1, chapter.getName());
|
||||
statement.setTimestamp(2, chapter.getStartDate());
|
||||
statement.setTimestamp(3, chapter.getEndDate());
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminAddPuzzle(Puzzle puzzle, int chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.ADD_PUZZLE.prepare(this.con);
|
||||
statement.setString(1, puzzle.getName());
|
||||
statement.setString(2, puzzle.getContent());
|
||||
statement.setBytes(3, puzzle.getSoluce());
|
||||
statement.setString(4, "");
|
||||
statement.setInt(5, puzzle.getScoreMax());
|
||||
statement.setInt(6, chapter);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminAddTag(String name) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.ADD_TAG.prepare(this.con);
|
||||
statement.setString(1, name);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdateChapter(int id, Chapter chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.EDIT_CHAPTER.prepare(this.con);
|
||||
statement.setString(1, chapter.getName());
|
||||
statement.setTimestamp(2, chapter.getStartDate());
|
||||
statement.setTimestamp(3, chapter.getEndDate());
|
||||
statement.setInt(4, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdatePuzzle(int id, Puzzle puzzle, int chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.EDIT_PUZZLE.prepare(this.con);
|
||||
statement.setString(1, puzzle.getName());
|
||||
statement.setString(2, puzzle.getContent());
|
||||
statement.setBytes(3, puzzle.getSoluce());
|
||||
statement.setString(4, "");
|
||||
statement.setInt(5, puzzle.getScoreMax());
|
||||
statement.setInt(6, chapter);
|
||||
statement.setInt(7, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdateTag(Tag tag) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.EDIT_TAG.prepare(this.con);
|
||||
statement.setString(1, tag.getName());
|
||||
statement.setInt(2, tag.getId());
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeleteChapter(int id) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.DELETE_CHAPTER.prepare(this.con);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeletePuzzle(int id) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.DELETE_PUZZLE.prepare(this.con);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeleteTag(int id) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.DELETE_TAG.prepare(this.con);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ import java.util.regex.Matcher;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Badge;
|
||||
import dev.peerat.backend.repository.DatabaseBadgeRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -15,10 +16,10 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class BadgeDetails implements Response {
|
||||
|
||||
private final DatabaseRepository databaseRepo;
|
||||
private final DatabaseBadgeRepository databaseRepo;
|
||||
|
||||
public BadgeDetails(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.databaseRepo = databaseRepo.getBadgeRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/badge/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du badge")
|
||||
|
|
|
@ -6,6 +6,9 @@ import dev.peerat.backend.bonus.extract.RouteDoc;
|
|||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseCompletionRepository;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -17,10 +20,12 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class ChapterElement implements Response {
|
||||
|
||||
private final DatabaseRepository databaseRepo;
|
||||
private final DatabaseChapterRepository chapterRepo;
|
||||
private final DatabaseCompletionRepository completionRepo;
|
||||
|
||||
public ChapterElement(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
public ChapterElement(DatabaseRepository repo) {
|
||||
this.chapterRepo = repo.getChapterRepository();
|
||||
this.completionRepo = repo.getCompletionRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/chapter/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du chapitre demander")
|
||||
|
@ -28,7 +33,7 @@ public class ChapterElement implements Response {
|
|||
|
||||
@Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
Chapter chapter = databaseRepo.getChapter(Integer.parseInt(matcher.group(1)));
|
||||
Chapter chapter = chapterRepo.getChapter(Integer.parseInt(matcher.group(1)));
|
||||
if (chapter != null){
|
||||
JsonMap chapterJSON = new JsonMap();
|
||||
chapterJSON.set("id", chapter.getId());
|
||||
|
@ -46,7 +51,7 @@ public class ChapterElement implements Response {
|
|||
puzzleJSON.set("name", puzzle.getName());
|
||||
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||
if (puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||
int score = this.databaseRepo.getScore(user.getId(), puzzle.getId());
|
||||
int score = this.completionRepo.getScore(user.getId(), puzzle.getId());
|
||||
if(score >= 0) puzzleJSON.set("score", score);
|
||||
puzzleJSON.set("show", puzzle.hasStarted());
|
||||
puzzles.add(puzzleJSON);
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.regex.Matcher;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -16,10 +17,10 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class ChapterList implements Response {
|
||||
|
||||
private final DatabaseRepository databaseRepo;
|
||||
private final DatabaseChapterRepository databaseRepo;
|
||||
|
||||
public ChapterList(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.databaseRepo = databaseRepo.getChapterRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/chapters", responseCode = 200, responseDescription = "JSON contenant les informations des chapitres")
|
||||
|
|
|
@ -38,9 +38,9 @@ public class Leaderboard implements Response {
|
|||
}
|
||||
|
||||
public final void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException {
|
||||
Chapter chInfo = databaseRepo.getChapter(chapterId);
|
||||
Chapter chInfo = databaseRepo.getChapterRepository().getChapter(chapterId);
|
||||
|
||||
SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
|
||||
SortedSet<Group> allGroupsForChapter = databaseRepo.getLeaderboardRepository().getAllGroupForChapterLeaderboard(chapterId);
|
||||
JsonMap leaderboardJSON = new JsonMap();
|
||||
if (chInfo.getStartDate() != null)
|
||||
leaderboardJSON.set("start_date", chInfo.getStartDate().toString());
|
||||
|
@ -69,7 +69,7 @@ public class Leaderboard implements Response {
|
|||
}
|
||||
|
||||
public final void playersLeaderboard(HttpWriter writer) throws IOException {
|
||||
SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard();
|
||||
SortedSet<Player> allPlayers = databaseRepo.getLeaderboardRepository().getAllPlayerForLeaderboard();
|
||||
JsonArray playersJSON = new JsonArray();
|
||||
if (allPlayers != null) {
|
||||
for (Player player : allPlayers) {
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.regex.Matcher;
|
|||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -15,10 +16,10 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class PlayerDetails implements Response {
|
||||
|
||||
private final DatabaseRepository databaseRepo;
|
||||
private final DatabasePlayerRepository databaseRepo;
|
||||
|
||||
public PlayerDetails(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.databaseRepo = databaseRepo.getPlayerRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/player/{id}", responseCode = 200, responseDescription = "JSON contenant les informations de l'utilisateur")
|
||||
|
|
|
@ -30,9 +30,9 @@ public class PuzzleElement implements Response {
|
|||
|
||||
@Route(path = "^\\/puzzle\\/([0-9]+)$", needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher));
|
||||
Puzzle puzzle = databaseRepo.getPuzzleRepository().getPuzzle(extractId(matcher));
|
||||
if (puzzle != null){
|
||||
Chapter chapter = this.databaseRepo.getChapter(puzzle);
|
||||
Chapter chapter = this.databaseRepo.getChapterRepository().getChapter(puzzle);
|
||||
if(chapter.getStartDate() != null){
|
||||
if(LocalDateTime.now().isBefore(chapter.getStartDate().toLocalDateTime())){
|
||||
context.response(423);
|
||||
|
@ -54,7 +54,7 @@ public class PuzzleElement implements Response {
|
|||
puzzleJSON.set("content", puzzle.getContent());
|
||||
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||
if(puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||
Completion completion = this.databaseRepo.getCompletionGroup(user.getId(), puzzle.getId());
|
||||
Completion completion = this.databaseRepo.getCompletionRepository().getCompletionGroup(user.getId(), puzzle.getId());
|
||||
if(completion != null && completion.getScore() >= 0){
|
||||
puzzleJSON.set("score", completion.getScore());
|
||||
puzzleJSON.set("tries", completion.getTries());
|
||||
|
|
|
@ -58,18 +58,18 @@ public class PuzzleResponse implements Response {
|
|||
|
||||
//saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
|
||||
JsonMap responseJSON = new JsonMap();
|
||||
if(this.databaseRepo.getScore(user.getId(), received.getPuzzleId()) > 0){
|
||||
if(this.databaseRepo.getCompletionRepository().getScore(user.getId(), received.getPuzzleId()) > 0){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
|
||||
Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId());
|
||||
Puzzle currentPuzzle = databaseRepo.getPuzzleRepository().getPuzzle(received.getPuzzleId());
|
||||
if(!currentPuzzle.hasStarted()){
|
||||
context.response(423);
|
||||
return;
|
||||
}
|
||||
|
||||
Chapter chapter = this.databaseRepo.getChapter(currentPuzzle);
|
||||
Chapter chapter = this.databaseRepo.getChapterRepository().getChapter(currentPuzzle);
|
||||
if(!chapter.hasStarted()){
|
||||
context.response(423);
|
||||
return;
|
||||
|
@ -86,7 +86,7 @@ public class PuzzleResponse implements Response {
|
|||
context.response(423);
|
||||
return;
|
||||
}
|
||||
Group group = this.databaseRepo.getPlayerGroup(user.getId(), chapter.getId());
|
||||
Group group = this.databaseRepo.getGroupRepository().getPlayerGroup(user.getId(), chapter.getId());
|
||||
if(group == null){
|
||||
context.response(423);
|
||||
return;
|
||||
|
@ -94,7 +94,7 @@ public class PuzzleResponse implements Response {
|
|||
}
|
||||
|
||||
|
||||
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
|
||||
Completion completion = databaseRepo.getCompletionRepository().insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
|
||||
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
|
||||
if(completion == null){
|
||||
context.response(400);
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.util.regex.Matcher;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseCompletionRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -13,10 +14,10 @@ import dev.peerat.framework.Route;
|
|||
|
||||
public class Result implements Response {
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseCompletionRepository repo;
|
||||
|
||||
public Result(DatabaseRepository repo) {
|
||||
this.repo = repo;
|
||||
this.repo = repo.getCompletionRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/result/<id>", responseCode = 200, responseDescription = "Le score")
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.sql.Timestamp;
|
|||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -17,10 +18,10 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class ChapterController{
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseAdminRepository repo;
|
||||
|
||||
public ChapterController(DatabaseRepository repo){
|
||||
this.repo = repo;
|
||||
this.repo = repo.getAdminRepository();
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/chapter/$", type = POST, needLogin = true)
|
||||
|
|
|
@ -5,6 +5,8 @@ import java.util.regex.Matcher;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -20,12 +22,14 @@ public class LogController {
|
|||
|
||||
private Locker<Context> contextLocker;
|
||||
private Locker<Throwable> exceptionLocker;
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseAdminRepository repo;
|
||||
private DatabasePlayerRepository playerRepo;
|
||||
|
||||
public LogController(Router<PeerAtUser> router, DatabaseRepository repo){
|
||||
this.contextLocker = router.getLogger();
|
||||
this.exceptionLocker = router.getExceptionLogger();
|
||||
this.repo = repo;
|
||||
this.repo = repo.getAdminRepository();
|
||||
this.playerRepo = repo.getPlayerRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/admin/logs", responseCode = 200, responseDescription = "L'utilisateur peux voir les logs en directe")
|
||||
|
@ -35,7 +39,7 @@ public class LogController {
|
|||
public void logs(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
log(reader, writer, this.contextLocker, (json, instance) -> {
|
||||
json.set("logged", instance.isLogged());
|
||||
if(instance.isLogged()) json.set("pseudo", repo.getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo());
|
||||
if(instance.isLogged()) json.set("pseudo", playerRepo.getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo());
|
||||
json.set("path", instance.getPath());
|
||||
json.set("type", instance.getType().toString());
|
||||
json.set("code", instance.getResponseCode());
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.util.List;
|
|||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -18,10 +19,10 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class PuzzleController {
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseAdminRepository repo;
|
||||
|
||||
public PuzzleController(DatabaseRepository repo){
|
||||
this.repo = repo;
|
||||
this.repo = repo.getAdminRepository();
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/puzzle/$", type = POST, needLogin = true)
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.util.List;
|
|||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.Tag;
|
||||
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -18,10 +19,10 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class TagController {
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseAdminRepository repo;
|
||||
|
||||
public TagController(DatabaseRepository repo){
|
||||
this.repo = repo;
|
||||
this.repo = repo.getAdminRepository();
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/tag/$", type = POST, needLogin = true)
|
||||
|
|
|
@ -10,6 +10,8 @@ import dev.peerat.backend.bonus.extract.RouteDoc;
|
|||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.framework.Context;
|
||||
|
@ -23,11 +25,13 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
public class GroupCreate extends FormResponse {
|
||||
|
||||
private Locker<Group> locker;
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseGroupRepository repo;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
private int groupDelay;
|
||||
|
||||
public GroupCreate(DatabaseRepository repo, @Injection("groups") Locker<Group> locker, Configuration config){
|
||||
this.repo = repo;
|
||||
this.repo = repo.getGroupRepository();
|
||||
this.chapterRepo = repo.getChapterRepository();
|
||||
this.locker = locker;
|
||||
this.groupDelay = config.getGroupJoinMinutes();
|
||||
|
||||
|
@ -55,7 +59,7 @@ public class GroupCreate extends FormResponse {
|
|||
return;
|
||||
}catch(NullPointerException e){
|
||||
if(newGroup.getLinkToChapter() != null){
|
||||
Chapter chapter = this.repo.getChapter(newGroup.getLinkToChapter());
|
||||
Chapter chapter = this.chapterRepo.getChapter(newGroup.getLinkToChapter());
|
||||
if(chapter.getStartDate() != null){
|
||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||
if(LocalDateTime.now().isAfter(start)){
|
||||
|
|
|
@ -11,6 +11,8 @@ import dev.peerat.backend.model.Chapter;
|
|||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -22,14 +24,16 @@ import dev.peerat.framework.Route;
|
|||
|
||||
public class GroupJoin implements Response{
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
private DatabaseGroupRepository groupRepo;
|
||||
private int groupDelay;
|
||||
private String waitTime;
|
||||
|
||||
private final Locker<Completion> leaderboard;
|
||||
|
||||
public GroupJoin(DatabaseRepository repo, Configuration config, @Injection("leaderboard") Locker<Completion> locker){
|
||||
this.repo = repo;
|
||||
this.chapterRepo = repo.getChapterRepository();
|
||||
this.groupRepo = repo.getGroupRepository();
|
||||
this.groupDelay = config.getGroupJoinMinutes();
|
||||
this.waitTime = config.getGroupQuitMinutes();
|
||||
this.leaderboard = locker;
|
||||
|
@ -45,7 +49,7 @@ public class GroupJoin implements Response{
|
|||
Group group = new Group(reader.readJson());
|
||||
PeerAtUser user = context.getUser();
|
||||
|
||||
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
Group userGroup = this.groupRepo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
if(group.equals(userGroup)){
|
||||
context.response(403);
|
||||
return;
|
||||
|
@ -58,7 +62,7 @@ public class GroupJoin implements Response{
|
|||
}
|
||||
|
||||
if(group.getLinkToChapter() != null){
|
||||
Chapter chapter = this.repo.getChapter(group.getLinkToChapter());
|
||||
Chapter chapter = this.chapterRepo.getChapter(group.getLinkToChapter());
|
||||
if(chapter.getStartDate() != null){
|
||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||
if(LocalDateTime.now().isAfter(start)){
|
||||
|
@ -68,7 +72,7 @@ public class GroupJoin implements Response{
|
|||
}
|
||||
}
|
||||
|
||||
if (this.repo.insertUserInGroup(group, user)) {
|
||||
if (this.groupRepo.insertUserInGroup(group, user)) {
|
||||
context.response(200);
|
||||
|
||||
leaderboard.setValue(new Completion(0, 0, 0, null, 0));
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.util.regex.Matcher;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -14,10 +15,10 @@ import dev.peerat.framework.utils.json.JsonArray;
|
|||
|
||||
public class GroupList implements Response {
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseGroupRepository repo;
|
||||
|
||||
public GroupList(DatabaseRepository repo) {
|
||||
this.repo = repo;
|
||||
public GroupList(DatabaseRepository repo){
|
||||
this.repo = repo.getGroupRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/groups", responseCode = 200, responseDescription = "JSON avec la liste des groups")
|
||||
|
|
|
@ -11,6 +11,8 @@ import dev.peerat.backend.model.Chapter;
|
|||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -22,13 +24,15 @@ import dev.peerat.framework.Route;
|
|||
|
||||
public class GroupQuit implements Response{
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
private DatabaseGroupRepository groupRepo;
|
||||
private int groupDelay;
|
||||
|
||||
private final Locker<Completion> leaderboard;
|
||||
|
||||
public GroupQuit(DatabaseRepository repo, Configuration config, @Injection("leaderboard") Locker<Completion> locker){
|
||||
this.repo = repo;
|
||||
this.chapterRepo = repo.getChapterRepository();
|
||||
this.groupRepo = repo.getGroupRepository();
|
||||
this.groupDelay = config.getGroupJoinMinutes();
|
||||
|
||||
this.leaderboard = locker;
|
||||
|
@ -43,14 +47,14 @@ public class GroupQuit implements Response{
|
|||
Group group = new Group(reader.readJson());
|
||||
PeerAtUser user = context.getUser();
|
||||
|
||||
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
Group userGroup = this.groupRepo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
if(!group.equals(userGroup)){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
|
||||
if(group.getLinkToChapter() != null){
|
||||
Chapter chapter = this.repo.getChapter(group.getLinkToChapter());
|
||||
Chapter chapter = this.chapterRepo.getChapter(group.getLinkToChapter());
|
||||
if(chapter.getStartDate() != null){
|
||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||
if(LocalDateTime.now().isAfter(start)){
|
||||
|
@ -60,7 +64,7 @@ public class GroupQuit implements Response{
|
|||
}
|
||||
}
|
||||
|
||||
if (this.repo.leaveGroup(group, user)) {
|
||||
if (this.groupRepo.leaveGroup(group, user)) {
|
||||
context.response(200);
|
||||
|
||||
leaderboard.setValue(new Completion(0, 0, 0, null, 0));
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.util.regex.Matcher;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -15,10 +16,10 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class ChangePassword implements Response{
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabasePlayerRepository repo;
|
||||
|
||||
public ChangePassword(DatabaseRepository repo){
|
||||
this.repo = repo;
|
||||
this.repo = repo.getPlayerRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/user/cpw", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont mots de passe")
|
||||
|
|
|
@ -13,6 +13,7 @@ import java.util.UUID;
|
|||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
|
@ -26,14 +27,14 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
public class ForgotPassword extends FormResponse{
|
||||
|
||||
private Router<PeerAtUser> router;
|
||||
private DatabaseRepository repo;
|
||||
private DatabasePlayerRepository repo;
|
||||
private Mail mail;
|
||||
private Map<String, String> codes;
|
||||
private List<Random> randoms;
|
||||
|
||||
public ForgotPassword(Router<PeerAtUser> router, DatabaseRepository repo, Mail mail){
|
||||
this.router = router;
|
||||
this.repo = repo;
|
||||
this.repo = repo.getPlayerRepository();
|
||||
this.mail = mail;
|
||||
this.codes = new HashMap<>();
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.util.regex.Matcher;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.framework.Context;
|
||||
|
@ -17,11 +18,11 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class Login extends FormResponse{
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private DatabaseAuthRepository repo;
|
||||
private Router<PeerAtUser> router;
|
||||
|
||||
public Login(DatabaseRepository databaseRepo, Router<PeerAtUser> router){
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.repo = databaseRepo.getAuthRepository();
|
||||
this.router = router;
|
||||
}
|
||||
|
||||
|
@ -41,7 +42,7 @@ public class Login extends FormResponse{
|
|||
return;
|
||||
}
|
||||
int id;
|
||||
if((id = databaseRepo.login(json.get("pseudo"), json.get("passwd"))) >= 0){
|
||||
if((id = repo.login(json.get("pseudo"), json.get("passwd"))) >= 0){
|
||||
context.response(200,
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.jose4j.json.internal.json_simple.JSONObject;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
|
@ -44,7 +45,7 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class MailConfirmation extends FormResponse{
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private DatabaseAuthRepository databaseRepo;
|
||||
private Router<PeerAtUser> router;
|
||||
private String usersFilesPath;
|
||||
private KeyPairGenerator generator;
|
||||
|
@ -62,7 +63,7 @@ public class MailConfirmation extends FormResponse{
|
|||
@Injection("waitting") Map<String, String> playersWaiting,
|
||||
Mail mail) throws NoSuchAlgorithmException{
|
||||
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.databaseRepo = databaseRepo.getAuthRepository();
|
||||
this.router = router;
|
||||
this.usersFilesPath = initUsersFilesPath;
|
||||
this.gitToken = gitToken;
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.regex.Matcher;
|
|||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.framework.Context;
|
||||
|
@ -16,10 +17,10 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class ProfileSettings extends FormResponse{
|
||||
|
||||
private DatabaseRepository repo;
|
||||
private DatabasePlayerRepository repo;
|
||||
|
||||
public ProfileSettings(DatabaseRepository repo){
|
||||
this.repo = repo;
|
||||
this.repo = repo.getPlayerRepository();
|
||||
|
||||
validator("pseudo", "[a-zA-Z0-9&|!?{}\\[\\]%/*\\-+=:;,_#@ ]{3,100}");
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.UUID;
|
|||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
|
@ -24,14 +25,14 @@ import dev.peerat.framework.utils.json.JsonMap;
|
|||
|
||||
public class Register extends FormResponse{
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private DatabaseAuthRepository databaseRepo;
|
||||
private Map<String, String> playersWaiting;
|
||||
private Mail mail;
|
||||
private String host;
|
||||
private List<Random> randoms;
|
||||
|
||||
public Register(DatabaseRepository databaseRepo, @Injection("waitting") Map<String, String> playersWaiting, Mail mail, @Injection("issuer") String host){
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.databaseRepo = databaseRepo.getAuthRepository();
|
||||
this.playersWaiting = playersWaiting;
|
||||
this.mail = mail;
|
||||
this.host = host;
|
||||
|
|
Loading…
Add table
Reference in a new issue