Skip to content

Commit c33af47

Browse files
author
Fatnan Ahmad Thawsan
committed
add address crud REST API
1 parent b567dc3 commit c33af47

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\API;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
use Validator;
8+
use App\Models\Address;
9+
10+
class AddressController extends Controller
11+
{
12+
public function __construct(Address $address)
13+
{
14+
$this->address = $address;
15+
}
16+
/**
17+
* Display a listing of the resource.
18+
*
19+
* @return \Illuminate\Http\Response
20+
*/
21+
public function index()
22+
{
23+
$data = $this->address->latest()->get();
24+
return response()->json([$data]);
25+
}
26+
27+
/**
28+
* Store a newly created resource in storage.
29+
*
30+
* @param \Illuminate\Http\Request $request
31+
* @return \Illuminate\Http\Response
32+
*/
33+
public function store(Request $request)
34+
{
35+
$validator = Validator::make($request->all(),[
36+
'address' => 'required|string|max:255',
37+
'city' => 'required|string',
38+
'postal_code' => 'required|string'
39+
]);
40+
41+
if($validator->fails()){
42+
return response()->json($validator->errors());
43+
}
44+
45+
try{
46+
$address = $this->address->create([
47+
'address' => $request->address,
48+
'city' => $request->city,
49+
'postal_code' => $request->postal_code
50+
]);
51+
52+
return response()->json(['Address created successfully.',$address]);
53+
} catch (\Throwable $th) {
54+
return response()->json($th->getMessage(), 500);
55+
}
56+
57+
58+
}
59+
60+
/**
61+
* Display the specified resource.
62+
*
63+
* @param int $id
64+
* @return \Illuminate\Http\Response
65+
*/
66+
public function show($id)
67+
{
68+
$address = Address::find($id);
69+
if (is_null($address)) {
70+
return response()->json('Data not found', 404);
71+
}
72+
return response()->json([$address]);
73+
}
74+
75+
/**
76+
* Update the specified resource in storage.
77+
*
78+
* @param \Illuminate\Http\Request $request
79+
* @param int $id
80+
* @return \Illuminate\Http\Response
81+
*/
82+
public function update(Request $request, Address $address)
83+
{
84+
$validator = Validator::make($request->all(),[
85+
'address' => 'required|string|max:255',
86+
'city' => 'required|string',
87+
'postal_code' => 'required|string'
88+
]);
89+
90+
if($validator->fails()){
91+
return response()->json($validator->errors());
92+
}
93+
94+
try{
95+
$data_address =[
96+
'address' => $request->address,
97+
'city' => $request->city,
98+
'postal_code' => $request->postal_code
99+
];
100+
101+
$address = $this->address->updateOrCreate(
102+
[
103+
'id' => $address->id
104+
],
105+
$data_address
106+
);
107+
108+
return response()->json(['Address updated successfully.', $address]);
109+
} catch (\Throwable $th) {
110+
return response()->json($th->getMessage(), 500);
111+
}
112+
}
113+
114+
/**
115+
* Remove the specified resource from storage.
116+
*
117+
* @param int $id
118+
* @return \Illuminate\Http\Response
119+
*/
120+
public function destroy($id)
121+
{
122+
$address = $this->address->find($id);
123+
if($address){
124+
$address->delete();
125+
return response()->json('Address deleted successfully');
126+
} else {
127+
return response()->json('Address does not exist');
128+
}
129+
}
130+
}

app/Models/Address.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Address extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = [
13+
'address',
14+
'city',
15+
'postal_code',
16+
];
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateAddressesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('addresses', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('address');
19+
$table->string('city');
20+
$table->string('postal_code');
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::dropIfExists('addresses');
33+
}
34+
}

routes/api.php

+3
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@
2727

2828
// API route for logout user
2929
Route::post('/logout', [App\Http\Controllers\API\AuthController::class, 'logout']);
30+
31+
// Address route
32+
Route::resource('address', App\Http\Controllers\API\AddressController::class);
3033
});

0 commit comments

Comments
 (0)