package be.jeffcheasey88.peeratcode.routes.groups; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; import java.util.regex.Matcher; import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.framework.HttpReader; import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Locker; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; import be.jeffcheasey88.peeratcode.framework.User; import be.jeffcheasey88.peeratcode.model.Completion; import be.jeffcheasey88.peeratcode.model.Group; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; public class GroupCreate implements Response { private Locker locker; private DatabaseRepository repo; public GroupCreate(DatabaseRepository repo, Locker locker){ this.repo = repo; this.locker = locker; } @Route(path = "^\\/groupCreate$", type = POST, needLogin = true) public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ Group group = new Group((JSONObject) HttpUtil.readJson(reader)); Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter(), group.getLinkToPuzzle()); if(group.equals(userGroup)){ HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); return; } if (this.repo.insertGroup(group, user)) { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); locker.setValue(group); } else { HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); } } }