-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadController.php
138 lines (96 loc) · 3.25 KB
/
ReadController.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
<?php
namespace Kaleidoscope\Factotum\Http\Controllers\Api\Product;
use Illuminate\Http\Request;
use Kaleidoscope\Factotum\Content;
use Kaleidoscope\Factotum\Product;
use Kaleidoscope\Factotum\ProductCategory;
class ReadController extends Controller
{
public function getListPaginated( Request $request )
{
$limit = $request->input('limit');
$offset = $request->input('offset');
$sort = $request->input('sort');
$direction = $request->input('direction');
$filters = $request->input('filters', null);
if ( !$sort ) {
$sort = 'id';
}
if ( !$direction ) {
$direction = 'DESC';
}
$query = Product::with([ 'brand', 'product_category' ]);
if ( isset($filters) && count($filters) > 0 ) {
if ( isset($filters['term']) && strlen($filters['term']) > 0 ) {
$query->whereRaw( 'LCASE(name) like "%' . $filters['term'] . '%"' );
$query->orWhereRaw( 'LCASE(code) like "%' . $filters['term'] . '%"' );
}
if ( isset($filters['product_category_id']) && $filters['product_category_id'] ) {
$productCategory = ProductCategory::find( $filters['product_category_id'] );
$productCategories = $productCategory->getFlatChildsArray();
$tmp = [ $productCategory->id ];
if ( count($productCategories) > 0 ) {
foreach ( $productCategories as $c ) {
$tmp[] = $c->id;
}
}
$tmp = array_unique( $tmp );
$query->whereIn( 'product_category_id', $tmp );
}
if ( isset($filters['brand_id']) && $filters['brand_id'] ) {
$query->whereIn('brand_id', $filters['brand_id']);
}
if ( isset($filters['active']) && $filters['active'] ) {
$query->where('active', true );
}
if ( isset($filters['featured']) && $filters['featured'] ) {
$query->where('featured', true );
}
}
$total = $query->count();
$query->orderBy($sort, $direction);
if ( $limit ) {
$query->take($limit);
}
if ( $offset ) {
$query->skip($offset);
}
$products = $query->get();
return response()->json( [ 'result' => 'ok', 'products' => $products, 'total' => $total ]);
}
public function getList( Request $request )
{
$products = Product::orderBy('id', 'DESC')->get();
return response()->json( [ 'result' => 'ok', 'products' => $products ]);
}
public function getListBySearch( Request $request )
{
$search = $request->input('search');
$query = Product::where( 'name', 'like', '%' . $search . '%' )
->orWhere( 'code', 'like', '%' . $search . '%' );
$productList = $query->get();
return response()->json( [
'result' => 'ok',
'products' => $productList
]);
}
public function getDetail(Request $request, $id)
{
$product = Product::find($id);
if ( $product ) {
$product->load([ 'brand', 'product_category', 'tax', 'product_variants' ]);
return response()->json( [ 'result' => 'ok', 'product' => $product ]);
}
return $this->_sendJsonError( 'Product not found', 404 );
}
public function getNumberByStatus( Request $request )
{
$totalActive = Product::where('active', 1)->count();
$totalNotActive = Product::whereNull('active')->orWhere('active', 0)->count();
return response()->json( [
'result' => 'ok',
'active' => $totalActive,
'not_active' => $totalNotActive
]);
}
}