Mejoras en la api

This commit is contained in:
2021-06-16 03:40:12 -04:00
parent 10f464eced
commit 64c05456af
10 changed files with 160 additions and 221 deletions

View File

@@ -11,13 +11,14 @@ class CanalVenta extends Model {
protected $table = 'canales_venta';
public static function findOrNull($id) {
try {
return CanalVenta::find($id);
} catch (QueryException $ex) {
Log::warning('Se intento obtener un canal de venta con un id invalido', ['id' => $id]);
return null;
public static function findOrFail($id) {
$canal_venta = CanalVenta::find($id);
if(!$canal_venta){
throw new ModelNotFoundException("canal_venta", $id);
}
return $canal_venta;
}
public function tipoCanal() {

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use App\Models\CanalVenta;
use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\QueryException;
@@ -21,13 +22,14 @@ class Restaurante extends Model {
protected $fillable = ['id', 'nombre'];
public static function findOrNull($id) {
try {
return Restaurante::find($id);
} catch (QueryException $ex) {
Log::warning('Se intento obtener un restaurante con un id invalido', ['id' => $id]);
return null;
public static function findOrFail($id) {
$restaurante = Restaurante::find($id);
if(!$restaurante){
throw new ModelNotFoundException("restaurant", $id);
}
return $restaurante;
}
public function usuarios() {

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use App\Services\Auth0Service;
use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\QueryException;
@@ -38,17 +39,18 @@ class Usuario extends Model {
* @param $id
* @return Usuario
*/
public static function findByIdOrAuth0Id($id) {
public static function findOrFail($id) {
if (str_starts_with($id, 'auth0')) {
return Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first();
$usuario = Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first();
} else {
try {
return Usuario::where('id', $id)->with('restaurantes')->first();
} catch (QueryException $ex) {
Log::warning('Se intento obtener un usuario con un id invalido', ['id' => $id]);
return null;
}
$usuario = Usuario::where('id', $id)->with('restaurantes')->first();
}
if(!$usuario) {
throw new ModelNotFoundException("usuario", $id);
}
return $usuario;
}
public function canManageRestaurants() {