-
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
Thasso Araújo
committed
Jun 16, 2020
1 parent
20731c6
commit c793c34
Showing
9 changed files
with
357 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Contracts\Auth\MustVerifyEmail; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Foundation\Auth\User as Authenticatable; | ||
use Illuminate\Notifications\Notifiable; | ||
use Tymon\JWTAuth\Contracts\JWTSubject; | ||
|
||
class Frete extends Model | ||
{ | ||
use Notifiable; | ||
|
||
protected $table = "tb_fretes"; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
"origem_latitude", | ||
"origem_longitude", | ||
"destino_latitude", | ||
"destino_longitude", | ||
"data_agendamento", | ||
"tipo_veiculo", | ||
"data_frete", | ||
"valor", | ||
"usuario_id" | ||
]; | ||
} |
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,186 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Frete; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Enumerations\TipoVeiculo; | ||
use App\Http\Helpers\DataHelper; | ||
use App\Http\Requests\Api\AgendarFreteRequest; | ||
use App\Http\Requests\Api\CalcularFreteRequest; | ||
use Exception; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class FreteController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index() | ||
{ | ||
$fretes = Frete::all(); | ||
|
||
return response()->json($fretes); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
* | ||
* @param int $id | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function show($id) | ||
{ | ||
try { | ||
$frete = Frete::find($id); | ||
|
||
if (!isset($frete)) { | ||
return response()->json(["error" => "Data not found"], 400); | ||
} | ||
|
||
return response()->json($frete); | ||
} catch (Exception $exception) { | ||
Log::info($exception->getMessage() . $exception->getTraceAsString()); | ||
return response()->json($exception->getMessage(), 400); | ||
} | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param int $id | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request) | ||
{ | ||
try { | ||
$id = $request->get('id'); | ||
|
||
$data = $request->only([ | ||
"origem_latitude", | ||
"origem_longitude", | ||
"destino_latitude", | ||
"destino_longitude", | ||
"data_agendamento", | ||
"tipo_veiculo", | ||
"data_frete" | ||
]); | ||
|
||
$data["data_frete"] = DataHelper::stringToCarbonDate($data["data_frete"]); | ||
|
||
if (!TipoVeiculo::isValidEnumValue($data["tipo_veiculo"])) { | ||
return response()->json(["error" => "Campo 'tipo_veiculo' inválido - | ||
(1 - Moto, 2 - Pick Up, 3 - Caminhão)"], 400); | ||
} | ||
|
||
$data["valor"] = rand(0, 100); | ||
|
||
$frete = Frete::find($id); | ||
if (!isset($frete)) { | ||
return response()->json(["error" => "Data not found"], 400); | ||
} | ||
|
||
$frete->update($data); | ||
|
||
return response()->json($frete); | ||
} catch (Exception $exception) { | ||
Log::info($exception->getMessage() . $exception->getTraceAsString()); | ||
return response()->json($exception->getMessage(), 400); | ||
} | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param int $id | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy($id) | ||
{ | ||
try { | ||
$frete = Frete::find($id); | ||
if (!isset($frete)) { | ||
return response()->json(["error" => "Data not found"], 400); | ||
} | ||
$frete->delete(); | ||
|
||
return response()->json([ "message" => "Data was deleted!" ]); | ||
} catch (Exception $exception) { | ||
Log::info($exception->getMessage() . $exception->getTraceAsString()); | ||
return response()->json($exception->getMessage(), 400); | ||
} | ||
} | ||
|
||
/** | ||
* Calcular valor de frete. | ||
* | ||
* @param \Illuminate\Http\Api\CalcularFreteRequest $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function calcular(CalcularFreteRequest $request) | ||
{ | ||
try { | ||
$data = $request->only([ | ||
"origem_latitude", | ||
"origem_longitude", | ||
"destino_latitude", | ||
"destino_longitude", | ||
"tipo_veiculo" | ||
]); | ||
|
||
if (!TipoVeiculo::isValidEnumValue($data["tipo_veiculo"])) { | ||
return response()->json(["error" => "Campo 'tipo_veiculo' inválido - | ||
(1 - Moto, 2 - Pick Up, 3 - Caminhão)"], 400); | ||
} | ||
|
||
return response()->json(["valor" => rand(0, 100)]); | ||
} catch (Exception $exception) { | ||
Log::info($exception->getMessage() . $exception->getTraceAsString()); | ||
return response()->json($exception->getMessage(), 400); | ||
} | ||
} | ||
|
||
/** | ||
* Agendamento de fretes | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function agendar(AgendarFreteRequest $request) | ||
{ | ||
try { | ||
$user = auth('api')->user(); | ||
|
||
$data = $request->only([ | ||
"origem_latitude", | ||
"origem_longitude", | ||
"destino_latitude", | ||
"destino_longitude", | ||
"data_agendamento", | ||
"tipo_veiculo", | ||
"data_frete" | ||
]); | ||
|
||
$data["usuario_id"] = $user->id; | ||
$data["data_frete"] = DataHelper::stringToCarbonDate($data["data_frete"]); | ||
|
||
if (!TipoVeiculo::isValidEnumValue($data["tipo_veiculo"])) { | ||
return response()->json(["error" => "Campo 'tipo_veiculo' inválido - | ||
(1 - Moto, 2 - Pick Up, 3 - Caminhão)"], 400); | ||
} | ||
|
||
$data["valor"] = rand(0, 100); | ||
|
||
$user = Frete::create($data); | ||
|
||
return response()->json($user); | ||
} catch (Exception $exception) { | ||
Log::info($exception->getMessage() . $exception->getTraceAsString()); | ||
return response()->json($exception->getMessage(), 400); | ||
} | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace App\Http\Enumerations; | ||
|
||
abstract class TipoVeiculo extends Enum | ||
{ | ||
const MOTO = 1; | ||
const PICK_UP = 2; | ||
const CAMINHAO = 3; | ||
|
||
private const LABELS = [ | ||
self::MOTO => "Moto", | ||
self::PICK_UP => "Pick Up", | ||
self::CAMINHAO => "Caminhão", | ||
]; | ||
|
||
/** | ||
* Get Label | ||
* | ||
* @param int $value | ||
* @return void | ||
*/ | ||
public static function getLabel($value) | ||
{ | ||
return self::LABELS[$value]; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
class AgendarFreteRequest extends ApiFormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
]; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
class CalcularFreteRequest extends ApiFormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
]; | ||
} | ||
} |
Oops, something went wrong.