Skip to content

Commit

Permalink
list api #3 #11 #12 #8 #14
Browse files Browse the repository at this point in the history
  • Loading branch information
codewizard0803 committed Feb 4, 2019
1 parent 2f4f517 commit eba4fc2
Show file tree
Hide file tree
Showing 17 changed files with 726 additions and 226 deletions.
11 changes: 11 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
APP_ENV=testing
APP_DEBUG=true
APP_KEY=base64:AJ/ZQwE0Mr8+LWlZALhhlHkE8SeHz4OgdqrbzENI0e4=

DB_CONNECTION=sqlite
DB_DATABASE=test.sqlite

BROADCAST_DRIVER=log
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
5 changes: 4 additions & 1 deletion app/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@

class City extends Model
{
//
public function scopeCountry($query, $value)
{
return $query->where('country', $value);
}
}
38 changes: 38 additions & 0 deletions app/Http/Controllers/ListController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Controllers;

use App\ListModel;
use Illuminate\Http\Request;
use Validator;

class ListController extends Controller
{
public function index(Request $request, \App\Search\ListSearch $listSearch)
{
return response()->json($listSearch->search($request));
}

public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|max:191',
// TODO: not sure if max int can be save in db
'price' => 'required|integer|digits_between:0,' . PHP_INT_MAX . '',
'isOnSale' => 'required|boolean',
'city_id' => 'required|exists:cities,id',
'developer_id' => 'required|exists:developers,id',
]);

if ($validator->fails()) {
return response()->json($validator->errors());
}

return response()->json(ListModel::create($request->all()));
}

public function show($id)
{
return response()->json(ListModel::findOrFail($id));
}
}
11 changes: 11 additions & 0 deletions app/ListModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class ListModel extends Model
{
protected $table = 'lists';
protected $fillable = ['name', 'price', 'isOnSale', 'city_id', 'developer_id'];

public function developer()
{
Expand All @@ -17,4 +18,14 @@ public function city()
{
return $this->belongsTo(City::class);
}

public function scopeIsOnSale($query, $value)
{
return $query->where('isOnSale', $value);
}

public function scopePriceLessThan($query, $value)
{
return $query->where('price', '<', $value);
}
}
31 changes: 31 additions & 0 deletions app/Providers/ListSearchServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Providers;

use App\Search\ListSearch;
use Illuminate\Support\ServiceProvider;

class ListSearchServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('App\Search\ListSearch', function ($app) {
return new ListSearch();
});
}
}
47 changes: 47 additions & 0 deletions app/Search/ListSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace App\Search;

use App\ListModel;
use Illuminate\Http\Request;

class ListSearch
{
private $lists;

public function __construct()
{
$this->lists = ListModel::query()->with('city');
}

public function search(Request $request)
{
if ($request->has('isOnSale')) {
$this->handleIsOnSale($request->input('isOnSale'));
}
if ($request->has('country')) {
$this->handleCountry($request->input('country'));
}
if ($request->has('price_less_than')) {
$this->handlePriceLessThan($request->input('price_less_than'));
}

return $this->lists->get();
}

private function handleIsOnSale($isOnSale)
{
$this->lists->isOnSale($isOnSale);
}

private function handleCountry($country)
{
$this->lists->whereHas('city', function($query) use ($country) {
$query->where('country', $country);
});
}

private function handlePriceLessThan($price)
{
$this->lists->priceLessThan($price);
}
}
Empty file modified bootstrap/cache/.gitignore
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"require-dev": {
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.7"
"phpunit/phpunit": "~6.4"
},
"autoload": {
"classmap": [
Expand Down
Loading

0 comments on commit eba4fc2

Please sign in to comment.