Get Score Route, wait for JWT & User
This commit is contained in:
parent
48727370d7
commit
c2257beacb
3 changed files with 59 additions and 2 deletions
|
@ -8,7 +8,6 @@ import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.net.ssl.SSLServerSocket;
|
import javax.net.ssl.SSLServerSocket;
|
||||||
import javax.net.ssl.SSLServerSocketFactory;
|
import javax.net.ssl.SSLServerSocketFactory;
|
||||||
import javax.net.ssl.SSLSocket;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
import be.jeffcheasey88.peeratcode.routes.ChapterElement;
|
import be.jeffcheasey88.peeratcode.routes.ChapterElement;
|
||||||
|
@ -16,6 +15,7 @@ import be.jeffcheasey88.peeratcode.routes.ChapterList;
|
||||||
import be.jeffcheasey88.peeratcode.routes.Login;
|
import be.jeffcheasey88.peeratcode.routes.Login;
|
||||||
import be.jeffcheasey88.peeratcode.routes.PuzzleElement;
|
import be.jeffcheasey88.peeratcode.routes.PuzzleElement;
|
||||||
import be.jeffcheasey88.peeratcode.routes.Register;
|
import be.jeffcheasey88.peeratcode.routes.Register;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.Result;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.Client;
|
import be.jeffcheasey88.peeratcode.webserver.Client;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
|
import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
|
||||||
|
@ -59,6 +59,7 @@ public class Main {
|
||||||
router.register(new PuzzleElement(repo));
|
router.register(new PuzzleElement(repo));
|
||||||
router.register(new Register(repo));
|
router.register(new Register(repo));
|
||||||
router.register(new Login(repo));
|
router.register(new Login(repo));
|
||||||
|
router.register(new Result(repo));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void startWebServer(Configuration config, Router router) throws IOException {
|
private static void startWebServer(Configuration config, Router router) throws IOException {
|
||||||
|
|
|
@ -23,7 +23,8 @@ public class DatabaseRepository {
|
||||||
private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM players WHERE email = ?";
|
private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM players WHERE email = ?";
|
||||||
private static final String REGISTER_QUERY = "INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, sgroup, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
private static final String REGISTER_QUERY = "INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, sgroup, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||||
private static final String CHECK_PASSWORD = "SELECT passwd FROM players WHERE pseudo=?";
|
private static final String CHECK_PASSWORD = "SELECT passwd FROM players WHERE pseudo=?";
|
||||||
|
private static final String SCORE = "SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?";
|
||||||
|
|
||||||
private Connection con;
|
private Connection con;
|
||||||
private Configuration config;
|
private Configuration config;
|
||||||
|
|
||||||
|
@ -77,6 +78,20 @@ public class DatabaseRepository {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getScore(int user, int puzzle){
|
||||||
|
try {
|
||||||
|
PreparedStatement stmt = this.con.prepareStatement(SCORE);
|
||||||
|
stmt.setInt(1, user);
|
||||||
|
stmt.setInt(2, puzzle);
|
||||||
|
|
||||||
|
ResultSet result = stmt.executeQuery();
|
||||||
|
if(result.next()) result.getInt("score");
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a specific chapter
|
* Get a specific chapter
|
||||||
|
|
41
src/be/jeffcheasey88/peeratcode/routes/Result.java
Normal file
41
src/be/jeffcheasey88/peeratcode/routes/Result.java
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||||
|
import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
|
||||||
|
import be.jeffcheasey88.peeratcode.webserver.HttpWriter;
|
||||||
|
import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||||
|
|
||||||
|
public class Result implements Response{
|
||||||
|
|
||||||
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
|
public Result(DatabaseRepository repo){
|
||||||
|
this.repo = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
int puzzle = Integer.parseInt(matcher.group(1));
|
||||||
|
|
||||||
|
int score = this.repo.getScore(0, puzzle);
|
||||||
|
if(score < 0) {
|
||||||
|
HttpUtil.responseHeaders(writer, 425, "Access-Control-Allow-Origin: *");
|
||||||
|
}else{
|
||||||
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
|
writer.write(score+"");
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pattern getPattern(){
|
||||||
|
return Pattern.compile("^\\/result\\/(\\d+)$");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue