package dev.peerat.backend.routes.groups;

import java.util.regex.Matcher;

import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Group;
import dev.peerat.backend.repository.DatabaseRepository;
import dev.peerat.framework.Context;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonArray;

public class GroupList implements Response {

	private DatabaseRepository repo;

	public GroupList(DatabaseRepository repo) {
		this.repo = repo;
	}
	
	@RouteDoc(path = "/groups", responseCode = 200, responseDescription = "JSON avec la liste des groups")

	@Route(path = "^\\/groups$", needLogin = true)
	public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
		context.response(200);
		JsonArray result = new JsonArray();
		for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
		writer.write(result.toString());
	}

}
