Refractor back for new versions of the framework

This commit is contained in:
jeffcheasey88 2023-07-26 17:03:21 +02:00
parent f4dd642b78
commit 5481dbd2cf
15 changed files with 85 additions and 29 deletions

View file

@ -12,6 +12,6 @@
</classpathentry>
<classpathentry kind="lib" path="Treasure.jar"/>
<classpathentry exported="true" kind="lib" path="JDA-5.0.0-beta.8-withDependencies.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/jeffc/eclipse-workspace/peer-at-code-backend/PeerAtCodeFramework.jar"/>
<classpathentry exported="true" kind="lib" path="PeerAtCodeFramework.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

Binary file not shown.

View file

@ -13,6 +13,7 @@ import be.jeffcheasey88.peeratcode.framework.Router;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Completion;
import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
import be.jeffcheasey88.peeratcode.routes.BadgeDetails;
import be.jeffcheasey88.peeratcode.routes.ChapterElement;
@ -38,8 +39,14 @@ public class Main{
Class.forName("com.mysql.cj.jdbc.Driver");
DatabaseRepository repo = new DatabaseRepository(config);
Router router = new Router(config.getTokenIssuer(),
config.getTokenExpiration());
Router<PeerAtUser> router = new Router<PeerAtUser>()
.configureJwt(
(builder) -> builder.setExpectedIssuer(config.getTokenIssuer()),
(claims) -> {
claims.setIssuer(config.getTokenIssuer()); // who creates the token and signs it
claims.setExpirationTimeMinutesInTheFuture(config.getTokenExpiration());
},
(claims) -> new PeerAtUser(claims));
router.setDefault((matcher, user, reader, writer) -> {
writer.response(404, "Access-Control-Allow-Origin: *");
@ -50,11 +57,11 @@ public class Main{
router.register(new Response(){
@Route(path = "^(.*)$", type = OPTIONS)
@Override
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
writer.response(200, "Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: *", "Access-Control-Allow-Headers: *");
}
});
initRoutes(router, repo, config);
@ -66,7 +73,7 @@ public class Main{
router.listen(config.getTcpPort(), config.useSsl());
}
private static void initRoutes(Router router, DatabaseRepository repo, Configuration config){
private static void initRoutes(Router<PeerAtUser> router, DatabaseRepository repo, Configuration config){
router.register(new ChapterElement(repo));
router.register(new ChapterList(repo));
router.register(new PuzzleElement(repo));

View file

@ -0,0 +1,28 @@
package be.jeffcheasey88.peeratcode.model;
import org.jose4j.jwt.JwtClaims;
public class PeerAtUser extends be.jeffcheasey88.peeratcode.framework.User{
private int id;
public PeerAtUser(int id){
this.id = id;
}
public PeerAtUser(JwtClaims claims){
this.id = ((Long) claims.getClaimValue("id")).intValue();
}
@Override
public void write(JwtClaims claims){
claims.setClaim("id", this.id);
}
public int getId(){
return this.id;
}
}

View file

@ -16,11 +16,11 @@ import com.password4j.Hash;
import com.password4j.Password;
import be.jeffcheasey88.peeratcode.Configuration;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Badge;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Completion;
import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.model.Player;
import be.jeffcheasey88.peeratcode.model.Puzzle;
@ -620,7 +620,7 @@ public class DatabaseRepository {
statement.executeUpdate();
}
public boolean insertGroup(Group group, User creator) throws SQLException {
public boolean insertGroup(Group group, PeerAtUser creator) throws SQLException {
Integer groupId = getGroupId(group);
if (groupId == null)
ensureConnection();
@ -659,7 +659,7 @@ public class DatabaseRepository {
return null;
}
public boolean insertUserInGroup(Group group, User user) throws SQLException {
public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException {
Integer id = getGroupId(group);
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter());
if (id != null && alreadyInGroup == null) {
@ -673,7 +673,7 @@ public class DatabaseRepository {
return false;
}
public boolean leaveGroup(Group group, User user) throws SQLException {
public boolean leaveGroup(Group group, PeerAtUser user) throws SQLException {
Integer id = getGroupId(group);
if (id != null) {
PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con);

View file

@ -12,6 +12,7 @@ import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.model.Puzzle;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
@ -38,13 +39,14 @@ public class ChapterElement implements Response {
if (chapter.getEndDate() != null)
chapterJSON.put("endDate", chapter.getEndDate().toString());
JSONArray puzzles = new JSONArray();
PeerAtUser peerat = (PeerAtUser)user;
for (Puzzle puzzle : chapter.getPuzzles()){
JSONObject puzzleJSON = new JSONObject();
puzzleJSON.put("id", puzzle.getId());
puzzleJSON.put("name", puzzle.getName());
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
int score = this.databaseRepo.getScore(user.getId(), puzzle.getId());
int score = this.databaseRepo.getScore(peerat.getId(), puzzle.getId());
if(score >= 0) puzzleJSON.put("score", score);
puzzles.add(puzzleJSON);
}

View file

@ -13,14 +13,15 @@ import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.Router;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class Login implements Response {
private DatabaseRepository databaseRepo;
private Router router;
private Router<PeerAtUser> router;
public Login(DatabaseRepository databaseRepo, Router router) {
public Login(DatabaseRepository databaseRepo, Router<PeerAtUser> router) {
this.databaseRepo = databaseRepo;
this.router = router;
}
@ -43,7 +44,7 @@ public class Login implements Response {
if ((id = databaseRepo.login(pseudo, password)) >= 0) {
writer.response(200, "Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(id));
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
return;
}
}

View file

@ -11,6 +11,7 @@ import be.jeffcheasey88.peeratcode.framework.HttpWriter;
import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.model.Player;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
@ -31,7 +32,8 @@ public class PlayerDetails implements Response {
if (matcher.group(1) != null) {
player = databaseRepo.getPlayerDetails(matcher.group(1));
} else {
player = databaseRepo.getPlayerDetails(user.getId());
PeerAtUser peerat = (PeerAtUser)user;
player = databaseRepo.getPlayerDetails(peerat.getId());
}
JSONObject playerJSON = new JSONObject();
if (player != null) {

View file

@ -13,6 +13,7 @@ import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Completion;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.model.Puzzle;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
@ -46,13 +47,15 @@ public class PuzzleElement implements Response {
// }
// }
PeerAtUser peerat = (PeerAtUser)user;
JSONObject puzzleJSON = new JSONObject();
puzzleJSON.put("id", puzzle.getId());
puzzleJSON.put("name", puzzle.getName());
puzzleJSON.put("content", puzzle.getContent());
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
if(puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
Completion completion = this.databaseRepo.getCompletionGroup(user.getId(), puzzle.getId());
Completion completion = this.databaseRepo.getCompletionGroup(peerat.getId(), puzzle.getId());
if(completion != null && completion.getScore() >= 0){
puzzleJSON.put("score", completion.getScore());
puzzleJSON.put("tries", completion.getTries());

View file

@ -23,6 +23,7 @@ import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Completion;
import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.model.Player;
import be.jeffcheasey88.peeratcode.model.Puzzle;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
@ -52,9 +53,11 @@ public class PuzzleResponse implements Response {
writer.response(400, "Access-Control-Allow-Origin: *");
return;
}
PeerAtUser peerat = (PeerAtUser)user;
//saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
JSONObject responseJSON = new JSONObject();
if(this.databaseRepo.getScore(user.getId(), received.getPuzzleId()) > 0){
if(this.databaseRepo.getScore(peerat.getId(), received.getPuzzleId()) > 0){
writer.response(403, "Access-Control-Allow-Origin: *");
return;
}
@ -79,7 +82,7 @@ public class PuzzleResponse implements Response {
writer.response(423, "Access-Control-Allow-Origin: *");
return;
}
Group group = this.databaseRepo.getPlayerGroup(user.getId(), chapter.getId());
Group group = this.databaseRepo.getPlayerGroup(peerat.getId(), chapter.getId());
if(group == null){
writer.response(423, "Access-Control-Allow-Origin: *");
return;
@ -87,7 +90,7 @@ public class PuzzleResponse implements Response {
}
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), peerat.getId(),
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
if(completion == null){
writer.response(400, "Access-Control-Allow-Origin: *");

View file

@ -16,15 +16,16 @@ import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.Router;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class Register implements Response {
private DatabaseRepository databaseRepo;
private Router router;
private Router<PeerAtUser> router;
private String usersFilesPath;
public Register(DatabaseRepository databaseRepo, Router router, String initUsersFilesPath) {
public Register(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath) {
this.databaseRepo = databaseRepo;
this.router = router;
usersFilesPath = initUsersFilesPath;
@ -67,7 +68,7 @@ public class Register implements Response {
avatar)) >= 0) {
writer.response(200, "Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(id));
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
createFolderToSaveSourceCode(pseudo);
return;
}

View file

@ -8,6 +8,7 @@ import be.jeffcheasey88.peeratcode.framework.HttpWriter;
import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class Result implements Response {
@ -24,8 +25,10 @@ public class Result implements Response {
@Route(path = "^\\/result\\/(\\d+)$", needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
int puzzle = Integer.parseInt(matcher.group(1));
int score = this.repo.getScore(user.getId(), puzzle);
PeerAtUser peerat = (PeerAtUser)user;
int score = this.repo.getScore(peerat.getId(), puzzle);
if (score < 0) {
writer.response(425, "Access-Control-Allow-Origin: *");
} else {

View file

@ -14,6 +14,7 @@ import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class GroupCreate implements Response {
@ -35,8 +36,9 @@ public class GroupCreate implements Response {
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
Group newGroup = new Group(reader.readJson());
PeerAtUser peerat = (PeerAtUser)user;
if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter()) == null) {
if (this.repo.getPlayerGroup(peerat.getId(), newGroup.getLinkToChapter()) == null) {
try {
if(this.repo.getGroupId(newGroup) == null) throw new NullPointerException();
writer.response(403, "Access-Control-Allow-Origin: *");
@ -52,7 +54,7 @@ public class GroupCreate implements Response {
}
}
}
if (this.repo.insertGroup(newGroup, user)) {
if (this.repo.insertGroup(newGroup, peerat)) {
writer.response(200, "Access-Control-Allow-Origin: *");
locker.setValue(newGroup);

View file

@ -15,6 +15,7 @@ import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Completion;
import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class GroupJoin implements Response{
@ -40,8 +41,9 @@ public class GroupJoin implements Response{
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
Group group = new Group(reader.readJson());
PeerAtUser peerat = (PeerAtUser)user;
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter());
Group userGroup = this.repo.getPlayerGroup(peerat.getId(), group.getLinkToChapter());
if(group.equals(userGroup)){
writer.response(403, "Access-Control-Allow-Origin: *");
return;
@ -64,7 +66,7 @@ public class GroupJoin implements Response{
}
}
if (this.repo.insertUserInGroup(group, user)) {
if (this.repo.insertUserInGroup(group, peerat)) {
writer.response(200, "Access-Control-Allow-Origin: *");
leaderboard.setValue(new Completion(0, 0, 0, null, 0));

View file

@ -15,6 +15,7 @@ import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Completion;
import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.model.PeerAtUser;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class GroupQuit implements Response{
@ -38,8 +39,9 @@ public class GroupQuit implements Response{
@Route(path = "^\\/groupQuit$", type = POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
Group group = new Group(reader.readJson());
PeerAtUser peerat = (PeerAtUser)user;
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter());
Group userGroup = this.repo.getPlayerGroup(peerat.getId(), group.getLinkToChapter());
if(!group.equals(userGroup)){
writer.response(403, "Access-Control-Allow-Origin: *");
return;
@ -56,7 +58,7 @@ public class GroupQuit implements Response{
}
}
if (this.repo.leaveGroup(group, user)) {
if (this.repo.leaveGroup(group, peerat)) {
writer.response(200, "Access-Control-Allow-Origin: *");
leaderboard.setValue(new Completion(0, 0, 0, null, 0));