Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desafio de BACK-END finalizado. #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Editor directories and files
.idea
15 changes: 15 additions & 0 deletions ow-interactive/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
47 changes: 47 additions & 0 deletions ow-interactive/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost:3000

LOG_CHANNEL=stack

DB_CONNECTION=mysql
#DB_HOST = mysql service name in the docker container
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=ow-interactive
DB_USERNAME=root
DB_PASSWORD=root

BROADCAST_DRIVER=log
CACHE_DRIVER=redis
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=ow-interactive-redis
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
5 changes: 5 additions & 0 deletions ow-interactive/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* text=auto
*.css linguist-vendored
*.scss linguist-vendored
*.js linguist-vendored
CHANGELOG.md export-ignore
17 changes: 17 additions & 0 deletions ow-interactive/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
node_modules
/public/hot
/public/storage
/storage/*.key
bootstrap/cache/services.php
/vendor
.env.*.php
.env.php
.env
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
docker-compose.yml
app/storage/
/.idea
13 changes: 13 additions & 0 deletions ow-interactive/.styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
php:
preset: laravel
disabled:
- unused_use
finder:
not-name:
- index.php
- server.php
js:
finder:
not-name:
- webpack.mix.js
css: true
88 changes: 88 additions & 0 deletions ow-interactive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# OW-INTERACTIVE API

## Clone
* Execute the following command to get the latest version of the project
```
$ git clone --recursive git@github.com:demartinezraul/desafio-backend.git ow-interactive
```

## Setting up a development environment

* create a file named .env (copy .env.example file) which should contain the following default setup ( you should provide your own values to this variables ):
```
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=ow-interactive
DB_USERNAME=root
DB_PASSWORD=root
```

## Running the app on DOCKER

* create a file named docker-compose.yml (copy docker-compose.yml.example file) which should contain the following default setup ( you should provide your own values to this variables ):
* Exec `docker-compose up -d` to create containers.
* Exec `docker-compose exec php-fpm bash` enter the container project
```
# inside the application container.

$ composer install
$ php artisan key:generate
$ php artisan storage:link
$ php artisan migrate:fresh --seed
$ php artisan passport:install
$ php artisan l5-swagger:generate

```

## Set permissions
Execute the following commands to set the write permission on the cache folder:
```
$ cd ow-interactive
$ sudo chmod -R 777 storage/
```
## Access the application on SWAGGER

```
http://localhost:3000/api/documentation
```

## Login SWAGGER
Click the "Authorize" button on the page of the link above.

```
username: admin@admin.com
password: admin
Client credentials location: Authorization Header
cliente_id: 2
client_secret: Token generated by the passport is found in the oauth_secrets table secret column of ID 2
```
After that, you will be authenticated in the system and will be able to access all routes.

## Access JSON SWAGGER

```
http://localhost:3000/docs/api-docs.json
```

## API's

* Users register - Route: /api/users/register [POST],
* Users list - Route: /api/users [GET],
* Users show - Route: /api/users/{id} [GET],
* Users update - Route: /api/users/{id} [PATCH],
* Users current balance - Route: /api/users/{id}/current-balance [GET],
* Users delete - Route: /api/users/destroy/{id} [DELETE],
---
* Transactions list - Route: /api/transactions [GET],
* Transactions create - Route: /api/transactions/store [POST],
* Transactions delete - Route: /api/transactions/destroy/{id} [DELETE],
* Transactions export - Route: /api/transactions/export [POST],

## Run automated tests (PHPUnit)
* Run the command below on the terminal to process the tests.
```
composer test
or
vendor/bin/phpunit
```
41 changes: 41 additions & 0 deletions ow-interactive/app/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
}

/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');
}
}
84 changes: 84 additions & 0 deletions ow-interactive/app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Response;
use Throwable;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];

/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];

/**
* Report or log an exception.
*
* @param Throwable $exception
* @return void
*
* @throws Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}

/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Throwable $exception
* @return Response
*
* @throws Throwable
*/
public function render($request, Throwable $exception)
{
/**
* Verifica se o retorno é através de alguma rota da API.
* Verifica se a exception é de validação
*/
if ($request->is('api/*')) {
if ($exception instanceof ValidationException) {
return response()->json($exception->errors(), $exception->status);
}
}

return parent::render($request, $exception);
}

/**
* Alterar método de "usuário não autenticados"
* Caso não esteja autenticado, será exibido a mensagem "Usuário não autorizado.".
*
* @param Request $request
* @param AuthenticationException $exception
* @return JsonResponse
*/
protected function unauthenticated($request, AuthenticationException $exception): JsonResponse
{
return response()->json(['error' => 'Usuário não autorizado.'], 401);
}
}
31 changes: 31 additions & 0 deletions ow-interactive/app/Exports/TransactionsExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromView;

class TransactionsExport implements FromView
{
private $transactions;

/**
* TransactionsExport constructor.
* @param Collection $transactions
*/
public function __construct(Collection $transactions)
{
$this->transactions = $transactions;
}

/**
* @return View
*/
public function view(): View
{
return view('transactions.excel', [
'transactions' => $this->transactions
]);
}
}
Loading