package be.jeffcheasey88.peeratcode.routes.groups; import java.util.regex.Matcher; import org.json.simple.JSONArray; import be.jeffcheasey88.peeratcode.framework.HttpReader; import be.jeffcheasey88.peeratcode.framework.HttpUtil; 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.Group; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; public class GroupList implements Response { private DatabaseRepository repo; public GroupList(DatabaseRepository repo) { this.repo = repo; } @Route(path = "^\\/groups$", needLogin = true) @Override public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); JSONArray result = new JSONArray(); for (Group group : this.repo.getAllGroups()) result.add(group.toJson()); writer.write(result.toJSONString()); } }