Skip to content

Commit

Permalink
adicionando patter Repository na API Conceito
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolinkk committed Jun 17, 2022
1 parent 008e077 commit f043390
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 12 deletions.
58 changes: 46 additions & 12 deletions app/Http/Controllers/ConceitoControllerApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,70 @@

use Illuminate\Http\Request;
use App\Models\Conceito;
use App\Interfaces\ConceitoRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class ConceitoControllerApi extends Controller
{

public function index()
private ConceitoRepositoryInterface $conceitoRepository;

public function __construct(ConceitoRepositoryInterface $conceitoRepository)
{
$this->conceitoRepository = $conceitoRepository;
}

public function index(): JsonResponse
{
return Conceito::all();
return response()->json([
'data' => $this->conceitoRepository->getAllConceitos()
]);
}


public function store(Request $request)
public function store(Request $request): JsonResponse
{
Conceito::create($request->all());

$conceitoDetails = $request->only([
'nome',
'descricao',
'user_id'
]);

return response()->json([
'data' => $this->conceitoRepository->createConceito($conceitoDetails)
],
Response::HTTP_CREATED
);
}


public function show($id)
public function show($id): JsonResponse
{
return Conceito::find($id);
$conceitoId = $id;
return response()->json([
'data' => $this->conceitoRepository->getConceitoById($conceitoId)
]);
}

public function update(Request $request, $id)
public function update(Request $request, $id): JsonResponse
{
$conceito = Conceito::find($id);
$conceito->update($request->all());
$conceitoId = $id;
$conceitoDetails = $request->only([
'nome',
'descricao',
]);

return response()->json([
'data' => $this->conceitoRepository->updateConceito($conceitoId,$conceitoDetails)
]);
}

public function destroy($id)
public function destroy($id): JsonResponse
{
$conceito = Conceito::find($id);
$conceito->delete();
$conceitoId = $id;
$this->conceitoRepository->deleteConceito($conceitoId);
return response()->json(null,Response::HTTP_NO_CONTENT);
}
}
12 changes: 12 additions & 0 deletions app/Interfaces/ConceitoRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Interfaces;

interface ConceitoRepositoryInterface
{
public function getAllConceitos();
public function getConceitoById($conceitoId);
public function deleteConceito($conceitoId);
public function createConceito(array $conceitoDetails);
public function updateConceito($conceitoId,array $newDetails);
}
30 changes: 30 additions & 0 deletions app/Providers/RepositoryServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Providers;

use App\Interfaces\ConceitoRepositoryInterface;
use App\Repositories\ConceitoRepository;
use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind(ConceitoRepositoryInterface::class,ConceitoRepository::class);
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
33 changes: 33 additions & 0 deletions app/Repositories/ConceitoRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Repositories;
use App\Interfaces\ConceitoRepositoryInterface;
use App\Models\Conceito;

class ConceitoRepository implements ConceitoRepositoryInterface
{
public function getAllConceitos()
{
return Conceito::all();
}

public function getConceitoById($conceitoId)
{
return Conceito::findOrFail($conceitoId);
}

public function deleteConceito($conceitoId)
{
Conceito::destroy($conceitoId);
}

public function createConceito(array $conceitoDetails)
{
return Conceito::create($conceitoDetails);
}

public function updateConceito($conceitoId, array $newDetails)
{
return Conceito::whereId($conceitoId)->update($newDetails);
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
App\PRoviders\RepositoryServiceProvider::class,

/*
* Package Service Providers...
Expand Down

0 comments on commit f043390

Please sign in to comment.