diff --git a/app/Actions/UserAction.php b/app/Actions/UserAction.php deleted file mode 100644 index 3851bf2..0000000 --- a/app/Actions/UserAction.php +++ /dev/null @@ -1,38 +0,0 @@ -create($params); - } - - public function show($id): User - { - return User::query()->find($id); - } - - public function update(array $params, $id): int - { - return User::query()->find($id)->update($params); - } - - public function delete($id) - { - // TODO: Implement delete() method. - } -} diff --git a/app/Helper/Helpers.php b/app/Helper/Helpers.php new file mode 100644 index 0000000..262fa2d --- /dev/null +++ b/app/Helper/Helpers.php @@ -0,0 +1,28 @@ + $value) { + foreach ($value as $v_key => $v_value) { + if ($v_key === $iten) { + $keized[$v_value] = $array[$key]; + } + } + } + return $keized; + } +} diff --git a/app/Helper/Model/EntityJson.php b/app/Helper/Model/EntityJson.php new file mode 100644 index 0000000..bc50d33 --- /dev/null +++ b/app/Helper/Model/EntityJson.php @@ -0,0 +1,135 @@ + $value) { + $this->$key = $value; + } + } + + public function jsonSerialize() + { + $vars = get_object_vars($this); + $obj = new \stdClass(); + foreach ($vars as $key => $value) { + $obj->$key = $value; + } + return $obj; + } + + public function jsonSerializeUpperKey() + { + $vars = get_object_vars($this); + $obj = new \stdClass(); + foreach ($vars as $key => $value) { + $key = strtoupper($key); + $obj->$key = $value; + } + return $obj; + } + + public function jsonSerializeLowerKey() + { + $vars = get_object_vars($this); + $obj = new \stdClass(); + foreach ($vars as $key => $value) { + $key = strtolower($key); + $obj->$key = $value; + } + return $obj; + } + + public function jsonSetObject(\stdClass $jsonData) + { + $vars = get_object_vars($jsonData); + foreach ($vars as $key => $value) { + $method = "set" . ucfirst($key); + if (method_exists($this, $method)) { + $this->$method($value); + } else { + if (property_exists(get_class($this), $key)) { + $this->$key = $value; + } + } + } + } + + public function fill(\stdClass $jsonData) + { + $vars = get_object_vars($jsonData); + foreach ($vars as $key => $value) { + $campo = strtolower($key); + $method = "set" . ucfirst($campo); + if (method_exists($this, $method)) { + $this->$method($value); + } else { + if (property_exists(get_class($this), $campo)) { + $this->$campo = $value; + } + } + } + } + + public function toArray() + { + $vars = get_object_vars($this); + $result = array(); + foreach ($vars as $key => $value) { + $method = "get" . ucfirst($key); + $key = strtoupper($key); + $valor = $value; + if (method_exists($this, $method)) { + $valor = $this->$method(); + } + $result[$key] = $valor; + } + return $result; + } + + /** + * Converte um array de objeto stdClass para uma entidade + * @param $className + * @param array $arrayOfObject + * @return array + */ + protected function arrayObjectCast($className, array $arrayOfObject) : array + { + $arr = []; + if (!empty($arrayOfObject)) { + // verificando o elemento do array + $obj = $arrayOfObject[0]; + // se for um array associativo converte para stdClass + if (is_array($obj) && count(array_filter(array_keys($obj), 'is_string')) > 0) { + $obj = (object) $obj; + } + if (get_class($obj) == "stdClass") { + foreach ($arrayOfObject as $item) { + $e = new $className(); + if (method_exists($e, 'jsonSetObject')) { + $e->jsonSetObject($item); + $arr[] = $e; + } + } + } elseif (get_class($obj) == $className) { + return $arrayOfObject; + } + } + return $arr; + } +} diff --git a/app/Helper/Model/ModelExtractor.php b/app/Helper/Model/ModelExtractor.php new file mode 100644 index 0000000..1684529 --- /dev/null +++ b/app/Helper/Model/ModelExtractor.php @@ -0,0 +1,97 @@ +modelClass = $modelClass; + $this->instanceModel(); + } + + public function getAttributeFromMethod($method, array $parameters = []) + { + if (method_exists($this->model, $method)) { + if (count($parameters) > 0) { + $result = [$this->model, $method](...$parameters); + } else { + $result = [$this->model, $method](); + } + if (isset($result)) { + if ($result instanceof $this->modelClass) { + $this->setAttributeFromDataArray($result->toArray(), $this->model->getCasts()); + return $this->getAttributes(); + } elseif ($result instanceof Collection) { + $this->setAttributeFromCollection($result); + return $this->getAttributes(); + } elseif (is_array($result)) { + $this->setAttributeFromDataArray((array) $result[0], $this->model->getCasts()); + return $this->getAttributes(); + } elseif (is_object($result)) { + $this->setAttributeFromDataArray((array) $result, $this->model->getCasts()); + return $this->getAttributes(); + } else { + throw new \Exception('Nao foi possivel converter esse tipo de retorno verificar '); + } + } + } else { + throw new \Exception('Method not found for this class'); + } + } + + private function setAttributeFromCollection(Collection $collection) + { + $casts = $this->model->getCasts(); + $this->setAttributeFromDataArray($collection->toArray()[0], $casts); + } + + private function setAttributeFromDataArray(array $data, array $casts = []) + { + $attributes = []; + foreach ($data as $key => $value) { + if (!empty($casts) && isset($casts[$key])) { + $attributes[] = $this->createColumn($key, $casts[$key]); + } else { + $attributes[] = $this->createColumn($key, $this->getDataTypeByValue($value)); + } + } + + $this->attributes = $attributes; + } + + private function instanceModel() + { + $this->model = new $this->modelClass; + $this->setTable($this->model->getTable()); + $this->setOwner($this->model->getConnectionName()); + } +} diff --git a/app/Helper/Model/TableExtractor.php b/app/Helper/Model/TableExtractor.php new file mode 100644 index 0000000..499301d --- /dev/null +++ b/app/Helper/Model/TableExtractor.php @@ -0,0 +1,201 @@ +setTable($tableName); + $this->setOwner($owner); + } + + public function getAttributes() : array + { + if (count($this->attributes) === 0) { + $result = DB::select(DB::raw($this->_sql_filterExtractor), array( + $this->tableName, + $this->owner, + )); + foreach ($result as $r) { + $r->column_name = strtolower($r->column_name); + $r->data_type = strtolower($r->data_type); + } + $this->attributes = $result; + } + return $this->attributes; + } + + public function toPhpProperties() : array + { + $attributes = $this->getAttributes(); + return array_map(function ($r) { + return + "{$this->e}/**". PHP_EOL . + "{$this->e}* @var ".$this->getDataTypePhp($r->data_type). PHP_EOL. + "{$this->e}*/" . PHP_EOL . + "{$this->e}protected $" . strtolower($r->column_name).';' . PHP_EOL; + }, $attributes); + } + + public function toSetters() : array + { + $attributes = $this->getAttributes(); + + return array_map(function ($r) { + return + "{$this->e}public function set" . ucfirst($r->column_name) . "(" . $this->getDataTypePhp($r->data_type) . " $".$r->column_name.")" . PHP_EOL . + "{$this->e}{" . PHP_EOL . + "{$this->e}{$this->e}\$this->".strtolower($r->column_name). " = $" . $r->column_name . ";" . PHP_EOL . + "{$this->e}}" . PHP_EOL; + }, $attributes); + } + + public function toGetters() : array + { + $attributes = $this->getAttributes(); + + return array_map(function ($r) { + return + "{$this->e}public function get" . ucfirst($r->column_name) . "()" . $this->getDataTypePhp($r->data_type, true) . PHP_EOL . + "{$this->e}{" . PHP_EOL . + "{$this->e}{$this->e}return \$this->".strtolower($r->column_name). ";" . PHP_EOL . + "{$this->e}}" . PHP_EOL; + }, $attributes); + } + + public function toSwaggerAnnotation($entityName = 'Entity') + { + $swagger = ""; + $attributes = $this->getAttributes(); + $swagger.='/**' . PHP_EOL; + $swagger.=' * @OA\Schema(' . PHP_EOL; + $swagger.=' * schema="'.$entityName.'",' . PHP_EOL; + $swagger.=' * type="object",' . PHP_EOL; + $swagger.=' * description="'.$entityName.' entity",' . PHP_EOL; + $swagger.=' * title="'.$entityName.' entity",' . PHP_EOL; + foreach ($attributes as $at) { + $type = 'type="'.$at->data_type.'"'; + if ($at->data_type === 'float') { + $type = 'type="number", format="float", example="15.99"'; + } + if ($at->data_type === 'date') { + $type = 'type="string", format="date", example="dd/mm/YYYY"'; + } + $swagger.=' * @OA\Property(property="'.$at->column_name.'", '.$type.', description="'.ucfirst($at->column_name).'"),' . PHP_EOL; + } + $swagger.=" * )" . PHP_EOL; + $swagger.=" */"; + return $swagger; + } + + public function getDataTypePhp($dataType, $return = false) + { + $valor = ""; + switch ($dataType): + case 'string': + $valor = 'string'; + break; + case 'float': + $valor = 'float'; + break; + case 'integer': + $valor = 'int'; + break; + case 'array': + $valor = 'array'; + break; + default: + $valor = ''; + break; + endswitch; + + if ($return === true && !empty($valor)) { + $valor = ' : ' . $valor; + } + return $valor; + } + + protected function setTable($table) + { + $this->tableName = strtoupper($table); + } + + protected function setOwner($owner) + { + $this->owner = strtoupper($owner); + } + + protected function createColumn($columName, $dataType) : \stdClass + { + $obj = new \stdClass(); + $obj->column_name = strtolower($columName); + $obj->data_type = strtolower($dataType); + return $obj; + } + + protected function getDataTypeByValue($value) + { + // verificando se é numerico + if (is_object($value)===false && is_numeric(str_replace(',', '.', $value))) { + // verificando se é float + if (strpos($value, ",")!==false || strpos($value, ".")!==false) { + return "float"; + } else { + return "integer"; + } + } elseif (is_string($value)) { + return "string"; + } elseif (is_array($value)) { + return "array"; + } else { + return ""; + } + } +} diff --git a/app/Helper/Model/filter/FieldFilterBuilder.php b/app/Helper/Model/filter/FieldFilterBuilder.php new file mode 100644 index 0000000..0089e16 --- /dev/null +++ b/app/Helper/Model/filter/FieldFilterBuilder.php @@ -0,0 +1,26 @@ +fields; + } + + public function setFields(array $fields) + { + $this->fields = $fields; + } +} diff --git a/app/Helper/Model/filter/FilterBuilder.php b/app/Helper/Model/filter/FilterBuilder.php new file mode 100644 index 0000000..c20563f --- /dev/null +++ b/app/Helper/Model/filter/FilterBuilder.php @@ -0,0 +1,159 @@ +getFilters()) === false) { + foreach ($this->getFilters() as $filter) { + $builder = FilterType::filter($builder, $filter); + } + } + // estilo antigo + //$builder->limit($this->getLimit()); + //$builder->offset($this->getOffset()); + + /** + * Estilo novo com paginate + * Vantagem q o paginate já retorna o total + */ + $currentPage = $this->getOffset(); + \Illuminate\Pagination\Paginator::currentPageResolver(function () use ($currentPage) { + return $currentPage; + }); + foreach ($this->getOrder() as $order) { + $builder->orderBy($order->getField(), $order->getDirection()); + } + return $builder; + } + + public function getOrder(): array + { + return $this->order; + } + + public function setOrder(array $orders) + { + $this->order = $this->arrayObjectCast(OrderItem::class, $orders); + } + + public function getFilters(): array + { + return $this->filters; + } + + public function getLimit(): int + { + return $this->limit; + } + + public function getOffset(): int + { + return $this->offset; + } + + public function setFilters(array $filters) + { + $this->filters = $this->arrayObjectCast(FilterItem::class, $filters); + } + + public function addFilter(FilterItem $filter) + { + $this->filters[] = $filter; + } + + public function setLimit(int $limit) + { + $this->limit = $limit; + } + + public function setOffset(int $offset) + { + $this->offset = $offset; + } + + public function getFields() : array + { + return $this->fields; + } + + public function setFields(array $fields) + { + $this->fields = $fields; + } +} diff --git a/app/Helper/Model/filter/FilterBuilderFactory.php b/app/Helper/Model/filter/FilterBuilderFactory.php new file mode 100644 index 0000000..8ce7e3f --- /dev/null +++ b/app/Helper/Model/filter/FilterBuilderFactory.php @@ -0,0 +1,15 @@ +jsonSetObject($json); + $response->setData($data); + return $response; + } +} diff --git a/app/Helper/Model/filter/FilterBuilderResponse.php b/app/Helper/Model/filter/FilterBuilderResponse.php new file mode 100644 index 0000000..3d1e342 --- /dev/null +++ b/app/Helper/Model/filter/FilterBuilderResponse.php @@ -0,0 +1,42 @@ +data; + } + + public function setData(array $data) + { + $this->data = $data; + } + + public function getTotal() : int + { + return $this->total; + } + + public function setTotal(int $total) + { + $this->total = $total; + } +} diff --git a/app/Helper/Model/filter/FilterItem.php b/app/Helper/Model/filter/FilterItem.php new file mode 100644 index 0000000..e4c4a4d --- /dev/null +++ b/app/Helper/Model/filter/FilterItem.php @@ -0,0 +1,111 @@ +=", description="Tipo do filtro podendo ser: =,<>,>,>=,<,<=,between,not_between,like,not_like,in,not_in"), + * @OA\Property(property="field", type="string", description="Campo a ser ordernado (tem ser um campo valido)"), + * @OA\Property(property="value", type="string", example="30", description="Valor a ser filtrado. No between e in usar array nos outros casos usar um valor normal (sem ser numerico)."), + * ) + * + * @OA\Schema( + * schema="FilterItemFilterList", + * allOf={ + * @OA\Schema(ref="#/components/schemas/FilterItemFilter") + * } + * ) + */ +class FilterItem extends EntityJson +{ + /** + * @var string + */ + protected $type = "="; + + /** + * @var string + */ + protected $field; + + /** + * + * @var mixed + */ + protected $value; + + /** + * @var bool + */ + protected bool $row = false; + + /** + * @return bool + */ + public function getRow(): bool + { + return $this->row; + } + + /** + * @param bool $row + */ + public function setRow(bool $row): void + { + $this->row = $row; + } + + + public function getType() : string + { + return $this->type; + } + + public function getField() :Expression|string + { + return $this->field; + } + + public function getValue() + { + return $this->value; + } + + public function setType(string $type) + { + $this->type = $type; + } + + public function setField($field) + { + $this->field = $field; + } + + public function setValue($value) + { + $this->value = $value; + } +} diff --git a/app/Helper/Model/filter/FilterType.php b/app/Helper/Model/filter/FilterType.php new file mode 100644 index 0000000..3df5669 --- /dev/null +++ b/app/Helper/Model/filter/FilterType.php @@ -0,0 +1,120 @@ +", + ">", + ">=", + "<", + "<=", + "like", + "not_like" + ); + + const IN_FILTER = array( + "in", + "not_in" + ); + + const BETWEEN_FILTER = array( + "between", + "not_between" + ); + + const NULL_FILTER = array( + "null", + "not_null" + ); + + const DATE_FILTER = [ + + ]; + + public static function filter(Builder $builder, FilterItem $filter) : Builder + { + if (in_array($filter->getType(), self::WHERE_FILTER)) { + return self::MAKE_WHERE_FILTER($builder, $filter); + } + if (in_array($filter->getType(), self::IN_FILTER)) { + return self::MAKE_IN_FILTER($builder, $filter); + } + + if (in_array($filter->getType(), self::BETWEEN_FILTER)) { + return self::MAKE_BETWEEN_FILTER($builder, $filter); + } + + if (in_array($filter->getType(), self::NULL_FILTER)) { + return self::MAKE_NULL_FILTER($builder, $filter); + } + + if (in_array($filter->getType(), self::DATE_FILTER)){ + + } + + return $builder; + } + + private static function MAKE_WHERE_FILTER(Builder $builder, FilterItem $filter) : Builder + { + $fType = $filter->getType(); + // filtro not_like sem o underscore (deixo o _ por padrao) + if ($fType === 'not_like') { + $fType = 'not like'; + } + + $field = ($filter->getRow()) ? DB::raw($filter->getField()) : $filter->getField(); + + return $builder->where($field, $fType, $filter->getValue()); + } + + private static function MAKE_IN_FILTER(Builder $builder, FilterItem $filter) : Builder + { + if ($filter->getType() === "in") { + return $builder->whereIn($filter->getField(), $filter->getValue()); + } elseif ($filter->getType() === "not_in") { + return $builder->whereNotIn($filter->getField(), $filter->getValue()); + } + + return $builder; + } + + private static function MAKE_BETWEEN_FILTER(Builder $builder, FilterItem $filter) : Builder + { + if ($filter->getType() === "between") { + return $builder->whereBetween($filter->getField(), $filter->getValue()); + } elseif ($filter->getType() === "not_between") { + return $builder->whereNotBetween($filter->getField(), $filter->getValue()); + } + + return $builder; + } + + private static function MAKE_NULL_FILTER(Builder $builder, FilterItem $filter) : Builder + { + if ($filter->getType() === "null") { + return $builder->whereNull($filter->getField()); + } elseif ($filter->getType() === "not_null") { + return $builder->whereNotNull($filter->getField()); + } + + return $builder; + } +} diff --git a/app/Helper/Model/filter/IFilterBuilder.php b/app/Helper/Model/filter/IFilterBuilder.php new file mode 100644 index 0000000..9179579 --- /dev/null +++ b/app/Helper/Model/filter/IFilterBuilder.php @@ -0,0 +1,15 @@ +field; + } + + public function setField(string $field) + { + $this->field = $field; + } + + public function getDirection() : string + { + return $this->direction; + } + + public function setDirection(string $direction="asc") + { + $this->direction = $direction; + } +} diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php index e2f9405..2a193c5 100644 --- a/app/Http/Controllers/Auth/AuthController.php +++ b/app/Http/Controllers/Auth/AuthController.php @@ -10,7 +10,7 @@ use Laravel\Sanctum\PersonalAccessToken; class AuthController extends Controller { - public function login(LoginResquest $request) + public function login(LoginResquest $request): JsonResponse { $credentials = $request->only('email', 'password'); @@ -29,21 +29,6 @@ class AuthController extends Controller } - public function login2(LoginResquest $request): JsonResponse - { - $credentials = $request->only('email', 'password'); - - if (!auth()->attempt($credentials)) - { - abort(401, 'Inavalid Credentials'); - } - - $request->session()->regenerate(); - - return response()->json([], 200); - - } - public function logout(Request $request): JsonResponse { $requestToken = $request->header('authorization'); diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php deleted file mode 100644 index 82bd411..0000000 --- a/app/Http/Controllers/Auth/RegisterController.php +++ /dev/null @@ -1,14 +0,0 @@ -limit(10)->get(); - - return response()->json($result, 200); + public function __invoke(Request $request) + { + try { + $this->economyContract->execute($request); + } catch (\Exception $exception) { + return \response()->json([], ResponseAlias::HTTP_INTERNAL_SERVER_ERROR); + } } +// public function index(): JsonResponse +// { +// +// Economy::query()->select(); +// abort_if(Gate::denies('teste-index'), ResponseAlias::HTTP_FORBIDDEN, '403 Forbidden'); +// +// $result = DadosCadastrais::query()->limit(10)->get(); +// +// return response()->json($result, 200); +// +// } +// +// +// public function teste(Request $request): JsonResponse +// { +// try { +// $data = (new EconomyRepository())->execute($request->all()); +// return \response()->json($data, ResponseAlias::HTTP_OK); +// } catch (\Exception $exception) { +// return \response()->json([], ResponseAlias::HTTP_INTERNAL_SERVER_ERROR); +// } +// +// } + } diff --git a/app/Http/Controllers/FaqController.php b/app/Http/Controllers/FaqController.php index 2748405..0e0c035 100644 --- a/app/Http/Controllers/FaqController.php +++ b/app/Http/Controllers/FaqController.php @@ -2,9 +2,67 @@ namespace App\Http\Controllers; +use App\Repositories\Faqs\FaqContractInterface; +use App\Traits\ApiResponse; +use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class FaqController extends Controller { - // -} + use ApiResponse; + + public function __construct( + protected FaqContractInterface $faq + ){} + + public function index(): JsonResponse + { + try { + $response = $this->faq->all(); + return response()->json($response, 200); + } catch (\Exception $ex) { + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + + public function store(Request $notificationRequest): JsonResponse + { + try { + $response = $this->faq->create($notificationRequest->all()); + return response()->json($response, 200); + } catch (\Exception $ex) { + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + + public function show(int $id): JsonResponse + { + try { + $response = $this->faq->find($id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + + public function update(Request $request, $id): JsonResponse + { + try { + $response = $this->faq->update($request->all(), $id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + + public function destroy($id): JsonResponse + { + try { + $response = $this->faq->destroy($id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php deleted file mode 100644 index f1ccaf5..0000000 --- a/app/Http/Controllers/NewsController.php +++ /dev/null @@ -1,8 +0,0 @@ -notification->all(); + return response()->json($response, 200); + } catch (\Exception $ex) { + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + + public function store(StoreNotificationRequest $request): JsonResponse + { + try { + $response = $this->notification->create($request->validated()); + $response->users()->sync($request->input('users.*.user_id', [])); + + return (new NotificationResource($response)) + ->response() + ->setStatusCode(Response::HTTP_CREATED); + } catch (\Exception $ex) { + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + + public function show(int $id): JsonResponse + { + try { + $response = $this->notification->find($id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + + public function update(Request $request, $id): JsonResponse + { + try { + $response = $this->notification->update($request->all(), $id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } + + public function destroy($id): JsonResponse + { + try { + $response = $this->notification->destroy($id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } + } +} \ No newline at end of file diff --git a/app/Http/Controllers/PldController.php b/app/Http/Controllers/PldController.php new file mode 100644 index 0000000..1064029 --- /dev/null +++ b/app/Http/Controllers/PldController.php @@ -0,0 +1,30 @@ +list(); + $list = $this->user->withRelationsByAll('roles'); return response()->json($list, 200); }catch (\Exception $ex){ - return response()->json(['message' => $ex->getMessage()], 500); + return $this->errorResponse(false, $ex->getMessage(), 404); } } @@ -27,45 +39,68 @@ class UserController extends Controller /** * Store a newly created resource in storage. * - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response + * @param StoreUserRequest $request + * @return JsonResponse */ - public function store(Request $request) + public function store(StoreUserRequest $request): JsonResponse { - // + try { + $response = $this->user->create($request->all()); + + return (new UserResource($response)) + ->response() + ->setStatusCode(Response::HTTP_CREATED); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } } /** * Display the specified resource. * - * @param int $id - * @return \Illuminate\Http\Response + * @param int $id + * @return JsonResponse */ - public function show($id) + public function show(int $id): JsonResponse { - // + try { + $response = $this->user->find($id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } } /** * Update the specified resource in storage. * - * @param \Illuminate\Http\Request $request - * @param int $id - * @return \Illuminate\Http\Response + * @param Request $request + * @param $id + * @return JsonResponse */ - public function update(Request $request, $id) + public function update(Request $request, $id): JsonResponse { - // + try { + $response = $this->user->update($request->all(), $id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } } /** * Remove the specified resource from storage. * - * @param int $id - * @return \Illuminate\Http\Response + * @param $id + * @return JsonResponse */ - public function destroy($id) + public function destroy($id): JsonResponse { - // + try { + $response = $this->user->destroy($id); + return response()->json($response, 200); + }catch (\Exception $ex){ + return $this->errorResponse(false, $ex->getMessage(), 404); + } } } diff --git a/app/Http/Requests/AzuxRequest.php b/app/Http/Requests/AzuxRequest.php new file mode 100644 index 0000000..01e3c53 --- /dev/null +++ b/app/Http/Requests/AzuxRequest.php @@ -0,0 +1,46 @@ +getContent()); + if (isset($jsonData)) { + $obj = new $className; + if (method_exists($obj, 'jsonToObject')) { + $obj->jsonSetObject($jsonData); + return $obj; + } + } else { + throw new Exception("Request inválido"); + } + } + + public function getFilterBuilder() : ?FilterBuilder + { + return $this->getJsonObject(FilterBuilder::class); + } + + public function getFieldFilterBuilder() : ?FieldFilterBuilder + { + return $this->getJsonObject(FieldFilterBuilder::class); + } +} diff --git a/app/Http/Requests/RegisterUserResquest.php b/app/Http/Requests/StoreNotificationRequest.php similarity index 56% rename from app/Http/Requests/RegisterUserResquest.php rename to app/Http/Requests/StoreNotificationRequest.php index 2363cc6..f8a8c41 100644 --- a/app/Http/Requests/RegisterUserResquest.php +++ b/app/Http/Requests/StoreNotificationRequest.php @@ -4,16 +4,16 @@ namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; -class RegisterUserResquest extends FormRequest +class StoreNotificationRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ - public function authorize() + public function authorize(): bool { - return false; + return true; } /** @@ -24,7 +24,12 @@ class RegisterUserResquest extends FormRequest public function rules() { return [ - // + 'title' => 'required|string', + 'body' => 'required', + 'users.*.user_i' => [ + 'integer', + 'exists:users,user_id', + ], ]; } } diff --git a/app/Http/Requests/UserLoginRequest.php b/app/Http/Requests/StoreUserRequest.php similarity index 60% rename from app/Http/Requests/UserLoginRequest.php rename to app/Http/Requests/StoreUserRequest.php index 98d9e23..7353906 100644 --- a/app/Http/Requests/UserLoginRequest.php +++ b/app/Http/Requests/StoreUserRequest.php @@ -4,7 +4,7 @@ namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; -class UserLoginRequest extends FormRequest +class StoreUserRequest extends FormRequest { /** * Determine if the user is authorized to make this request. @@ -13,7 +13,7 @@ class UserLoginRequest extends FormRequest */ public function authorize() { - return false; + return true; } /** @@ -24,7 +24,10 @@ class UserLoginRequest extends FormRequest public function rules() { return [ - // + 'name' => 'required|string', + 'email' => 'required|string|email|max:255|unique:users', + 'password'=> 'required|string|min:6|confirmed', + 'client_id' => 'required' ]; } } diff --git a/app/Http/Resources/NotificationResource.php b/app/Http/Resources/NotificationResource.php new file mode 100644 index 0000000..cc0f324 --- /dev/null +++ b/app/Http/Resources/NotificationResource.php @@ -0,0 +1,19 @@ +format('d/m/Y H:i:s'); + } + + public function users(): BelongsToMany + { + return $this->belongsToMany(User::class, 'notificacoes_users', 'notification_id', 'user_id', ); + } + } diff --git a/app/Models/Role.php b/app/Models/Role.php index c9fa5da..b9b37c4 100644 --- a/app/Models/Role.php +++ b/app/Models/Role.php @@ -2,6 +2,7 @@ namespace App\Models; +use DateTimeInterface; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -28,6 +29,11 @@ class Role extends Model implements Auditing 'deleted_at', ]; + protected function serializeDate(DateTimeInterface $date): string + { + return $date->format('d/m/Y H:i:s'); + } + public function permissions(): BelongsToMany { return $this->belongsToMany(Permission::class); diff --git a/app/Models/User.php b/app/Models/User.php index 2950e0a..3b79c6f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use DateTimeInterface; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\SoftDeletes; @@ -15,11 +16,10 @@ class User extends Authenticatable implements Auditing { use HasApiTokens, HasFactory, Notifiable, SoftDeletes, Auditable; - /** - * The attributes that are mass assignable. - * - * @var array - */ + protected $table = "users"; + + protected $guarded = ['id']; + protected $fillable = [ 'name', 'email', @@ -29,28 +29,27 @@ class User extends Authenticatable implements Auditing 'deleted_at', ]; - /** - * The attributes that should be hidden for serialization. - * - * @var array - */ protected $hidden = [ 'password', 'remember_token', ]; - /** - * The attributes that should be cast. - * - * @var array - */ protected $casts = [ 'email_verified_at' => 'datetime', ]; + protected function serializeDate(DateTimeInterface $date): string + { + return $date->format('d/m/Y H:i:s'); + } + public function roles(): BelongsToMany { return $this->belongsToMany(Role::class); } + public function notifications(): BelongsToMany + { + return $this->belongsToMany(Notifications::class, 'notificacoes_users', 'notification_id', 'user_id'); + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5b..8757589 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,16 @@ namespace App\Providers; +use App\Repositories\Economy\EconomyContractInterface; +use App\Repositories\Economy\EconomyRepository; +use App\Repositories\Faqs\FaqContractInterface; +use App\Repositories\Faqs\FaqRepository; +use App\Repositories\Notifications\NotificationContractInterface; +use App\Repositories\Notifications\NotificationRepository; +use App\Repositories\Pld\PldContractInterface; +use App\Repositories\Pld\PldRepository; +use App\Repositories\Users\UserContractInterface; +use App\Repositories\Users\UserRepository; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -11,9 +21,28 @@ class AppServiceProvider extends ServiceProvider * * @return void */ - public function register() + public function register(): void { - // + $this->app->bind( + UserContractInterface::class, + UserRepository::class + ); + $this->app->bind( + NotificationContractInterface::class, + NotificationRepository::class + ); + $this->app->bind( + FaqContractInterface::class, + FaqRepository::class + ); + $this->app->bind( + PldContractInterface::class, + PldRepository::class + ); + $this->app->bind( + EconomyContractInterface::class, + EconomyRepository::class + ); } /** diff --git a/app/Repositories/AbstractRepository.php b/app/Repositories/AbstractRepository.php new file mode 100644 index 0000000..746af6e --- /dev/null +++ b/app/Repositories/AbstractRepository.php @@ -0,0 +1,59 @@ +model = $model ?? $this::resolveModel(); + } + + public function __call($name, $arguments) + { + $result = $this->forwardCallTo($this->model, $name, $arguments); + + if ($result === $this->model) { + return $this; + } + + return $result; + } + + public function __get($name) + { + return $this->model->{$name}; + } + + /** + * @throws BindingResolutionException + * @throws Exception + */ + final protected function resolveModel(): AbstractRepository + { + $model = app()->make($this->model); + + if (!$model instanceof Model) { + throw new Exception( + "Class {$this->model} must be an instance of Illuminate\\Database\\Eloquent\\Model" + ); + } + + return (new static)->$model; + } + +} diff --git a/app/Repositories/ContractInterface.php b/app/Repositories/ContractInterface.php new file mode 100644 index 0000000..f0ef004 --- /dev/null +++ b/app/Repositories/ContractInterface.php @@ -0,0 +1,13 @@ +model + ->select( + $this->getRowField() + ) + ->join( + "dados_cadastrais", + "dados_cadastrais.cod_smart_unidade", + "=", + "economia.cod_smart_unidade", + ); + + dd( $query->limit(5)->get()); + + } + + + protected function where($query) + { + return $query->where( + DB::raw( + + ) + ); + } + + + protected function getRowField(): array + { + return [ + DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY') as mes"), + DB::raw("TRIM(TO_CHAR(economia.custo_cativo, '99999999.99')) as custo_cativo"), + DB::raw("TRIM(TO_CHAR(economia.custo_livre, '99999999.99')) as custo_livre"), + DB::raw("COALESCE(economia.economia_mensal / NULLIF(economia.custo_livre, 0), 0) as custo") + ]; + } + +} diff --git a/app/Repositories/Faqs/FaqContractInterface.php b/app/Repositories/Faqs/FaqContractInterface.php new file mode 100644 index 0000000..f32706c --- /dev/null +++ b/app/Repositories/Faqs/FaqContractInterface.php @@ -0,0 +1,10 @@ +model->all(); + } + + public function find($id) + { + return $this->model->find($id); + } + + public function create(array $params) + { + return $this->model->create($params); + } + + public function update(array $params, $id) + { + ($model = $this->model->findOrFail($id))->update($params); + return $model; + } + + public function destroy($id) + { + return $this->model::find($id)->delete(); + } + + public function withRelationsByAll($relations): Collection|array + { + return $this->model->with($relations)->get(); + } +} \ No newline at end of file diff --git a/app/Repositories/Notifications/NotificationContractInterface.php b/app/Repositories/Notifications/NotificationContractInterface.php new file mode 100644 index 0000000..f189bba --- /dev/null +++ b/app/Repositories/Notifications/NotificationContractInterface.php @@ -0,0 +1,10 @@ +model = $this->resolveModel(); + } + + public function getJsonObject($json, $className) + { + try { + $jsonData = json_decode(json_encode($json), false); + + $obj = new $className; + + if (!isset($jsonData) && method_exists($obj, 'jsonToObject')) + { + throw new Exception("Request inválido"); + } + + $obj->jsonSetObject($jsonData); + + return $obj; + + } catch (Exception $ex) { + return $ex->getMessage(); + } + } + + public function __get($name) + { + return $this->model->{$name}; + } + + /** + * @throws BindingResolutionException + * @throws Exception + */ + protected function resolveModel(): Model + { + $model = app()->make($this->model()); + + if (!$model instanceof Model) { + throw new Exception( + "Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model" + ); + } + return $model; + } + +} diff --git a/app/Repository/Economy/EconomyRepository.php b/app/Repository/Economy/EconomyRepository.php new file mode 100644 index 0000000..db0c564 --- /dev/null +++ b/app/Repository/Economy/EconomyRepository.php @@ -0,0 +1,56 @@ +getFilterBuilder($params); + + foreach ($filter->getFilters() as $filters) + { + if (!$filters->getRow()) + { + continue; + } + $filters->setField(DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY')")); + } + + $field = collect($filter->getFields())->merge($this->getRowField())->all(); + + $query = $this->model->newQuery() + ->select($field) + ->join( + "dados_cadastrais", + "dados_cadastrais.cod_smart_unidade", + "=", + "economia.cod_smart_unidade", + ); + + $res = $filter->applyFilter($query); + + return $res->paginate(); + + } + + public function getRowField(): array + { + return [ + DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY') as mes"), + DB::raw("TRIM(TO_CHAR(economia.custo_cativo, '99999999.99')) as custo_cativo"), + DB::raw("TRIM(TO_CHAR(economia.custo_livre, '99999999.99')) as custo_livre"), + DB::raw("COALESCE(economia.economia_mensal / NULLIF(economia.custo_livre, 0), 0) as custo") + ]; + } + + protected function model(): string + { + return Economy::class; + } +} diff --git a/app/Repository/Med5min/Med5minRepository.php b/app/Repository/Med5min/Med5minRepository.php new file mode 100644 index 0000000..467f779 --- /dev/null +++ b/app/Repository/Med5min/Med5minRepository.php @@ -0,0 +1,20 @@ +model->all(); + } + + public function create(array $params) + { + return $this->model->create($params); + } + + public function find(int $id) + { + // TODO: Implement find() method. + } + + public function update(array $params, int $id) + { + return $this->model->find($id)->update( $params); + } + + public function delete(int $id) + { + // TODO: Implement delete() method. + } + + public function getFilterBuilder($json): ?FilterBuilder + { + return $this->getJsonObject($json, FilterBuilder::class); + } + + + static function logQuery($query, $type = 'get') + { + DB::enableQueryLog(); + $query->$type(); + dd(DB::getQueryLog()); + } + + +} diff --git a/app/Support/FilterQueryBuilder.php b/app/Support/FilterQueryBuilder.php new file mode 100644 index 0000000..b346c39 --- /dev/null +++ b/app/Support/FilterQueryBuilder.php @@ -0,0 +1,54 @@ +initializeRequest($request); + + } + +// public function __construct($class) +// { +// $this->class = $class; +// +// return $this; +// } + + public static function for($request) + { + + return (new static($request)); + } + + + protected function initializeRequest(?Request $request = null): static + { + + $this->request = $request + ? QueryBuilderRequest::fromRequest($request) + : app(QueryBuilderRequest::class); + + return $this; + } + + + public function __get($name) + { + return $this->class->{$name}; + } + + public function __set($name, $value) + { + $this->class->{$name} = $value; + } +} \ No newline at end of file diff --git a/app/Support/FiltersQuery.php b/app/Support/FiltersQuery.php new file mode 100644 index 0000000..4b6e8dc --- /dev/null +++ b/app/Support/FiltersQuery.php @@ -0,0 +1,8 @@ +getRequestData($includeParameterName); + + if (is_string($includeParts)) { + $includeParts = explode(static::getIncludesArrayValueDelimiter(), $includeParts); + } + + return collect($includeParts)->filter(); + } + + public function appends(): Collection + { + $appendParameterName = config('query-builder.parameters.append'); + + $appendParts = $this->getRequestData($appendParameterName); + + if (! is_array($appendParts) && ! is_null($appendParts)) { + $appendParts = explode(static::getAppendsArrayValueDelimiter(), $appendParts); + } + + return collect($appendParts)->filter(); + } + + public function fields(): Collection + { + $fieldsParameterName = config('query-builder.parameters.fields'); + + $fieldsPerTable = collect($this->getRequestData($fieldsParameterName)); + + if ($fieldsPerTable->isEmpty()) { + return collect(); + } + + return $fieldsPerTable->map(function ($fields) { + return explode(static::getFieldsArrayValueDelimiter(), $fields); + }); + } + + public function sorts(): Collection + { + $sortParameterName = config('query-builder.parameters.sort'); + + $sortParts = $this->getRequestData($sortParameterName); + + if (is_string($sortParts)) { + $sortParts = explode(static::getSortsArrayValueDelimiter(), $sortParts); + } + + return collect($sortParts)->filter(); + } + + public function filters(): Collection + { + $filterParameterName = config('query-builder.parameters.filter'); + + $filterParts = $this->getRequestData($filterParameterName, []); + + if (is_string($filterParts)) { + return collect(); + } + + $filters = collect($filterParts); + + return $filters->map(function ($value) { + return $this->getFilterValue($value); + }); + } + + /** + * @param $value + * + * @return array|bool + */ + protected function getFilterValue($value) + { + if (is_array($value)) { + return collect($value)->map(function ($valueValue) { + return $this->getFilterValue($valueValue); + })->all(); + } + + if (Str::contains($value, static::getFilterArrayValueDelimiter())) { + return explode(static::getFilterArrayValueDelimiter(), $value); + } + + if ($value === 'true') { + return true; + } + + if ($value === 'false') { + return false; + } + + return $value; + } + + protected function getRequestData(?string $key = null, $default = null) + { + if (config('query-builder.request_data_source') === 'body') { + return $this->input($key, $default); + } + + return $this->get($key, $default); + } + + public static function setIncludesArrayValueDelimiter(string $includesArrayValueDelimiter): void + { + static::$includesArrayValueDelimiter = $includesArrayValueDelimiter; + } + + public static function setAppendsArrayValueDelimiter(string $appendsArrayValueDelimiter): void + { + static::$appendsArrayValueDelimiter = $appendsArrayValueDelimiter; + } + + public static function setFieldsArrayValueDelimiter(string $fieldsArrayValueDelimiter): void + { + static::$fieldsArrayValueDelimiter = $fieldsArrayValueDelimiter; + } + + public static function setSortsArrayValueDelimiter(string $sortsArrayValueDelimiter): void + { + static::$sortsArrayValueDelimiter = $sortsArrayValueDelimiter; + } + + public static function setFilterArrayValueDelimiter(string $filterArrayValueDelimiter): void + { + static::$filterArrayValueDelimiter = $filterArrayValueDelimiter; + } + + public static function getIncludesArrayValueDelimiter(): string + { + return static::$includesArrayValueDelimiter; + } + + public static function getAppendsArrayValueDelimiter(): string + { + return static::$appendsArrayValueDelimiter; + } + + public static function getFieldsArrayValueDelimiter(): string + { + return static::$fieldsArrayValueDelimiter; + } + + public static function getSortsArrayValueDelimiter(): string + { + return static::$sortsArrayValueDelimiter; + } + + public static function getFilterArrayValueDelimiter(): string + { + return static::$filterArrayValueDelimiter; + } + + public static function resetDelimiters(): void + { + self::$includesArrayValueDelimiter = ','; + self::$appendsArrayValueDelimiter = ','; + self::$fieldsArrayValueDelimiter = ','; + self::$sortsArrayValueDelimiter = ','; + self::$filterArrayValueDelimiter = ','; + } +} diff --git a/app/Traits/ApiResponse.php b/app/Traits/ApiResponse.php new file mode 100644 index 0000000..ba9df34 --- /dev/null +++ b/app/Traits/ApiResponse.php @@ -0,0 +1,27 @@ +json([ + 'status' => 'Success', + 'message' => $message, + 'data' => $data + ], $code); + } + + protected function errorResponse ($data, string $message, int $code): JsonResponse + { + return response()->json([ + 'status' => 'Error', + 'message' => $message, + 'data' => $data + ], $code); + } + +} diff --git a/composer.json b/composer.json index 3bac7c7..b1e4cfb 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "laravel/framework": "^9.11", "laravel/sanctum": "^2.14.1", "laravel/tinker": "^2.7", - "owen-it/laravel-auditing": "^13.0" + "owen-it/laravel-auditing": "^13.0", + "umbrellio/laravel-pg-extensions": "^5.3" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/composer.lock b/composer.lock index 006f6e2..618e1c8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8d22be6a885e9930e292c80a7eee9a14", + "content-hash": "d8590635104440a9a9893816079d3e83", "packages": [ { "name": "brick/math", @@ -141,6 +141,353 @@ }, "time": "2021-08-13T13:06:58+00:00" }, + { + "name": "doctrine/cache", + "version": "2.1.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/cache.git", + "reference": "331b4d5dbaeab3827976273e9356b3b453c300ce" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/cache/zipball/331b4d5dbaeab3827976273e9356b3b453c300ce", + "reference": "331b4d5dbaeab3827976273e9356b3b453c300ce", + "shasum": "" + }, + "require": { + "php": "~7.1 || ^8.0" + }, + "conflict": { + "doctrine/common": ">2.2,<2.4" + }, + "require-dev": { + "alcaeus/mongo-php-adapter": "^1.1", + "cache/integration-tests": "dev-master", + "doctrine/coding-standard": "^8.0", + "mongodb/mongodb": "^1.1", + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0", + "predis/predis": "~1.0", + "psr/cache": "^1.0 || ^2.0 || ^3.0", + "symfony/cache": "^4.4 || ^5.2 || ^6.0@dev", + "symfony/var-exporter": "^4.4 || ^5.2 || ^6.0@dev" + }, + "suggest": { + "alcaeus/mongo-php-adapter": "Required to use legacy MongoDB driver" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\Cache\\": "lib/Doctrine/Common/Cache" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.", + "homepage": "https://www.doctrine-project.org/projects/cache.html", + "keywords": [ + "abstraction", + "apcu", + "cache", + "caching", + "couchdb", + "memcached", + "php", + "redis", + "xcache" + ], + "support": { + "issues": "https://github.com/doctrine/cache/issues", + "source": "https://github.com/doctrine/cache/tree/2.1.1" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fcache", + "type": "tidelift" + } + ], + "time": "2021-07-17T14:49:29+00:00" + }, + { + "name": "doctrine/dbal", + "version": "3.3.6", + "source": { + "type": "git", + "url": "https://github.com/doctrine/dbal.git", + "reference": "9e7f76dd1cde81c62574fdffa5a9c655c847ad21" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/dbal/zipball/9e7f76dd1cde81c62574fdffa5a9c655c847ad21", + "reference": "9e7f76dd1cde81c62574fdffa5a9c655c847ad21", + "shasum": "" + }, + "require": { + "composer-runtime-api": "^2", + "doctrine/cache": "^1.11|^2.0", + "doctrine/deprecations": "^0.5.3|^1", + "doctrine/event-manager": "^1.0", + "php": "^7.3 || ^8.0", + "psr/cache": "^1|^2|^3", + "psr/log": "^1|^2|^3" + }, + "require-dev": { + "doctrine/coding-standard": "9.0.0", + "jetbrains/phpstorm-stubs": "2022.1", + "phpstan/phpstan": "1.6.3", + "phpstan/phpstan-strict-rules": "^1.2", + "phpunit/phpunit": "9.5.20", + "psalm/plugin-phpunit": "0.16.1", + "squizlabs/php_codesniffer": "3.6.2", + "symfony/cache": "^5.2|^6.0", + "symfony/console": "^2.7|^3.0|^4.0|^5.0|^6.0", + "vimeo/psalm": "4.23.0" + }, + "suggest": { + "symfony/console": "For helpful console commands such as SQL execution and import of files." + }, + "bin": [ + "bin/doctrine-dbal" + ], + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\DBAL\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + } + ], + "description": "Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.", + "homepage": "https://www.doctrine-project.org/projects/dbal.html", + "keywords": [ + "abstraction", + "database", + "db2", + "dbal", + "mariadb", + "mssql", + "mysql", + "oci8", + "oracle", + "pdo", + "pgsql", + "postgresql", + "queryobject", + "sasql", + "sql", + "sqlite", + "sqlserver", + "sqlsrv" + ], + "support": { + "issues": "https://github.com/doctrine/dbal/issues", + "source": "https://github.com/doctrine/dbal/tree/3.3.6" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdbal", + "type": "tidelift" + } + ], + "time": "2022-05-02T17:21:01+00:00" + }, + { + "name": "doctrine/deprecations", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/deprecations.git", + "reference": "0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/deprecations/zipball/0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de", + "reference": "0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de", + "shasum": "" + }, + "require": { + "php": "^7.1|^8.0" + }, + "require-dev": { + "doctrine/coding-standard": "^9", + "phpunit/phpunit": "^7.5|^8.5|^9.5", + "psr/log": "^1|^2|^3" + }, + "suggest": { + "psr/log": "Allows logging deprecations via PSR-3 logger implementation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.", + "homepage": "https://www.doctrine-project.org/", + "support": { + "issues": "https://github.com/doctrine/deprecations/issues", + "source": "https://github.com/doctrine/deprecations/tree/v1.0.0" + }, + "time": "2022-05-02T15:47:09+00:00" + }, + { + "name": "doctrine/event-manager", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/event-manager.git", + "reference": "41370af6a30faa9dc0368c4a6814d596e81aba7f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/event-manager/zipball/41370af6a30faa9dc0368c4a6814d596e81aba7f", + "reference": "41370af6a30faa9dc0368c4a6814d596e81aba7f", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "conflict": { + "doctrine/common": "<2.9@dev" + }, + "require-dev": { + "doctrine/coding-standard": "^6.0", + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\": "lib/Doctrine/Common" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + }, + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com" + } + ], + "description": "The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.", + "homepage": "https://www.doctrine-project.org/projects/event-manager.html", + "keywords": [ + "event", + "event dispatcher", + "event manager", + "event system", + "events" + ], + "support": { + "issues": "https://github.com/doctrine/event-manager/issues", + "source": "https://github.com/doctrine/event-manager/tree/1.1.x" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fevent-manager", + "type": "tidelift" + } + ], + "time": "2020-05-29T18:28:51+00:00" + }, { "name": "doctrine/inflector", "version": "2.0.4", @@ -2161,6 +2508,55 @@ ], "time": "2021-12-04T23:24:31+00:00" }, + { + "name": "psr/cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/cache.git", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/cache/zipball/aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Cache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for caching libraries", + "keywords": [ + "cache", + "psr", + "psr-6" + ], + "support": { + "source": "https://github.com/php-fig/cache/tree/3.0.0" + }, + "time": "2021-02-03T23:26:27+00:00" + }, { "name": "psr/container", "version": "2.0.2", @@ -4960,6 +5356,79 @@ }, "time": "2021-12-08T09:12:39+00:00" }, + { + "name": "umbrellio/laravel-pg-extensions", + "version": "5.3.2", + "source": { + "type": "git", + "url": "https://github.com/umbrellio/laravel-pg-extensions.git", + "reference": "c20048f51cf29a6a87cd2ba7ed0a92579f90cf18" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/umbrellio/laravel-pg-extensions/zipball/c20048f51cf29a6a87cd2ba7ed0a92579f90cf18", + "reference": "c20048f51cf29a6a87cd2ba7ed0a92579f90cf18", + "shasum": "" + }, + "require": { + "doctrine/dbal": "^2.9|^3.0", + "laravel/framework": "^5.8|^6.0|^7.0|^8.0|^9.0", + "php": "^7.2|^7.3|^7.4|^8.0" + }, + "require-dev": { + "codeception/codeception": "^3.0|^4.0", + "orchestra/testbench": "^3.5|^6.0|^4.0", + "php-coveralls/php-coveralls": "^2.1", + "umbrellio/code-style-php": "^1.0" + }, + "suggest": { + "umbrellio/laravel-common-objects": "Package with helpers for common Laravel components", + "umbrellio/laravel-ltree": "Package for working with Postgres LTree extension" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Umbrellio\\Postgres\\UmbrellioPostgresProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Umbrellio\\Postgres\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Vitaliy Lazeev", + "email": "vetal@umbrellio.biz" + }, + { + "name": "Korben Dallas", + "email": "pvsaintpe@umbrellio.biz" + } + ], + "description": "Extensions for Postgres Laravel", + "keywords": [ + "builder", + "extension", + "laravel", + "migrations", + "php", + "postgres", + "postgresql", + "schema" + ], + "support": { + "issues": "https://github.com/umbrellio/laravel-pg-extensions/issues", + "source": "https://github.com/umbrellio/laravel-pg-extensions" + }, + "time": "2022-02-24T12:46:20+00:00" + }, { "name": "vlucas/phpdotenv", "version": "v5.4.1", @@ -7840,8 +8309,8 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": "^8.0.2" + "php": "^8.1" }, "platform-dev": [], - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.3.0" } diff --git a/database/factories/NewsFactory.php b/database/factories/NewsFactory.php index 33fbd9e..e9277aa 100644 --- a/database/factories/NewsFactory.php +++ b/database/factories/NewsFactory.php @@ -5,7 +5,7 @@ namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\News> + * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Question> */ class NewsFactory extends Factory { diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_06_08_000000_create_users_table.php similarity index 87% rename from database/migrations/2014_10_12_000000_create_users_table.php rename to database/migrations/2014_06_08_000000_create_users_table.php index 374f804..2dadc8d 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_06_08_000000_create_users_table.php @@ -15,6 +15,7 @@ return new class extends Migration { Schema::create('users', function (Blueprint $table) { $table->id(); + $table->bigInteger('client_id', false, true)->unique()->nullable(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); @@ -22,6 +23,8 @@ return new class extends Migration $table->rememberToken(); $table->timestamps(); $table->softDeletes(); + + $table->index(['client_id']); }); } diff --git a/database/migrations/2022_05_12_022040_create_faqs_table.php b/database/migrations/2022_05_12_022040_create_faqs_table.php index 4cab10b..7d27c43 100644 --- a/database/migrations/2022_05_12_022040_create_faqs_table.php +++ b/database/migrations/2022_05_12_022040_create_faqs_table.php @@ -11,10 +11,12 @@ return new class extends Migration * * @return void */ - public function up() + public function up(): void { Schema::create('faqs', function (Blueprint $table) { $table->id(); + $table->string('question'); + $table->text('answer'); $table->timestamps(); $table->softDeletes(); }); diff --git a/database/migrations/2022_05_12_171300_create_dados_cadastrais_table.php b/database/migrations/2022_05_12_171300_create_dados_cadastrais_table.php index 44e402b..38d3386 100644 --- a/database/migrations/2022_05_12_171300_create_dados_cadastrais_table.php +++ b/database/migrations/2022_05_12_171300_create_dados_cadastrais_table.php @@ -14,16 +14,16 @@ return new class extends Migration public function up(): void { Schema::create('dados_cadastrais', function (Blueprint $table) { - $table->bigIncrements('cod_smart_unidade'); - $table->text('cliente'); - $table->text('unidade'); - $table->text('codigo_scde'); - $table->decimal('demanda_p'); - $table->decimal('demanda_fp'); - $table->text('status_empresa'); - $table->text('status_unidade'); - $table->decimal('data_de_migracao'); - $table->bigInteger('cod_smart_cliente'); + $table->bigInteger('cod_smart_unidade', false)->primary()->default(null); + $table->text('cliente')->nullable(); + $table->text('unidade')->nullable(); + $table->text('codigo_scde')->nullable(); + $table->numeric('demanda_p')->nullable(); + $table->numeric('demanda_fp')->nullable(); + $table->text('status_empresa')->nullable(); + $table->text('status_unidade')->nullable(); + $table->numeric('data_de_migracao')->nullable(); + $table->bigInteger('cod_smart_cliente')->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/database/migrations/2022_05_12_173518_create_economia_table.php b/database/migrations/2022_05_12_173518_create_economia_table.php index a424853..830169b 100644 --- a/database/migrations/2022_05_12_173518_create_economia_table.php +++ b/database/migrations/2022_05_12_173518_create_economia_table.php @@ -14,20 +14,23 @@ return new class extends Migration public function up(): void { Schema::create('economia', function (Blueprint $table) { - $table->bigIncrements('cod_econ'); - $table->bigInteger('cod_smart_unidade'); - $table->text('mes text'); - $table->decimal('custo_cativo'); - $table->decimal('custo_livre'); - $table->decimal('economia_mensal'); - $table->decimal('economia_acumulada'); - $table->decimal('custo_unit'); - $table->boolean('dad_estimado'); + + $table->bigInteger('cod_te')->unsigned(); + $table->bigInteger('cod_smart_unidade')->unsigned(); + $table->text('mes')->nullable()->default(null); + $table->numeric('custo_cativo')->nullable(); + $table->numeric('custo_livre')->nullable(); + $table->numeric('economia_mensal')->nullable(); + $table->numeric('economia_acumulada')->nullable(); + $table->numeric('custo_unit')->nullable(); + $table->boolean('dad_estimado')->nullable(); $table->timestamps(); $table->softDeletes(); $table->foreign('cod_smart_unidade', 'economia_cod_smart_unidade_fkey') - ->references('cod_smart_unidade')->on('dados_cadastrais'); + ->references('cod_smart_unidade')->on('dados_cadastrais')->onDelete('no action')->onUpdate('no action')->notValid(); + + $table->primary(['cod_te','cod_smart_unidade']); }); } diff --git a/database/migrations/2022_05_12_174204_create_pld_table.php b/database/migrations/2022_05_12_174204_create_pld_table.php index 9038f24..94114f3 100644 --- a/database/migrations/2022_05_12_174204_create_pld_table.php +++ b/database/migrations/2022_05_12_174204_create_pld_table.php @@ -15,12 +15,12 @@ return new class extends Migration { Schema::create('pld', function (Blueprint $table) { $table->integerIncrements('id'); - $table->decimal('dia_num')->nullable(); - $table->decimal('hora')->nullable(); + $table->numeric('dia_num')->nullable(); + $table->numeric('hora')->nullable(); $table->text('submercado')->nullable(); - $table->decimal('valor')->nullable(); + $table->numeric('valor')->nullable(); $table->string('mes_ref')->nullable(); - $table->decimal('dia_da_semana')->nullable(); + $table->numeric('dia_da_semana')->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/database/migrations/2022_05_12_174614_create_med5min_table.php b/database/migrations/2022_05_12_174614_create_med5min_table.php index bb8035b..783799a 100644 --- a/database/migrations/2022_05_12_174614_create_med5min_table.php +++ b/database/migrations/2022_05_12_174614_create_med5min_table.php @@ -15,14 +15,14 @@ return new class extends Migration { Schema::create('med_5min', function (Blueprint $table) { $table->bigIncrements('id'); - $table->text('origem'); - $table->decimal('dia_num'); - $table->integer('minuto'); - $table->decimal('ativa_consumo'); - $table->decimal('ativa_geracao'); - $table->decimal('reativa_consumo'); - $table->decimal('reativa_geracao'); - $table->text('ponto'); + $table->text('origem')->nullable(); + $table->decimal('dia_num')->nullable(); + $table->integer('minuto')->nullable(); + $table->numeric('ativa_consumo')->nullable(); + $table->numeric('ativa_geracao')->nullable(); + $table->numeric('reativa_consumo')->nullable(); + $table->numeric('reativa_geracao')->nullable(); + $table->text('ponto')->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/database/migrations/2022_05_12_175125_create_dados_te_table.php b/database/migrations/2022_05_12_175125_create_dados_te_table.php index 0ebfcb4..084f38c 100644 --- a/database/migrations/2022_05_12_175125_create_dados_te_table.php +++ b/database/migrations/2022_05_12_175125_create_dados_te_table.php @@ -14,20 +14,22 @@ return new class extends Migration public function up(): void { Schema::create('dados_te', function (Blueprint $table) { - $table->bigIncrements('cod_te'); - $table->bigInteger('cod_smart_unidade')->nullable(); - $table->text('mes')->nullable();; + $table->bigInteger('cod_te')->unsigned(); + $table->bigInteger('cod_smart_unidade')->unsigned(); + $table->text('mes')->nullable(); $table->text('operacao')->nullable(); $table->text('tipo')->nullable(); - $table->decimal('montante_nf')->nullable(); - $table->decimal('preco_nf')->nullable(); - $table->decimal('nf_c_icms')->nullable(); + $table->numeric('montante_nf')->nullable(); + $table->numeric('preco_nf')->nullable(); + $table->numeric('nf_c_icms')->nullable(); $table->text('perfil_contr')->nullable(); $table->timestamps(); $table->softDeletes(); $table->foreign('cod_smart_unidade', 'dados_te_cod_smart_unidade_fkey') - ->references('cod_smart_unidade')->on('dados_cadastrais')->onDelete('no action')->onUpdate('no action'); + ->references('cod_smart_unidade')->on('dados_cadastrais')->onDelete('no action')->onUpdate('no action')->notValid(); + + $table->primary(['cod_te','cod_smart_unidade']); }); } diff --git a/database/migrations/2022_05_13_155256_create_notifications_table.php b/database/migrations/2022_05_13_155256_create_notifications_table.php index 07a224b..15c41fa 100644 --- a/database/migrations/2022_05_13_155256_create_notifications_table.php +++ b/database/migrations/2022_05_13_155256_create_notifications_table.php @@ -15,10 +15,11 @@ return new class extends Migration { Schema::create('notificacoes', function (Blueprint $table) { $table->id(); - $table->string('cliente_id'); - $table->text('texto'); + $table->string('title'); + $table->text('body')->nullable(); $table->timestamps(); $table->softDeletes(); + }); } diff --git a/database/migrations/2022_05_13_161015_create_parameters_table.php b/database/migrations/2022_05_13_161015_create_parameters_table.php index dd0f1a9..27879d5 100644 --- a/database/migrations/2022_05_13_161015_create_parameters_table.php +++ b/database/migrations/2022_05_13_161015_create_parameters_table.php @@ -15,7 +15,7 @@ return new class extends Migration { Schema::create('parametros', function (Blueprint $table) { $table->integerIncrements('codigo'); - $table->string('argumento'); + $table->string('argumento')->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/database/migrations/2022_06_09_022214_create_notifications_users_pivot_table.php b/database/migrations/2022_06_09_022214_create_notifications_users_pivot_table.php new file mode 100644 index 0000000..e35b86c --- /dev/null +++ b/database/migrations/2022_06_09_022214_create_notifications_users_pivot_table.php @@ -0,0 +1,43 @@ +id(); +// $table->bigInteger('notification_id')->unsigned()->nullable(); +// $table->foreign('notification_id')->references('id')->on('notificacoes')->onDelete('cascade'); +// $table->bigInteger('user_id')->unsigned()->nullable(); +// $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); +// +// + + $table->unsignedBigInteger('notification_id'); + $table->unsignedBigInteger('user_id'); + $table->foreign('notification_id')->references('id')->on('notificacoes') + ->onUpdate('cascade')->onDelete('cascade'); + $table->foreign('user_id')->references('id')->on('users') + ->onUpdate('cascade')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('notificacoes_users'); + } +}; diff --git a/database/seeders/RoleUserTableSeeder.php b/database/seeders/RoleUserTableSeeder.php index 26cc044..77349ac 100644 --- a/database/seeders/RoleUserTableSeeder.php +++ b/database/seeders/RoleUserTableSeeder.php @@ -10,5 +10,7 @@ class RoleUserTableSeeder extends Seeder public function run() { User::findOrFail(1)->roles()->sync(1); + User::findOrFail(2)->roles()->sync(1); + User::findOrFail(3)->roles()->sync(2); } } diff --git a/database/seeders/UserTableSeeder.php b/database/seeders/UserTableSeeder.php index 61bcff7..a174ce8 100644 --- a/database/seeders/UserTableSeeder.php +++ b/database/seeders/UserTableSeeder.php @@ -16,6 +16,7 @@ class UserTableSeeder extends Seeder public function run(): void { User::query()->create([ + 'client_id' => null, 'name' => 'Admin', 'email' => 'admin@admin.com', 'password' => bcrypt('password'), @@ -23,6 +24,15 @@ class UserTableSeeder extends Seeder ]); User::query()->create([ + 'client_id' => null, + 'name' => 'Admin', + 'email' => 'admin2@admin.com', + 'password' => bcrypt('password'), + 'remember_token' => null, + ]); + + User::query()->create([ + 'client_id' => 180103211, 'name' => 'Client', 'email' => 'client@client.com', 'password' => bcrypt('password'), diff --git a/docker-compose.yml b/docker-compose.yml index d21d912..e3067be 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ version: '3' services: laravel.test: build: - context: ./docker/8.1 + context: ./vendor/laravel/sail/runtimes/8.1 dockerfile: Dockerfile args: WWWGROUP: '${WWWGROUP}' diff --git a/routes/api.php b/routes/api.php index 558d08e..7c5582d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -17,12 +17,21 @@ use Illuminate\Support\Facades\Route; Route::prefix('auth')->group(function (){ Route::post('login', [\App\Http\Controllers\Auth\AuthController::class, 'login']); Route::post('logout', [\App\Http\Controllers\Auth\AuthController::class, 'logout']); - Route::post('register', [\App\Http\Controllers\Auth\RegisterController::class, 'store']); }); Route::middleware(['auth:sanctum', 'verified'])->group(function () { - Route::get('teste', [\App\Http\Controllers\EconomyController::class, 'index']); + + Route::post('pld/overview', [\App\Http\Controllers\PldController::class, 'overviewByRegion']); + Route::post('pld/list', [\App\Http\Controllers\PldController::class, 'listConsumption']); + Route::post('pld/daily', [\App\Http\Controllers\PldController::class, 'consumptionByDaily']); + Route::post('pld/schedule', [\App\Http\Controllers\PldController::class, 'consumptionBySchedule']); + + Route::post('economy', \App\Http\Controllers\EconomyController::class); + Route::apiResource('user', \App\Http\Controllers\UserController::class); + Route::apiResource('notification', \App\Http\Controllers\NotificationController::class); + Route::apiResource('faq', \App\Http\Controllers\FaqController::class); + });