package org.flowcolab.account.service.client.clients;

import org.flowcolab.account.service.client.dto.role.RoleResponse;
import org.flowcolab.account.service.client.errors.role.RoleNotFound.RoleNotFoundException;
import org.flowcolab.account.service.client.services.RoleWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestOperations;

@Service
public class RoleClient implements RoleWebService {

    private static final String URL = "http://accountService/v1/role";

    private RestOperations restOperations;

    @Autowired
    public RoleClient(RestOperations restOperations) {
        this.restOperations = restOperations;
    }

    @Override
    public RoleResponse findById(long id) throws RoleNotFoundException {
        ResponseEntity<RoleResponse> response =
                restOperations.getForEntity(URL + "/{id}", RoleResponse.class, id);

        return response.getBody();
    }

    @Override
    public RoleResponse findByName(String name) throws RoleNotFoundException {
        ResponseEntity<RoleResponse> response =
                restOperations.getForEntity(URL + "?name={name}", RoleResponse.class, name);

        return response.getBody();
    }
}