Skip to content

Commit 6e32ae6

Browse files
committed
Creted all the Resources REST API end points and the crud operation functionality. Implemented the api validation check functionality to access the resources. Implemented the PHP Unit test cases to test all the API endpoints.
1 parent 160e64d commit 6e32ae6

37 files changed

+1855
-53
lines changed

app/Exceptions/Handler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function report(Exception $exception)
4646
*/
4747
public function render($request, Exception $exception)
4848
{
49+
50+
// Custom responce for 404 - resource not found
51+
if ($exception instanceof ModelNotFoundException && $request->wantsJson() ) {
52+
return response()->json([
53+
'error' => 'Resource not found!'
54+
], 404);
55+
}
56+
4957
return parent::render($request, $exception);
5058
}
5159
}

app/Field.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Field extends Model
8+
{
9+
/**
10+
* The attributes that are mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $fillable = ['title', 'type'];
15+
16+
/**
17+
* Get the SubscriberField record associated with the Field.
18+
*/
19+
public function subscriberfield()
20+
{
21+
return $this->hasMany('App\SubscriberField', 'field_id');
22+
}
23+
}

app/Http/Controllers/Auth/LoginController.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use App\Http\Controllers\Controller;
66
use Illuminate\Foundation\Auth\AuthenticatesUsers;
7+
use Illuminate\Http\Request;
8+
use Auth;
79

810
class LoginController extends Controller
911
{
@@ -36,4 +38,37 @@ public function __construct()
3638
{
3739
$this->middleware('guest')->except('logout');
3840
}
41+
42+
public function login(Request $request)
43+
{
44+
$this->validateLogin($request);
45+
46+
if ($this->attemptLogin($request)) {
47+
$user = $this->guard()->user();
48+
$user->generateToken();
49+
50+
return response()->json([
51+
'data' => $user->toArray(),
52+
]);
53+
}
54+
55+
return $this->sendFailedLoginResponse($request);
56+
}
57+
58+
public function logout(Request $request)
59+
{
60+
$user = Auth::guard('api')->user();
61+
62+
if ($user) {
63+
$user->api_token = null;
64+
$user->save();
65+
}
66+
67+
return response()->json(['data' => 'User logged out.'], 200);
68+
}
69+
70+
protected function unauthenticated($request, AuthenticationException $exception)
71+
{
72+
return response()->json(['error' => 'Unauthenticated'], 401);
73+
}
3974
}

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Hash;
88
use Illuminate\Support\Facades\Validator;
99
use Illuminate\Foundation\Auth\RegistersUsers;
10+
use Illuminate\Http\Request;
1011

1112
class RegisterController extends Controller
1213
{
@@ -69,4 +70,11 @@ protected function create(array $data)
6970
'password' => Hash::make($data['password']),
7071
]);
7172
}
73+
74+
protected function registered(Request $request, $user)
75+
{
76+
$user->generateToken();
77+
78+
return response()->json(['data' => $user->toArray()], 201);
79+
}
7280
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Field;
7+
8+
/**
9+
* Class FieldController.
10+
* @desc Field Api end point CRUD operation
11+
*/
12+
class FieldController extends Controller
13+
{
14+
/**
15+
* List all the resources for Field
16+
*
17+
* @param null
18+
*
19+
* @return mix
20+
*/
21+
public function index()
22+
{
23+
return Field::all();
24+
}
25+
26+
/**
27+
* List particluar resource of Field
28+
*
29+
* @param Obj Field
30+
*
31+
* @return mix
32+
*/
33+
public function show(Field $field)
34+
{
35+
return $field;
36+
}
37+
38+
/**
39+
* Store data for a particluar resource of Field
40+
*
41+
* @param Obj Request
42+
*
43+
* @return json
44+
*/
45+
public function store(Request $request)
46+
{
47+
$field = Field::create($request->all());
48+
49+
return response()->json($field, 201);
50+
}
51+
52+
/**
53+
* Upadte data for a particluar resource of Field
54+
*
55+
* @param Obj Request
56+
* @param Obj Field
57+
*
58+
* @return json
59+
*/
60+
public function update(Request $request, Field $field)
61+
{
62+
$field->update($request->all());
63+
64+
return response()->json($field, 200);
65+
}
66+
67+
/**
68+
* Delete a particluar resource of Field
69+
*
70+
* @param Obj Field
71+
*
72+
* @return json
73+
*/
74+
public function delete(Field $field)
75+
{
76+
$field->delete();
77+
78+
return response()->json(null, 204);
79+
}
80+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Subscriber;
7+
use EmailValidator;
8+
9+
/**
10+
* Class SubscriberController.
11+
* @desc Subscriber Api end point CRUD operation
12+
*/
13+
class SubscriberController extends Controller
14+
{
15+
/**
16+
* List all the resources for Subscriber
17+
*
18+
* @param null
19+
*
20+
* @return mix
21+
*/
22+
public function index()
23+
{
24+
return Subscriber::all();
25+
}
26+
27+
/**
28+
* List particluar resource of Subscriber
29+
*
30+
* @param Obj Subscriber
31+
*
32+
* @return mix
33+
*/
34+
public function show(Subscriber $subscriber)
35+
{
36+
return $subscriber;
37+
}
38+
39+
/**
40+
* Store data for a particluar resource of Subscriber
41+
*
42+
* @param Obj Request
43+
*
44+
* @return json
45+
*/
46+
public function store(Request $request)
47+
{
48+
//Check for valid Email address
49+
if ($request->input('email_address')) {
50+
if( !EmailValidator::verify($request->input('email_address'))->isValid()[0]){
51+
return response()->json(['email' => 'Email address is not valid.'], 422);
52+
}
53+
} else {
54+
return response()->json(['email' => 'The email field is required!'], 422);
55+
}
56+
57+
58+
$subscriber = Subscriber::create($request->all());
59+
60+
return response()->json($subscriber, 201);
61+
}
62+
63+
/**
64+
* Upadte data for a particluar resource of Subscriber
65+
*
66+
* @param Obj Request
67+
* @param Obj Subscriber
68+
*
69+
* @return json
70+
*/
71+
public function update(Request $request, Subscriber $subscriber)
72+
{
73+
//Check for valid Email address
74+
if ($request->input('email_address')) {
75+
if( !EmailValidator::verify($request->input('email_address'))->isValid()[0]){
76+
return response()->json(['email' => 'Email address is not valid.'], 422);
77+
}
78+
} else {
79+
return response()->json(['email' => 'The email field is required!'], 422);
80+
}
81+
82+
$subscriber->update($request->all());
83+
84+
return response()->json($subscriber, 200);
85+
}
86+
87+
/**
88+
* Delete a particluar resource of Subscriber
89+
*
90+
* @param Obj Subscriber
91+
*
92+
* @return json
93+
*/
94+
public function delete(Subscriber $subscriber)
95+
{
96+
$subscriber->delete();
97+
98+
return response()->json(null, 204);
99+
}
100+
}

app/Http/Middleware/Authenticate.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ class Authenticate extends Middleware
1414
*/
1515
protected function redirectTo($request)
1616
{
17-
if (! $request->expectsJson()) {
17+
if ($request->expectsJson()) {
18+
return json_encode([
19+
'error' => 'invalid api token.'
20+
], 404); // Status code here
21+
} else {
1822
return route('login');
1923
}
2024
}

app/Subscriber.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Subscriber extends Model
8+
{
9+
/**
10+
* The attributes that are mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $fillable = ['email_address', 'name', 'state'];
15+
16+
/**
17+
* Get the SubscriberField record associated with the Subscriber.
18+
*/
19+
public function subscriberfield()
20+
{
21+
return $this->hasMany('App\SubscriberField', 'subscriber_id');
22+
}
23+
24+
25+
}

app/SubscriberField.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class SubscriberField extends Model
8+
{
9+
/**
10+
* The attributes that are mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $fillable = ['subscriber_id', 'field_id'];
15+
16+
/**
17+
* Get the SubscriberField record associated with the Subscriber.
18+
*/
19+
public function subscriber()
20+
{
21+
return $this->belongsTo('App\Subscriber', 'subscriber_id');
22+
}
23+
24+
/**
25+
* Get the SubscriberField record associated with the Field.
26+
*/
27+
public function field()
28+
{
29+
return $this->belongsTo('App\Field', 'field_id');
30+
}
31+
}

app/User.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ class User extends Authenticatable
2727
protected $hidden = [
2828
'password', 'remember_token',
2929
];
30+
31+
32+
public function generateToken()
33+
{
34+
$this->api_token = str_random(50);
35+
$this->save();
36+
37+
return $this->api_token;
38+
}
3039
}

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
"type": "project",
77
"require": {
88
"php": "^7.1.3",
9+
"doctrine/dbal": "^2.8",
910
"fideloper/proxy": "^4.0",
1011
"laravel/framework": "5.7.*",
11-
"laravel/tinker": "^1.0"
12+
"laravel/tinker": "^1.0",
13+
"unicodeveloper/laravel-email-validator": "^1.0"
1214
},
1315
"require-dev": {
1416
"beyondcode/laravel-dump-server": "^1.0",
@@ -48,6 +50,9 @@
4850
"post-autoload-dump": [
4951
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
5052
"@php artisan package:discover --ansi"
53+
],
54+
"test" : [
55+
"vendor/bin/phpunit"
5156
]
5257
},
5358
"config": {

0 commit comments

Comments
 (0)