Skip to content

Commit

Permalink
Criaçao de Fretes
Browse files Browse the repository at this point in the history
  • Loading branch information
Thasso Araújo committed Jun 16, 2020
1 parent 20731c6 commit c793c34
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 29 deletions.
33 changes: 33 additions & 0 deletions app/Frete.php
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"
];
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function login(AuthRequest $request)
$credentials = $request->only(['email', 'password']);

if (! $token = auth('api')->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
return response()->json(['error' => 'Dados de Acesso inválidos'], 401);
}

$user = User::where('email', $credentials['email'])
Expand Down
186 changes: 186 additions & 0 deletions app/Http/Controllers/Api/FreteController.php
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);
}
}
}
31 changes: 3 additions & 28 deletions app/Http/Enumerations/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,7 @@ abstract class Enum
public static function values()
{
$reflect = new ReflectionClass(get_called_class());
return collect($reflect->getConstants()['LABEL']);
}

/**
* Return string representation of this enum
*
* @return string
*/
public function getValue()
{
return $this->value;
}

/**
* Tries to set the value of this enum
*
* @param string $value
* @throws \Exception
*/
public function setValue($value)
{
if ($this->isValidEnumValue($value)) {
$this->value = $value;
} else {
throw new \Exception("Invalid type specified!");
}
return collect($reflect->getConstants());
}

/**
Expand All @@ -50,9 +25,9 @@ public function setValue($value)
* @return bool
* @throws \ReflectionException
*/
public function isValidEnumValue($checkValue)
public static function isValidEnumValue($checkValue)
{
$reflector = new ReflectionClass(get_class($this));
$reflector = new ReflectionClass(get_called_class());
foreach ($reflector->getConstants() as $validValue) {
if ($validValue == $checkValue) {
return true;
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Enumerations/TipoVeiculo.php
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];
}
}
27 changes: 27 additions & 0 deletions app/Http/Requests/Api/AgendarFreteRequest.php
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 [
];
}
}
27 changes: 27 additions & 0 deletions app/Http/Requests/Api/CalcularFreteRequest.php
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 [
];
}
}
Loading

0 comments on commit c793c34

Please sign in to comment.