Skip to content

Instantly share code, notes, and snippets.

@zloadmin
Created November 28, 2019 09:05
Show Gist options
  • Save zloadmin/d059f68a8bd010a97d18fa24ecc25d8e to your computer and use it in GitHub Desktop.
Save zloadmin/d059f68a8bd010a97d18fa24ecc25d8e to your computer and use it in GitHub Desktop.
CompanyController.php
<?php
namespace App\Http\Controllers\Api;
use App\Company;
use App\Http\Controllers\Controller;
use App\Http\Resources\CompanyCollection;
use App\Http\Resources\SingleCompany;
use Illuminate\Http\Request;
/**
* @group Companies
*
* APIs for managing companies
*/
class CompanyController extends ApiController
{
/**
* Get list of companies
* @queryParam limit int the limit of company. Defaults to 20 Example: 20
* @queryParam offset int the offset of company. Defaults to 0 Example: 0
* @queryParam type string type of company - company or factory. Defaults to company Example: company
*/
public function index()
{
return new CompanyCollection($this->getIndexCompanies());
}
/**
* Get the company
* @response 404 {
* "message": "Company not found!"
* }
* @urlParam company_id required The ID of the company. Example: 129
*/
public function show($company_id)
{
$company = $this->getCompanyById($company_id);
if(is_null($company)) return $this->respondNotFound('Company not found!');
return $this->respondData(new SingleCompany($company));
}
private function getCompanyCategory() : string
{
return request('type') == 'factory' ? 'uspeshnyj-opyt' : 'predpriyatiya';
}
private function getIndexCompanies()
{
return Company::with('meta')
->published()
->hasMeta('fw_options')
->taxonomy('predpriyatie-category', $this->getCompanyCategory())
->newest()
->offset($this->getOffset())
->limit($this->getLimit())
->get();
}
private function getCompanyById($company_id)
{
return Company::with('meta')
->published()
->hasMeta('fw_options')
->whereId($company_id)
->first();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment