Route get groups by chapter
This commit is contained in:
parent
d089ff237a
commit
250758c3bf
3 changed files with 30 additions and 4 deletions
|
@ -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 (?,?)"),
|
||||
|
|
|
@ -472,6 +472,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
|
||||
*
|
||||
|
|
|
@ -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)
|
||||
@Route(path = "^\\/groups\\/?(\\d+)?$", needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
context.response(200);
|
||||
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.getAllGroupsByChapter(chapter)) result.add(group.toJson());
|
||||
context.response(200);
|
||||
writer.write(result.toString());
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue