Skip to content

Commit eba4fc2

Browse files
list api #3 #11 #12 #8 #14
1 parent 2f4f517 commit eba4fc2

File tree

17 files changed

+726
-226
lines changed

17 files changed

+726
-226
lines changed

.env.testing

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
APP_ENV=testing
2+
APP_DEBUG=true
3+
APP_KEY=base64:AJ/ZQwE0Mr8+LWlZALhhlHkE8SeHz4OgdqrbzENI0e4=
4+
5+
DB_CONNECTION=sqlite
6+
DB_DATABASE=test.sqlite
7+
8+
BROADCAST_DRIVER=log
9+
CACHE_DRIVER=array
10+
SESSION_DRIVER=array
11+
QUEUE_DRIVER=sync

app/City.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66

77
class City extends Model
88
{
9-
//
9+
public function scopeCountry($query, $value)
10+
{
11+
return $query->where('country', $value);
12+
}
1013
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\ListModel;
6+
use Illuminate\Http\Request;
7+
use Validator;
8+
9+
class ListController extends Controller
10+
{
11+
public function index(Request $request, \App\Search\ListSearch $listSearch)
12+
{
13+
return response()->json($listSearch->search($request));
14+
}
15+
16+
public function store(Request $request)
17+
{
18+
$validator = Validator::make($request->all(), [
19+
'name' => 'required|max:191',
20+
// TODO: not sure if max int can be save in db
21+
'price' => 'required|integer|digits_between:0,' . PHP_INT_MAX . '',
22+
'isOnSale' => 'required|boolean',
23+
'city_id' => 'required|exists:cities,id',
24+
'developer_id' => 'required|exists:developers,id',
25+
]);
26+
27+
if ($validator->fails()) {
28+
return response()->json($validator->errors());
29+
}
30+
31+
return response()->json(ListModel::create($request->all()));
32+
}
33+
34+
public function show($id)
35+
{
36+
return response()->json(ListModel::findOrFail($id));
37+
}
38+
}

app/ListModel.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class ListModel extends Model
88
{
99
protected $table = 'lists';
10+
protected $fillable = ['name', 'price', 'isOnSale', 'city_id', 'developer_id'];
1011

1112
public function developer()
1213
{
@@ -17,4 +18,14 @@ public function city()
1718
{
1819
return $this->belongsTo(City::class);
1920
}
21+
22+
public function scopeIsOnSale($query, $value)
23+
{
24+
return $query->where('isOnSale', $value);
25+
}
26+
27+
public function scopePriceLessThan($query, $value)
28+
{
29+
return $query->where('price', '<', $value);
30+
}
2031
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use App\Search\ListSearch;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class ListSearchServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap the application services.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Register the application services.
22+
*
23+
* @return void
24+
*/
25+
public function register()
26+
{
27+
$this->app->bind('App\Search\ListSearch', function ($app) {
28+
return new ListSearch();
29+
});
30+
}
31+
}

app/Search/ListSearch.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace App\Search;
3+
4+
use App\ListModel;
5+
use Illuminate\Http\Request;
6+
7+
class ListSearch
8+
{
9+
private $lists;
10+
11+
public function __construct()
12+
{
13+
$this->lists = ListModel::query()->with('city');
14+
}
15+
16+
public function search(Request $request)
17+
{
18+
if ($request->has('isOnSale')) {
19+
$this->handleIsOnSale($request->input('isOnSale'));
20+
}
21+
if ($request->has('country')) {
22+
$this->handleCountry($request->input('country'));
23+
}
24+
if ($request->has('price_less_than')) {
25+
$this->handlePriceLessThan($request->input('price_less_than'));
26+
}
27+
28+
return $this->lists->get();
29+
}
30+
31+
private function handleIsOnSale($isOnSale)
32+
{
33+
$this->lists->isOnSale($isOnSale);
34+
}
35+
36+
private function handleCountry($country)
37+
{
38+
$this->lists->whereHas('city', function($query) use ($country) {
39+
$query->where('country', $country);
40+
});
41+
}
42+
43+
private function handlePriceLessThan($price)
44+
{
45+
$this->lists->priceLessThan($price);
46+
}
47+
}

bootstrap/cache/.gitignore

100644100755
File mode changed.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"require-dev": {
1414
"mockery/mockery": "0.9.*",
15-
"phpunit/phpunit": "~5.7"
15+
"phpunit/phpunit": "~6.4"
1616
},
1717
"autoload": {
1818
"classmap": [

0 commit comments

Comments
 (0)