peer-at-code-backend/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java
2023-04-12 10:08:52 +02:00

43 lines
1.4 KiB
Java

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.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 GroupQuit implements Response{
private DatabaseRepository repo;
public GroupQuit(DatabaseRepository repo){
this.repo = repo;
}
@Route(path = "^\\/groupQuit$", 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.leaveGroup(group, user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
} else {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
}
}
}