Skip to content

Commit a847ea0

Browse files
author
Michael Dahlke
committed
refactor all routing and how listings are saved/created
1 parent b979ea8 commit a847ea0

29 files changed

+1190
-437
lines changed

app/GraphQL/Queries/ListingsQuery.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closur
119119
->whereHas('activeDate')
120120
->orderBy('sort');
121121

122-
// dd(sql_with_bindings($builder));
123-
124122
$page = ($args['page'] ?? 1);
125123

126124
if (($args['limit'] ?? 0) > 0) {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
namespace App\GraphQL\Query;
4+
5+
use App\GraphQL\Types\ListingsType;
6+
use App\Listing;
7+
use Closure;
8+
use GraphQL\Type\Definition\ResolveInfo;
9+
use GraphQL\Type\Definition\Type;
10+
use Illuminate\Database\Eloquent\Builder;
11+
use Illuminate\Support\Facades\Auth;
12+
use Rebing\GraphQL\Support\Facades\GraphQL;
13+
use Rebing\GraphQL\Support\Query;
14+
15+
class UserListingsQuery extends Query {
16+
protected $attributes = [
17+
'name' => 'User listings Query',
18+
'description' => 'A query of a user\'s listings',
19+
];
20+
21+
public function authorize($root, array $args, $ctx, ResolveInfo $resolveInfo = null, Closure $getSelectFields = null): bool {
22+
// true, if logged in
23+
return true;
24+
return !Auth::guest();
25+
}
26+
27+
/**
28+
* @return Type
29+
*/
30+
public function type(): Type {
31+
// return Type::string();
32+
// result of query with pagination laravel
33+
return GraphQL::type('listings');
34+
}
35+
36+
/**
37+
* @return array
38+
*/
39+
public function args(): array {
40+
return [
41+
'id' => [
42+
'name' => 'id',
43+
'type' => Type::string(),
44+
],
45+
// 'limit' => [
46+
// 'name' => 'limit',
47+
// 'type' => Type::int(),
48+
// 'default' => 1,
49+
// ],
50+
// 'page' => [
51+
// 'name' => 'page',
52+
// 'type' => Type::int(),
53+
// 'default' => 1
54+
// ]
55+
];
56+
}
57+
58+
protected function rules(array $args = []): array {
59+
return [
60+
// 'limit' => ['int'],
61+
// 'page' => ['int']
62+
];
63+
}
64+
65+
public function validationErrorMessages(array $args = []): array {
66+
return [
67+
'id.required' => 'The `id` is required',
68+
// 'limit.int' => 'The `limit` argument must be an `int`',
69+
// 'page.int' => 'The `page` argument must be an `int`',
70+
];
71+
}
72+
73+
/**
74+
* @param $root
75+
* @param $args
76+
* @param $context
77+
* @param ResolveInfo $resolveInfo
78+
* @param Closure $fields
79+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
80+
*/
81+
public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $fields) {
82+
$where = function ($query) use ($args) {
83+
/** @var Builder $query */
84+
85+
if (isset($args['id'])) {
86+
$query->where('id', $args['id']);
87+
}
88+
if (isset($args['bounds'])) {
89+
$bounds = json_decode($args['bounds']);
90+
$sw = $bounds->sw;
91+
$ne = $bounds->ne;
92+
$query->where(function ($q) use ($sw, $ne) {
93+
$q->whereBetween('longitude', [$sw->lng, $ne->lng])
94+
->whereBetween('latitude', [$sw->lat, $ne->lat]);
95+
});
96+
}
97+
};
98+
99+
$select = $fields()->getSelect();
100+
101+
$builder = Listing::query()
102+
->select($select)
103+
// this allows ONLY still alive listings to appear.
104+
// @TODO ask Mark for the correct way of doing this
105+
->selectRaw('(SELECT `start` FROM listing_dates WHERE listings.id = listing_dates.listing_id ORDER BY `start` LIMIT 1 ) as sort')
106+
->with(array_keys($fields()->getRelations()))
107+
->where($where)
108+
->orderBy('sort');
109+
110+
111+
112+
// $page = ($args['page'] ?? 1);
113+
114+
// if (($args['limit'] ?? 0) > 0) {
115+
// $limit = $args['limit'];
116+
// } else {
117+
// // set the limit high to get all listings
118+
// $limit = (int)99999999;
119+
// // set the page to zero so we get listings
120+
// $page = 0;
121+
// }
122+
123+
return $builder->first();
124+
}
125+
}

app/GraphQL/Types/ListingsType.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ public function fields(): array {
3232
'type' => Type::string(),
3333
'description' => 'The address where the listing is located',
3434
],
35+
'house_number' => [
36+
'type' => Type::string(),
37+
'description' => 'The house number where the listing is located',
38+
],
39+
'street_name' => [
40+
'type' => Type::string(),
41+
'description' => 'The name of the street where the listing is located',
42+
],
43+
'city' => [
44+
'type' => Type::string(),
45+
'description' => 'The city where the listing is located',
46+
],
47+
'state' => [
48+
'type' => Type::string(),
49+
'description' => 'The state where the listing is located',
50+
],
51+
'postcode' => [
52+
'type' => Type::string(),
53+
'description' => 'The postcode where the listing is located',
54+
],
55+
'country_code' => [
56+
'type' => Type::string(),
57+
'description' => 'The country code where the listing is located (IE: US)',
58+
],
3559
'latitude' => [
3660
'type' => Type::float(),
3761
'description' => 'The latitude coordinates of the listing',

app/Http/Controllers/AuthController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ public function login(Request $request) {
7070
$tokenResult = $user->createToken('Personal Access Token');
7171
$token = $tokenResult->token;
7272

73-
if ($request->remember_me) {
74-
$token->expires_at = Carbon::now()->addWeeks(1);
75-
}
73+
$token->expires_at = Carbon::now()->addWeeks(1);
74+
7675

7776
$token->save();
7877

app/Http/Controllers/Listing.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class Listing extends Controller
8+
{
9+
/**
10+
* Display a listing of the resource.
11+
*
12+
* @return \Illuminate\Http\Response
13+
*/
14+
public function index()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Show the form for creating a new resource.
21+
*
22+
* @return \Illuminate\Http\Response
23+
*/
24+
public function create()
25+
{
26+
//
27+
}
28+
29+
/**
30+
* Store a newly created resource in storage.
31+
*
32+
* @param \Illuminate\Http\Request $request
33+
* @return \Illuminate\Http\Response
34+
*/
35+
public function store(Request $request)
36+
{
37+
//
38+
}
39+
40+
/**
41+
* Display the specified resource.
42+
*
43+
* @param int $id
44+
* @return \Illuminate\Http\Response
45+
*/
46+
public function show($id)
47+
{
48+
//
49+
}
50+
51+
/**
52+
* Show the form for editing the specified resource.
53+
*
54+
* @param int $id
55+
* @return \Illuminate\Http\Response
56+
*/
57+
public function edit($id)
58+
{
59+
//
60+
}
61+
62+
/**
63+
* Remove the specified resource from storage.
64+
*
65+
* @param int $id
66+
* @return \Illuminate\Http\Response
67+
*/
68+
public function destroy($id)
69+
{
70+
//
71+
}
72+
}

0 commit comments

Comments
 (0)