import { Role } from "@/types";

export const getRoles = async (token : string) => {
    return await fetch('/api/role', {
        method : 'GET',
        headers : {
            'Accept' : 'application/json',
            'Authorization': `Bearer ${token}`
        }
    });
}

export const getRoleById = async (id : number, token : string) => {
    return await fetch(`/api/role/${id}`, {
        method : 'GET',
        headers : {
            'Accept' : 'application/json',
            'Authorization': `Bearer ${token}`
        }
    });
}

export const saveRole = async (role: Role, token : string) => {
    if(role.id){
        return await fetch(`/api/role/${role.id}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(role)
        });
    } else {
        return await fetch('/api/role', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(role)
        });
    }
}

