Skip to content

Commit b91d97f

Browse files
committed
Criando paginação de bancos estilo materialize
1 parent 9e4d971 commit b91d97f

29 files changed

+797
-3667
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace CodeFin\Http\Controllers\Admin;
4+
5+
use CodeFin\Http\Controllers\Controller;
6+
use CodeFin\Http\Controllers\Response;
7+
use CodeFin\Http\Requests;
8+
use CodeFin\Http\Requests\BankCreateRequest;
9+
use CodeFin\Http\Requests\BankUpdateRequest;
10+
use CodeFin\Repositories\Interfaces\BankRepository;
11+
use Prettus\Validator\Contracts\ValidatorInterface;
12+
use Prettus\Validator\Exceptions\ValidatorException;
13+
14+
15+
class BanksController extends Controller
16+
{
17+
18+
/**
19+
* @var BankRepository
20+
*/
21+
protected $repository;
22+
23+
24+
public function __construct(BankRepository $repository)
25+
{
26+
$this->repository = $repository;
27+
}
28+
29+
30+
/**
31+
* Display a listing of the resource.
32+
*
33+
* @return \Illuminate\Http\Response
34+
*/
35+
public function index()
36+
{
37+
$banks = $this->repository->paginate(3);
38+
39+
if (request()->wantsJson()) {
40+
41+
return response()->json([
42+
'data' => $banks,
43+
]);
44+
}
45+
46+
return view('admin.banks.index', compact('banks'));
47+
}
48+
49+
public function create()
50+
{
51+
return view('banks.create');
52+
}
53+
54+
/**
55+
* Store a newly created resource in storage.
56+
*
57+
* @param BankCreateRequest $request
58+
*
59+
* @return \Illuminate\Http\Response
60+
*/
61+
public function store(BankCreateRequest $request)
62+
{
63+
64+
try {
65+
66+
$this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
67+
68+
$bank = $this->repository->create($request->all());
69+
70+
$response = [
71+
'message' => 'Bank created.',
72+
'data' => $bank->toArray(),
73+
];
74+
75+
if ($request->wantsJson()) {
76+
77+
return response()->json($response);
78+
}
79+
80+
return redirect()->back()->with('message', $response['message']);
81+
} catch (ValidatorException $e) {
82+
if ($request->wantsJson()) {
83+
return response()->json([
84+
'error' => true,
85+
'message' => $e->getMessageBag()
86+
]);
87+
}
88+
89+
return redirect()->back()->withErrors($e->getMessageBag())->withInput();
90+
}
91+
}
92+
93+
94+
/**
95+
* Show the form for editing the specified resource.
96+
*
97+
* @param int $id
98+
*
99+
* @return \Illuminate\Http\Response
100+
*/
101+
public function edit($id)
102+
{
103+
104+
$bank = $this->repository->find($id);
105+
106+
return view('banks.edit', compact('bank'));
107+
}
108+
109+
110+
/**
111+
* Update the specified resource in storage.
112+
*
113+
* @param BankUpdateRequest $request
114+
* @param string $id
115+
*
116+
* @return Response
117+
*/
118+
public function update(BankUpdateRequest $request, $id)
119+
{
120+
121+
try {
122+
123+
$this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
124+
125+
$bank = $this->repository->update($request->all(), $id);
126+
127+
$response = [
128+
'message' => 'Bank updated.',
129+
'data' => $bank->toArray(),
130+
];
131+
132+
if ($request->wantsJson()) {
133+
134+
return response()->json($response);
135+
}
136+
137+
return redirect()->back()->with('message', $response['message']);
138+
} catch (ValidatorException $e) {
139+
140+
if ($request->wantsJson()) {
141+
142+
return response()->json([
143+
'error' => true,
144+
'message' => $e->getMessageBag()
145+
]);
146+
}
147+
148+
return redirect()->back()->withErrors($e->getMessageBag())->withInput();
149+
}
150+
}
151+
152+
153+
/**
154+
* Remove the specified resource from storage.
155+
*
156+
* @param int $id
157+
*
158+
* @return \Illuminate\Http\Response
159+
*/
160+
public function destroy($id)
161+
{
162+
$deleted = $this->repository->delete($id);
163+
164+
if (request()->wantsJson()) {
165+
166+
return response()->json([
167+
'message' => 'Bank deleted.',
168+
'deleted' => $deleted,
169+
]);
170+
}
171+
172+
return redirect()->back()->with('message', 'Bank deleted.');
173+
}
174+
}

app/Http/Controllers/Auth/LoginController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace CodeFin\Http\Controllers\Auth;
44

55
use CodeFin\Http\Controllers\Controller;
6-
use CodeFin\User;
6+
use CodeFin\Models\User;
77
use Illuminate\Foundation\Auth\AuthenticatesUsers;
88
use Illuminate\Http\Request;
99

app/Http/Controllers/Auth/RegisterController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace CodeFin\Http\Controllers\Auth;
44

5-
use CodeFin\User;
5+
use CodeFin\Models\User;
66
use Validator;
77
use CodeFin\Http\Controllers\Controller;
88
use Illuminate\Foundation\Auth\RegistersUsers;
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace CodeFin\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class BankCreateRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return false;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
//
28+
];
29+
}
30+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace CodeFin\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class BankUpdateRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return false;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
//
28+
];
29+
}
30+
}

app/Models/Bank.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace CodeFin\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Prettus\Repository\Contracts\Transformable;
7+
use Prettus\Repository\Traits\TransformableTrait;
8+
9+
class Bank extends Model implements Transformable
10+
{
11+
use TransformableTrait;
12+
13+
protected $fillable = [
14+
'name',
15+
'logo'
16+
];
17+
18+
}

app/User.php app/Models/User.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace CodeFin;
3+
namespace CodeFin\Models;
44

55
use Illuminate\Notifications\Notifiable;
66
use Illuminate\Foundation\Auth\User as Authenticatable;

app/Providers/AuthServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace CodeFin\Providers;
44

5-
use CodeFin\User;
5+
use CodeFin\Models\User;
66
use Illuminate\Support\Facades\Gate;
77
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CodeFin\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class RepositoryServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Register the application services.
21+
*
22+
* @return void
23+
*/
24+
public function register()
25+
{
26+
$this->app->bind(\CodeFin\Repositories\Interfaces\BankRepository::class, \CodeFin\Repositories\BankRepositoryEloquent::class);
27+
//:end-bindings:
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace CodeFin\Repositories;
4+
5+
use Prettus\Repository\Eloquent\BaseRepository;
6+
use Prettus\Repository\Criteria\RequestCriteria;
7+
use CodeFin\Repositories\Interfaces\BankRepository;
8+
use CodeFin\Models\Bank;
9+
use CodeFin\Validators\BankValidator;
10+
11+
/**
12+
* Class BankRepositoryEloquent
13+
* @package namespace CodeFin\Repositories;
14+
*/
15+
class BankRepositoryEloquent extends BaseRepository implements BankRepository
16+
{
17+
/**
18+
* Specify Model class name
19+
*
20+
* @return string
21+
*/
22+
public function model()
23+
{
24+
return Bank::class;
25+
}
26+
27+
28+
29+
/**
30+
* Boot up the repository, pushing criteria
31+
*/
32+
public function boot()
33+
{
34+
$this->pushCriteria(app(RequestCriteria::class));
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace CodeFin\Repositories\Interfaces;
4+
5+
use Prettus\Repository\Contracts\RepositoryInterface;
6+
7+
/**
8+
* Interface BankRepository
9+
* @package namespace CodeFin\Repositories\Interfaces;
10+
*/
11+
interface BankRepository extends RepositoryInterface
12+
{
13+
//
14+
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": ">=5.6.4",
99
"laravel/framework": "5.3.18",
1010
"tymon/jwt-auth": "dev-develop",
11-
"barryvdh/laravel-cors": "^0.8.5"
11+
"barryvdh/laravel-cors": "^0.8.5",
12+
"prettus/l5-repository": "~2.6.6"
1213
},
1314
"require-dev": {
1415
"fzaninotto/faker": "~1.4",

0 commit comments

Comments
 (0)