-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathProductRepository.php
149 lines (131 loc) · 3.65 KB
/
ProductRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace App\Repositories;
use Illuminate\Support\Str;
use App\Helpers\UploadHelper;
use App\Interfaces\CrudInterface;
use App\Models\Product;
use App\Models\User;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Support\Facades\Auth;
class ProductRepository implements CrudInterface
{
/**
* Authenticated User Instance.
*
* @var User
*/
public User | null $user;
/**
* Constructor.
*/
public function __construct()
{
$this->user = Auth::guard()->user();
}
/**
* Get All Products.
*
* @return collections Array of Product Collection
*/
public function getAll(): Paginator
{
return $this->user->products()
->orderBy('id', 'desc')
->with('user')
->paginate(10);
}
/**
* Get Paginated Product Data.
*
* @param int $pageNo
* @return collections Array of Product Collection
*/
public function getPaginatedData($perPage): Paginator
{
$perPage = isset($perPage) ? intval($perPage) : 12;
return Product::orderBy('id', 'desc')
->with('user')
->paginate($perPage);
}
/**
* Get Searchable Product Data with Pagination.
*
* @param int $pageNo
* @return collections Array of Product Collection
*/
public function searchProduct($keyword, $perPage): Paginator
{
$perPage = isset($perPage) ? intval($perPage) : 10;
return Product::where('title', 'like', '%' . $keyword . '%')
->orWhere('description', 'like', '%' . $keyword . '%')
->orWhere('price', 'like', '%' . $keyword . '%')
->orderBy('id', 'desc')
->with('user')
->paginate($perPage);
}
/**
* Create New Product.
*
* @param array $data
* @return object Product Object
*/
public function create(array $data): Product
{
$titleShort = Str::slug(substr($data['title'], 0, 20));
$data['user_id'] = $this->user->id;
if (!empty($data['image'])) {
$data['image'] = UploadHelper::upload('image', $data['image'], $titleShort . '-' . time(), 'images/products');
}
return Product::create($data);
}
/**
* Delete Product.
*
* @param int $id
* @return boolean true if deleted otherwise false
*/
public function delete(int $id): bool
{
$product = Product::find($id);
if (empty($product)) {
return false;
}
UploadHelper::deleteFile('images/products/' . $product->image);
$product->delete($product);
return true;
}
/**
* Get Product Detail By ID.
*
* @param int $id
* @return void
*/
public function getByID(int $id): Product|null
{
return Product::with('user')->find($id);
}
/**
* Update Product By ID.
*
* @param int $id
* @param array $data
* @return object Updated Product Object
*/
public function update(int $id, array $data): Product|null
{
$product = Product::find($id);
if (!empty($data['image'])) {
$titleShort = Str::slug(substr($data['title'], 0, 20));
$data['image'] = UploadHelper::update('image', $data['image'], $titleShort . '-' . time(), 'images/products', $product->image);
} else {
$data['image'] = $product->image;
}
if (is_null($product)) {
return null;
}
// If everything is OK, then update.
$product->update($data);
// Finally return the updated product.
return $this->getByID($product->id);
}
}