Lepiej zrobić kilka endpointów, z których każdy zwraca model powiązany z repozytorium (getById, getByName, etc.).
Następnie pobierałbym dane z każdego z tych endpointów po stronie frontendu.
Nie podoba mi się takie rozwiązanie - jest to pomieszanie logiki backendowej z tym co się dzieje na froncie:
Backend:
class TestRepository implements TestRepositoryInterface
{
public function getById(int $id): TestInterface
{
}
public function getByName(string $name): TestInterface
{
}
public function getByStatus(string $status): TestInterface
{
}
}
Frontend:
$.ajax({
type: "get",
url: `/rest/V1/module/test?id=${id}`,
contentType: 'application/json',
cache: true
}).done($.proxy(function(response) {
if (response.status === 1) {
$.ajax({
type: "get",
url: `/rest/V1/module/test?name=${response.name}`,
contentType: 'application/json',
cache: true
}).done($.proxy(function(response) {
this.onDone(JSON.parse(response));
}, this))
} else {
$.ajax({
type: "get",
url: `/rest/V1/module/test?name=${response.status}`,
contentType: 'application/json',
cache: true
}).done($.proxy(function(response) {
this.onDone(JSON.parse(response));
}, this))
}
}, this)).fail($.proxy(function(response, status, error) {
this.onFail(response, status, error);
}, this))
Wolałbym całą logikę zrobić po stronie php.
Czyli coś na wzór:
Backend:
class TestRepository implements TestRepositoryInterface
{
public function getByFilterData(int $id, string $name, string $status): TestInterface
{
$test = $this->getById($id);
if($test->getStatus() === 1){
return $this->getByName($name);
} else {
return $this->getByStatus($status);
}
}
}
Frontend:
$.ajax({
type: "get",
url: `/rest/V1/module/test?id=${id}&name=${name}&status=${status}`,
contentType: 'application/json',
cache: true
}).done($.proxy(function(response) {
this.onDone(JSON.parse(response));
}, this)).fail($.proxy(function(response, status, error) {
this.onFail(response, status, error);
}, this))
Ostatnie pytanie:
Czy w danym repozytorium, które jest powiązane z rest api mogę wstrzykiwać inne repozytoria?
Jeżeli nie - jak w takim wypadku obejść konieczność pobierania danych z innych repozytoriów?