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

Submissão dos arquivos para o desafio-backend. #8

Open
wants to merge 1 commit 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
15 changes: 15 additions & 0 deletions .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
49 changes: 49 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=financial_transactions
DB_USERNAME=root
DB_PASSWORD=

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

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
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 .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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.phpunit.result.cache
docker-compose.override.yml
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
13 changes: 13 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
php:
preset: laravel
disabled:
- no_unused_imports
finder:
not-name:
- index.php
- server.php
js:
finder:
not-name:
- webpack.mix.js
css: true
41 changes: 41 additions & 0 deletions 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');
}
}
94 changes: 94 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Exception;

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 = [
'current_password',
'password',
'password_confirmation',
'api_token',
];

/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Exception $e) {
//
});
}

public function render($request, Throwable $e)
{
if (null !== $e->getStatusCode()) {
return response()->json([
'error_' . $e->getStatusCode() => 'An error was found on your request. Please use one of the following routes to continue:',
'.---------' => '----------',
'USERS' => '-',
'/register (POST)' => 'Registers a new user. Returns the new user\'s API key,',
'/adduser (POST)' => 'Adds a new user. Returns the new user\'s API key,',
'/listusers/{page_number}/{quantity_by_page} (POST)' => 'Lists information of all users,',
'/listuser/{id} (POST)' => 'Lists information of only one user,',
'/edituser/{id} (PUT)' => 'Edits the information of a user,',
'/removeuser/{id} (DELETE)' => 'Deletes a user,',
'/userseeder/{quantity} (POST)' => 'Seeds new users.',
'..--------' => '----------',
'TRANSACTIONS' => '-',
'/addcharge/{id} (POST)' => 'Adds charges to the user\'s balance,',
'/listinformation/{id}/{page_number}/{quantity_by_page} (POST)' => 'Lists information of a user\'s balance,',
'/chargesreport/{id}/{filter} (POST)' => 'Lists information of a user\'s balance in a period of time,',
'/sumtransactions/{id} (POST)' => 'Lists user\'s transactions, and initial and current balance,',
'/editbalance/{id} (PUT)' => 'Edits user\'s initial balance,',
'/removecharge/{id} (DELETE)' => 'Deletes a charge from a user\'s balance,',
'/transactionseeder/{id}/{quantity} (POST)' => 'Seeds charges to a user\'s balance.',
]);
} else {
return response()->json([
'error' => 'The server found and error and could not recover. Please use one of the following routes to continue:',
'.---------' => '----------',
'USERS' => '-',
'/register (POST)' => 'Registers a new user. Returns the new user\'s API key,',
'/adduser (POST)' => 'Adds a new user. Returns the new user\'s API key,',
'/listusers/{page_number}/{quantity_by_page} (POST)' => 'Lists information of all users,',
'/listuser/{id} (POST)' => 'Lists information of only one user,',
'/edituser/{id} (PUT)' => 'Edits the information of a user,',
'/removeuser/{id} (DELETE)' => 'Deletes a user,',
'/userseeder/{quantity} (POST)' => 'Seeds new users.',
'..--------' => '----------',
'TRANSACTIONS' => '-',
'/addcharge/{id} (POST)' => 'Adds charges to the user\'s balance,',
'/listinformation/{id}/{page_number}/{quantity_by_page} (POST)' => 'Lists information of a user\'s balance,',
'/chargesreport/{id}/{filter} (POST)' => 'Lists information of a user\'s balance in a period of time,',
'/sumtransactions/{id} (POST)' => 'Lists user\'s transactions, and initial and current balance,',
'/editbalance/{id} (PUT)' => 'Edits user\'s initial balance,',
'/removecharge/{id} (DELETE)' => 'Deletes a charge from a user\'s balance,',
'/transactionseeder/{id}/{quantity} (POST)' => 'Seeds charges to a user\'s balance.',
]);
}

return parent::render($request, $e);
}
}
62 changes: 62 additions & 0 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Http\Controllers;

use AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Models\User;

class AuthController extends Controller
{
public function register(Request $request){
if (strtolower(request()->method()) != 'post') {
return response()->json([
'request_method_error' => 'Not allowed. This endpoint should be accessed through POST method.'
]);
}

$validator = Validator::make($request->all(), [
'name' => 'required|string',
'email' => 'required|string|email|unique:users',
'birthday' => 'required|date',
'password' => 'required|min:8',
'initial_balance' => 'required|numeric|between:0,9999999999.99'
]);

if ($validator->fails()) {
return response()->json($validator->errors(), 401);
} else {
$post_data = [
'name' => $request->name,
'email' => $request->email,
'birthday' => $request->birthday,
'password' => $request->password,
'initial_balance' => $request->initial_balance
];
}

$user = User::create([
'name' => $post_data['name'],
'email' => $post_data['email'],
'birthday' => $post_data['birthday'],
'password' => Hash::make($post_data['password']),
'initial_balance' => $post_data['initial_balance'],
'current_balance' => $post_data['initial_balance']
]);

$token = $user->createToken('authToken')->plainTextToken;

User::where('email', $post_data['email'])
->update(['api_token' => $token]);

return response()->json([
'api_token' => $token,
'token_type' => 'Bearer',
]);
}
}
13 changes: 13 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
Loading