Route get groups by chapter

This commit is contained in:
jeffcheasey88 2024-03-26 21:14:22 +01:00
parent d089ff237a
commit 250758c3bf
3 changed files with 30 additions and 4 deletions

View file

@ -21,6 +21,7 @@ public enum DatabaseQuery {
// GROUPS
ALL_GROUPS("SELECT * FROM groups"),
ALL_GROUPS_BY_CHAPTER("SELECT * FROM groups WHERE fk_chapter = ?"),
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 = ?
INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"),

View file

@ -471,6 +471,22 @@ public class DatabaseRepository {
}
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

View file

@ -22,11 +22,20 @@ public class GroupList implements Response {
@RouteDoc(path = "/groups", responseCode = 200, responseDescription = "JSON avec la liste des groups")
@Route(path = "^\\/groups$", needLogin = true)
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
context.response(200);
@Route(path = "^\\/groups\\/?(\\d+)?$", needLogin = true)
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
String param = matcher.group(1);
if(param == null){
JsonArray result = new JsonArray();
for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
context.response(200);
writer.write(result.toString());
return;
}
int chapter = Integer.parseInt(param);
JsonArray result = new JsonArray();
for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
for(Group group : this.repo.getAllGroupsByChapter(chapter)) result.add(group.toJson());
context.response(200);
writer.write(result.toString());
}