Skip to content

Commit

Permalink
primer commit subida del proyecto
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Jan 5, 2019
0 parents commit 56be68d
Show file tree
Hide file tree
Showing 42 changed files with 5,214 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
APP_NAME=Lumen
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://book.test
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=book
DB_USERNAME=homestead
DB_PASSWORD=secret





CACHE_DRIVER=file
QUEUE_CONNECTION=sync


ACCEPTED_SECRETS=
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
/.idea
Homestead.json
Homestead.yaml
.env
20 changes: 20 additions & 0 deletions app/Book.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'description',
'price',
'author_id',
];
}
Empty file added app/Console/Commands/.gitkeep
Empty file.
29 changes: 29 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\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)
{
//
}
}
10 changes: 10 additions & 0 deletions app/Events/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Events;

use Illuminate\Queue\SerializesModels;

abstract class Event
{
use SerializesModels;
}
16 changes: 16 additions & 0 deletions app/Events/ExampleEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Events;

class ExampleEvent extends Event
{
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
}
86 changes: 86 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Exceptions;

use Exception;
use App\Traits\ApiResponser;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;

class Handler extends ExceptionHandler
{
use ApiResponser;

/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];

/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if ($exception instanceof HttpException) {
$code = $exception->getStatusCode();
$message = Response::$statusTexts[$code];

return $this->errorResponse($message, $code);
}

if ($exception instanceof ModelNotFoundException) {
$model = strtolower(class_basename($exception->getModel()));

return $this->errorResponse("No existe una instacia de {$model} con el id enviado", Response::HTTP_NOT_FOUND);
}

if ($exception instanceof AuthorizationException) {
return $this->errorResponse($exception->getMessage(), Response::HTTP_FORBIDDEN);
}

if ($exception instanceof AuthenticationException) {
return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED);
}

if ($exception instanceof ValidationException) {
$errors = $exception->validator->errors()->getMessages();

return $this->errorResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
}

if (env('APP_DEBUG', false)) {
return parent::render($request, $exception);
}

return $this->errorResponse('Error inesperado, intente mas tarde', Response::HTTP_INTERNAL_SERVER_ERROR);

}
}
108 changes: 108 additions & 0 deletions app/Http/Controllers/BookController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace App\Http\Controllers;

use App\Book;
use App\Traits\ApiResponser;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class BookController extends Controller
{
use ApiResponser;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Retornamos un listado de book
* @return Illuminate\Http\Response
*/
public function index()
{
$books = Book::all();


return $this->successResponse($books);
}

/**
* Creamos un instacia de book
* @return Illuminate\Http\Response
*/
public function store(Request $request)
{
$rules = [
'title' => 'required|max:255',
'description' => 'required|max:255',
'price' => 'required|min:1',
'author_id' => 'required|min:1',
];


$this->validate($request, $rules);

$book = Book::create($request->all());

return $this->successResponse($book, Response::HTTP_CREATED);
}

/**
* Retornamos un book especifico
* @return Illuminate\Http\Response
*/
public function show($book)
{
$book = Book::findOrFail($book);

return $this->successResponse($book);
}

/**
* Actualizamos la información de un book
* @return Illuminate\Http\Response
*/
public function update(Request $request, $book)
{
$rules = [
'title' => 'max:255',
'description' => 'max:255',
'price' => 'min:1',
'author_id' => 'min:1',
];

$this->validate($request, $rules);

$book = Book::findOrFail($book);

$book->fill($request->all());

if ($book->isClean()) {
return $this->errorResponse('At least one value must change', Response::HTTP_UNPROCESSABLE_ENTITY);
}

$book->save();

return $this->successResponse($book);
}

/**
* Eliminamos un book
* @return Illuminate\Http\Response
*/
public function destroy($book)
{
$book = Book::findOrFail($book);

$book->delete();

return $this->successResponse($book);
}
}
10 changes: 10 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
//
}
18 changes: 18 additions & 0 deletions app/Http/Controllers/ExampleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Controllers;

class ExampleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}

//
}
44 changes: 44 additions & 0 deletions app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;

class Authenticate
{
/**
* The authentication guard factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;

/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}

return $next($request);
}
}
Loading

0 comments on commit 56be68d

Please sign in to comment.