forked from jefponte/3s-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
239 changed files
with
14,514 additions
and
6,517 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
use Spatie\Enum\Laravel\Enum; | ||
|
||
/** | ||
* @method static self reopened() | ||
* @method static self opened() | ||
* @method static self closed() | ||
* @method static self inProgress() | ||
* @method static self reserved() | ||
* @method static self pendingResource() | ||
* @method static self pendingCustomerResponse() | ||
* @method static self committed() | ||
* @method static self canceled() | ||
*/ | ||
class OrderStatus extends Enum | ||
{ | ||
protected static function values(): array | ||
{ | ||
return [ | ||
'opened' => 'opened', | ||
'reopened' => 'reopened', | ||
'closed' => 'closed', | ||
'inProgress' => 'in progress', | ||
'reserved' => 'reserved', | ||
'pendingResource' => 'pending resource', | ||
'pendingCustomerResponse' => 'pending customer response', | ||
'committed' => 'committed', | ||
'canceled' => 'canceled', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
use Spatie\Enum\Laravel\Enum; | ||
|
||
/** | ||
* @method static self customer() | ||
* @method static self provider() | ||
* @method static self administrator() | ||
* @method static self disabled() | ||
*/ | ||
class UserRole extends Enum | ||
{ | ||
protected static function values(): array | ||
{ | ||
return [ | ||
'customer' => 'customer', | ||
'provider' => 'provider', | ||
'administrator' => 'administrator', | ||
'disabled' => 'disabled', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\Api\AuthRequest; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
class AuthController extends Controller | ||
{ | ||
public function auth(AuthRequest $request) | ||
{ | ||
|
||
$apiOrigin = env('UNILAB_API_ORIGIN') === null ? 'https://api.unilab.edu.br/api' : env('UNILAB_API_ORIGIN'); | ||
|
||
$dataAPi = ['login' => $request->login, 'senha' => $request->password]; | ||
$response = Http::post($apiOrigin.'/authenticate', $dataAPi); | ||
$responseJ = json_decode($response->body()); | ||
$userId = 0; | ||
if (isset($responseJ->id)) { | ||
$userId = intval($responseJ->id); | ||
} | ||
if ($userId === 0) { | ||
throw ValidationException::withMessages([ | ||
'login' => trans('auth.failed'), | ||
]); | ||
} | ||
$headers = [ | ||
'Authorization' => 'Bearer '.$responseJ->access_token, | ||
]; | ||
$response = Http::withHeaders($headers)->get($apiOrigin.'/user', $headers); | ||
$responseJ2 = json_decode($response->body()); | ||
|
||
$response = Http::withHeaders($headers)->get($apiOrigin.'/bond', $headers); | ||
$responseJ3 = json_decode($response->body()); | ||
|
||
$user = User::firstOrNew(['id' => $userId]); | ||
$user->id = $userId; | ||
$user->name = $responseJ2->nome; | ||
$user->email = $responseJ2->email; | ||
$user->login = $responseJ2->login; | ||
$user->division_sig = $responseJ3[0]->sigla_unidade; | ||
$user->division_sig_id = $responseJ3[0]->id_unidade; | ||
if ($user->role == null) { | ||
$user->role = $responseJ2->id_status_servidor != 1 ? 'disabled' : 'customer'; | ||
} | ||
$user->password = $request->password; | ||
$user->save(); | ||
$token = $user->createToken($request->device_name)->plainTextToken; | ||
|
||
return response()->json( | ||
['token' => $token, 'user' => $user] | ||
); | ||
} | ||
|
||
public function logout(Request $request) | ||
{ | ||
auth()->user()->tokens->each(function ($token, $key) { | ||
$token->delete(); | ||
}); | ||
|
||
return response()->json([ | ||
'message' => 'Logged out successfully!', | ||
'status_code' => 200, | ||
], 200); | ||
} | ||
|
||
public function me(Request $request) | ||
{ | ||
$user = $request->user(); | ||
|
||
return response()->json( | ||
['me' => $user] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use EloquentFilter\Filterable; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
abstract class BasicCrudController extends Controller | ||
{ | ||
protected $defaultPerPage = 15; | ||
|
||
abstract protected function model(); | ||
|
||
abstract protected function rulesStore(); | ||
|
||
abstract protected function rulesUpdate(); | ||
|
||
abstract protected function resource(); | ||
|
||
abstract protected function resourceCollection(); | ||
|
||
public function index(Request $request) | ||
{ | ||
$perPage = (int) $request->get('per_page', $this->defaultPerPage); | ||
$hasFilter = in_array(Filterable::class, class_uses($this->model())); | ||
|
||
$query = $this->queryBuilder(); | ||
|
||
if ($hasFilter) { | ||
$query = $query->filter($request->all()); | ||
} | ||
|
||
$data = $request->has('all') || ! $this->defaultPerPage | ||
? $query->get() | ||
: $query->paginate($perPage); | ||
|
||
$resourceCollectionClass = $this->resourceCollection(); | ||
$refClass = new \ReflectionClass($this->resourceCollection()); | ||
|
||
return $refClass->isSubclassOf(ResourceCollection::class) | ||
? new $resourceCollectionClass($data) | ||
: $resourceCollectionClass::collection($data); | ||
} | ||
|
||
public function store(Request $request) | ||
{ | ||
$validatedData = $this->validate($request, $this->rulesStore()); | ||
$obj = $this->queryBuilder()->create($validatedData); | ||
$obj->refresh(); | ||
$resource = $this->resource(); | ||
|
||
return new $resource($obj); | ||
} | ||
|
||
protected function findOrFail($id) | ||
{ | ||
$model = $this->model(); | ||
$keyName = (new $model)->getRouteKeyName(); | ||
|
||
return $this->queryBuilder()->where($keyName, $id)->firstOrFail(); | ||
|
||
} | ||
|
||
protected function rulesPatch() | ||
{ | ||
return array_map(function ($rules) { | ||
if (is_array($rules)) { | ||
$exists = in_array('required', $rules); | ||
if ($exists) { | ||
array_unshift($rules, 'sometimes'); | ||
} | ||
} else { | ||
return str_replace('required', 'sometimes|required', $rules); | ||
} | ||
|
||
return $rules; | ||
}, $this->rulesUpdate()); | ||
} | ||
|
||
public function destroyCollection(Request $request) | ||
{ | ||
$data = $this->validateIds($request); | ||
$this->model()::whereIn('id', $data['ids'])->delete(); | ||
|
||
return response()->noContent(); | ||
} | ||
|
||
protected function validateIds(Request $request) | ||
{ | ||
$model = $this->model(); | ||
$ids = explode(',', $request->get('ids')); | ||
$validator = Validator::make( | ||
[ | ||
'ids' => $ids, | ||
], | ||
[ | ||
'ids' => 'required|exists:'.(new $model)->getTable().',id', | ||
] | ||
); | ||
|
||
return $validator->validate(); | ||
} | ||
|
||
protected function queryBuilder(): Builder | ||
{ | ||
return $this->model()::query(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Resources\DivisionResource; | ||
use App\Models\Division; | ||
use Illuminate\Http\Request; | ||
|
||
class DivisionsController extends BasicCrudController | ||
{ | ||
public function __construct() | ||
{ | ||
$this->authorizeResource(Division::class, 'division'); | ||
} | ||
|
||
public function show(Division $division) | ||
{ | ||
return new DivisionResource($division); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param int $id | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request, Division $division) | ||
{ | ||
$this->validate($request, [ | ||
'name' => 'required|max:255', | ||
'description' => 'required|max:255', | ||
'email' => 'required|max:255', | ||
]); | ||
|
||
$division->update($request->all()); | ||
|
||
return response()->json($division, 200); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param int $id | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy(Division $division) | ||
{ | ||
$division->delete(); | ||
|
||
return response()->json(null, 204); | ||
} | ||
|
||
private $rules = [ | ||
'name' => 'required|max:255', | ||
]; | ||
|
||
protected function model() | ||
{ | ||
return Division::class; | ||
} | ||
|
||
protected function rulesStore() | ||
{ | ||
return $this->rules; | ||
} | ||
|
||
protected function rulesUpdate() | ||
{ | ||
return $this->rules; | ||
} | ||
|
||
protected function resourceCollection() | ||
{ | ||
return $this->resource(); | ||
} | ||
|
||
protected function resource() | ||
{ | ||
return DivisionResource::class; | ||
} | ||
} |
Oops, something went wrong.