diff --git a/src/be/jeffcheasey88/peeratcode/model/Chapter.java b/src/be/jeffcheasey88/peeratcode/model/Chapter.java index 0e1b3b1..38a4303 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Chapter.java +++ b/src/be/jeffcheasey88/peeratcode/model/Chapter.java @@ -1,6 +1,7 @@ package be.jeffcheasey88.peeratcode.model; import java.sql.Timestamp; +import java.time.LocalDateTime; import java.util.List; public class Chapter { @@ -33,6 +34,14 @@ public class Chapter { public void setPuzzles(List puzzles) { this.puzzles = puzzles; } + + public boolean isInCurrentTime(){ + LocalDateTime now = LocalDateTime.now(); + boolean show = true; + if(startDate != null) show &= now.isAfter(startDate.toLocalDateTime()); + if(endDate != null) show &= now.isBefore(endDate.toLocalDateTime()); + return show; + } public Timestamp getStartDate() { return startDate; diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java index 22a44f1..d97d5f1 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -33,22 +33,24 @@ public class ChapterElement implements Response { JSONObject chapterJSON = new JSONObject(); chapterJSON.put("id", chapter.getId()); chapterJSON.put("name", chapter.getName()); - if (chapter.getStartDate() != null) - chapterJSON.put("startDate", chapter.getStartDate().toString()); - if (chapter.getEndDate() != null) - chapterJSON.put("endDate", chapter.getEndDate().toString()); - JSONArray puzzles = new JSONArray(); - 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()); - if(score >= 0) puzzleJSON.put("score", score); - puzzles.add(puzzleJSON); + boolean show = chapter.isInCurrentTime(); + chapterJSON.put("show", show); + + if(show){ + JSONArray puzzles = new JSONArray(); + 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()); + if(score >= 0) puzzleJSON.put("score", score); + puzzles.add(puzzleJSON); + } + chapterJSON.put("puzzles", puzzles); } - chapterJSON.put("puzzles", puzzles); + writer.response(200, "Access-Control-Allow-Origin: *"); writer.write(chapterJSON.toJSONString()); } else { diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java index e7a7420..a73560a 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java @@ -27,23 +27,20 @@ public class ChapterList implements Response { @RouteDoc(responseCode = 400, responseDescription = "Aucun chapitre trouver") @Route(path = "^\\/chapters$", needLogin = true) - public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { + public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ List allChapters = databaseRepo.getAllChapters(); - if (allChapters != null) { + if(allChapters != null){ JSONArray chaptersJSON = new JSONArray(); for (Chapter chapter : allChapters) { JSONObject chapterJSON = new JSONObject(); chapterJSON.put("id", chapter.getId()); chapterJSON.put("name", chapter.getName()); - if (chapter.getStartDate() != null) - chapterJSON.put("startDate", chapter.getStartDate().toString()); - if (chapter.getEndDate() != null) - chapterJSON.put("endDate", chapter.getEndDate().toString()); + chapterJSON.put("show", chapter.isInCurrentTime()); chaptersJSON.add(chapterJSON); } writer.response(200, "Access-Control-Allow-Origin: *"); writer.write(chaptersJSON.toJSONString()); - } else { + }else{ writer.response(400, "Access-Control-Allow-Origin: *"); } }