CREATE OR REPLACE FUNCTION git."ReadableRepos" (
        gituserid integer
)
RETURNS table(
	ID integer,
	path character varying(255),
	name character varying(255),
	owner character varying(255),
	description character varying,
	groupeID integer,
	logoUrl character varying(255)
)
LANGUAGE plpgsql
AS $code$
BEGIN
		RETURN QUERY
        /* user.repo */
        SELECT repos.*
        FROM git.perms
        JOIN git.repos ON "repoID"=repos."ID"
        WHERE "accessType">'READ' AND "userID"=gituserid
		UNION
        /* groupe.repo */
        SELECT repos.*
        FROM git.perms
        JOIN git.repos ON "repoID"=repos."ID"
        JOIN git.appartenances ON appartenances."groupeID"=-perms."userID"
        WHERE "accessType">'READ' AND appartenances."utilisateurID"=gituserid
		UNION
		/* user.repoGroup */
        SELECT repos.*
        FROM git.perms
        JOIN git.repos ON "repoID"=-repos."groupeID"
        WHERE "accessType">'READ' AND "userID"=gituserid
		UNION
		/* groupe.repoGroupe */
        SELECT repos.*
        FROM git.perms
        JOIN git.appartenances ON appartenances."groupeID"=-perms."userID"
        JOIN git.repos ON "repoID"=-repos."groupeID"
        WHERE "accessType">'READ' AND "userID"=gituserid;
END
$code$
