From 67520d08c722137db571ca368dfedb9196f650bb Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Tue, 29 Oct 2019 23:21:41 +0100 Subject: [PATCH 01/14] fix: fixed up some typo --- routes/v1.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/routes/v1.php b/routes/v1.php index 58da2b443..1496b64e9 100644 --- a/routes/v1.php +++ b/routes/v1.php @@ -71,7 +71,7 @@ // Show all visitor - Route::get('visitors/all', 'VisitorController@index')->middleware('superAdminadmin'); + Route::get('visitors/all', 'VisitorController@index')->middleware('superAdmin'); //create faq Route::post('faq', 'FaqController@store')->middleware('superAdmin'); @@ -126,16 +126,7 @@ //Show total number of pending service providers in the estate of logged in Estate Admin Route::get('statistics/pendingEstateService/', 'Statistics\ServiceStatsController@pendingEstateRequests')->middleware('estateAdmin'); - - - - }); - - - - - // General Users Routes ******************************************************* Route::group(['middleware' => ['jwt.verify']], function () { From b6e775fa151680bfbc02c09bec2195c1fae155d4 Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Tue, 29 Oct 2019 23:24:29 +0100 Subject: [PATCH 02/14] removed duplicated code --- routes/v1.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/routes/v1.php b/routes/v1.php index 1496b64e9..5459b37e2 100644 --- a/routes/v1.php +++ b/routes/v1.php @@ -86,8 +86,6 @@ //delete support message Route::delete('/support/{id}', 'SupportController@destroy')->middleware('superAdmin'); - Route::get('visitors/all', 'VisitorController@index')->middleware('superAdmin'); - // Show Total Number of Estates on the system Route::get('statistics/estate', 'Statistics\EstateStatsController@index')->middleware('superAdmin'); From d6688f8a4ddd5d83fed21df395d2b82e15b4f43e Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Tue, 29 Oct 2019 23:33:10 +0100 Subject: [PATCH 03/14] updated gateman controller --- app/Http/Controllers/GatemanController.php | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/app/Http/Controllers/GatemanController.php b/app/Http/Controllers/GatemanController.php index e12d01259..4c8b350ba 100644 --- a/app/Http/Controllers/GatemanController.php +++ b/app/Http/Controllers/GatemanController.php @@ -5,12 +5,17 @@ use App\Gateman; use App\Notifications\GatemanAcceptanceNotification; use App\User; +use App\Home; use App\Visitor; +use Exception; use App\Http\Resources\Visitor as VisitorResource; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Gate; +use Illuminate\Support\Facades\DB; +use \Illuminate\Database\QueryException; +use App\Http\Controllers\ImageController; use JWTAuth; class GatemanController extends Controller @@ -296,4 +301,60 @@ public function visitor_out(Request $request) return response()->json($res, 404); } } + + public function addEstateGateman( + $id, + Home $home, + User $new_user, + Request $request, + ImageController $image + ){ + // validate the posted data + $this->validate($request, [ + 'name' => ['required', 'regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/'], + 'phone' => ['required', 'string'], + ]); + + DB::beginTransaction(); + + try{ + // Create user + $new_user->name = $request->name; + $new_user->phone = $request->phone; + $new_user->role = 2; + $new_user->user_type = 'gateman'; + $new_user->save(); + + // Register gateman estate + $home->user_id = $new_user->id; + $home->estate_id = $id; + $home->save(); + + // transaction was successful + DB::commit(); + + $result = [ + 'name' => $new_user->name, + 'user_id' => $new_user->id, + 'phone' => $new_user->phone, + 'estate_id' => (int) $home->estate_id + ]; + + // send response + return response()->json([ + 'status' => true, + 'message' => 'The gateman was successfully added', + 'result' => $result + ], 200); + } catch(Exception $e) { + // transaction was not successful + DB::rollBack(); + + return response()->json([ + 'status' => false, + 'message' => 'Error, the gateman could not be added', + 'hint' => $e->getMessage() + ], 200); + } + } } From f8018d8b8a4fdaff2df3c0586ea0c5ccc8fbb1d4 Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Tue, 29 Oct 2019 23:33:45 +0100 Subject: [PATCH 04/14] fix: fixed up typos --- app/Http/Controllers/VisitorController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/VisitorController.php b/app/Http/Controllers/VisitorController.php index 1399c647f..1788805bc 100644 --- a/app/Http/Controllers/VisitorController.php +++ b/app/Http/Controllers/VisitorController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers; use App\Visitor; -use Exeception; +use Exception; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; @@ -169,7 +169,7 @@ public function store( 'image_info' => $data, 'qr_image_src'=> $qr_code ], 200); - }catch(Exeception $e) { + }catch(Exception $e) { //if any operation fails, Thanos snaps finger - user was not created rollback what is saved DB::rollBack(); $res['status'] = false; @@ -249,7 +249,7 @@ public function update( 'visitor' => $visitor, 'image_info' => $data ], 200); - }catch(Exeception $e) { + }catch(Exception $e) { //if any operation fails, rollback what is saved DB::rollBack(); $res['status'] = false; From 8021001bb491cc7427bbe3a62a43653c858a0777 Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Wed, 30 Oct 2019 17:30:31 +0100 Subject: [PATCH 05/14] added retrieval of gatemen by an estate admin --- app/Http/Controllers/GatemanController.php | 171 +++++++++++++++------ 1 file changed, 126 insertions(+), 45 deletions(-) diff --git a/app/Http/Controllers/GatemanController.php b/app/Http/Controllers/GatemanController.php index 4c8b350ba..5a3df4c4a 100644 --- a/app/Http/Controllers/GatemanController.php +++ b/app/Http/Controllers/GatemanController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Gateman; +use App\Estate; use App\Notifications\GatemanAcceptanceNotification; use App\User; use App\Home; @@ -302,59 +303,139 @@ public function visitor_out(Request $request) } } + /** + * Adds a gateman to an estate + * + * @return \Illuminate\Http\Response + */ public function addEstateGateman( $id, Home $home, User $new_user, - Request $request, - ImageController $image + Request $request ){ - // validate the posted data - $this->validate($request, [ - 'name' => ['required', 'regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/'], - 'phone' => ['required', 'string'], - ]); - - DB::beginTransaction(); - - try{ - // Create user - $new_user->name = $request->name; - $new_user->phone = $request->phone; - $new_user->role = 2; - $new_user->user_type = 'gateman'; - $new_user->save(); - - // Register gateman estate - $home->user_id = $new_user->id; - $home->estate_id = $id; - $home->save(); - - // transaction was successful - DB::commit(); - - $result = [ - 'name' => $new_user->name, - 'user_id' => $new_user->id, - 'phone' => $new_user->phone, - 'estate_id' => (int) $home->estate_id - ]; - - // send response + // Verifies that the user is assigned to that estate + $user_estate = Home::whereUserIdAndEstateId($this->user->id, $id)->first(); + + if (is_null($user_estate)) { return response()->json([ - 'status' => true, - 'message' => 'The gateman was successfully added', - 'result' => $result - ], 200); - } catch(Exception $e) { - // transaction was not successful - DB::rollBack(); + 'status' => false, + 'message'=> "Access denied", + ], 403); + } + else + { + // validate the posted data + $this->validate($request, [ + 'name' => ['required', 'regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/'], + 'phone' => ['required', 'string'], + ]); + + DB::beginTransaction(); + + try{ + // Create user + $new_user->name = $request->name; + $new_user->phone = $request->phone; + $new_user->role = 2; + $new_user->user_type = 'gateman'; + $new_user->save(); + + // Register gateman's estate + $home->user_id = $new_user->id; + $home->estate_id = $id; + $home->save(); + + // transaction was successful + DB::commit(); + + $result = [ + 'name' => $new_user->name, + 'phone' => $new_user->phone, + 'user_id' => $new_user->id, + 'home_id' => $home->id, + 'estate_id' => (int) $home->estate_id + ]; + + // send response + return response()->json([ + 'status' => true, + 'message' => 'The gateman was successfully added', + 'result' => $result + ], 200); + } catch(Exception $e) { + // transaction was not successful + DB::rollBack(); + + return response()->json([ + 'status' => false, + 'message' => 'Error, the gateman could not be added', + 'hint' => $e->getMessage() + ], 200); + } + } + } + /** + * Gets a single gateman or all gatemen details for an estate + * + * @return \Illuminate\Http\Response + */ + public function estateGatemen( + $estate_id, + $id = null, + Request $request + ){ + // Verifies that the user is assigned to that estate + $user_estate = Home::whereUserIdAndEstateId($this->user->id, $estate_id)->first(); + + if (is_null($user_estate)) { return response()->json([ - 'status' => false, - 'message' => 'Error, the gateman could not be added', - 'hint' => $e->getMessage() - ], 200); + 'status' => false, + 'message'=> "Access denied", + ], 403); + } + else { + // Check if requests is for a single gateman + if (is_null($id)) { + // Request is for all gatemen associated with the estate + // Get all gatemen users type associated with the estate + $user = User::join('homes', 'homes.user_id', 'users.id') + ->where('users.user_type', 'gateman') + ->where('homes.estate_id', $estate_id) + ->get(); + + return response()->json([ + 'count' => $user->count(), + 'status' => true, + 'gatemen' => $user, + ], 200); + } + else { + // Request is for a single gateman associated with the estate + // Get the gateman if only he is truly a gateman and is associated with the estate + $user = User::join('homes', 'homes.user_id', 'users.id') + ->where('users.id', $id) + ->where('homes.estate_id', $estate_id) + ->first([ + 'users.name', 'users.username', 'users.phone', + 'users.email', 'users.image', 'users.duty_time', + 'homes.id as home_id', 'users.id as user_id' + ]); + + if($user) { + return response()->json([ + 'status' => true, + 'gateman' => $user + ], 200); + } + else { + return response()->json([ + 'status' => false, + 'message' => "We cannot verify the user with id: {$id} as a gateman assigned to ". Estate::find($estate_id)->estate_name, + ], 404); + } + } } } } From f242f1726c43068bcfd11fb98eda4975117f96b7 Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Wed, 30 Oct 2019 17:37:05 +0100 Subject: [PATCH 06/14] fix: fixed up typos --- app/Http/Controllers/GatemanController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/GatemanController.php b/app/Http/Controllers/GatemanController.php index 5a3df4c4a..4f3d76019 100644 --- a/app/Http/Controllers/GatemanController.php +++ b/app/Http/Controllers/GatemanController.php @@ -314,7 +314,7 @@ public function addEstateGateman( User $new_user, Request $request ){ - // Verifies that the user is assigned to that estate + // Verifies that the logged-in user is assigned to the requested estate $user_estate = Home::whereUserIdAndEstateId($this->user->id, $id)->first(); if (is_null($user_estate)) { @@ -386,7 +386,7 @@ public function estateGatemen( $id = null, Request $request ){ - // Verifies that the user is assigned to that estate + // Verifies that the logged-in user is assigned to the requested estate $user_estate = Home::whereUserIdAndEstateId($this->user->id, $estate_id)->first(); if (is_null($user_estate)) { From 5df0033696a176634e498d7db78ce391d168cfed Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Wed, 30 Oct 2019 18:04:56 +0100 Subject: [PATCH 07/14] added updating of gateman details by an estate admin --- app/Http/Controllers/GatemanController.php | 83 ++++++++++++++++++++-- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/GatemanController.php b/app/Http/Controllers/GatemanController.php index 4f3d76019..f2fb08d7a 100644 --- a/app/Http/Controllers/GatemanController.php +++ b/app/Http/Controllers/GatemanController.php @@ -325,7 +325,7 @@ public function addEstateGateman( } else { - // validate the posted data + // Validate the posted data $this->validate($request, [ 'name' => ['required', 'regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/'], 'phone' => ['required', 'string'], @@ -395,12 +395,13 @@ public function estateGatemen( 'message'=> "Access denied", ], 403); } - else { + else + { // Check if requests is for a single gateman if (is_null($id)) { // Request is for all gatemen associated with the estate // Get all gatemen users type associated with the estate - $user = User::join('homes', 'homes.user_id', 'users.id') + $gatemen = User::join('homes', 'homes.user_id', 'users.id') ->where('users.user_type', 'gateman') ->where('homes.estate_id', $estate_id) ->get(); @@ -408,13 +409,14 @@ public function estateGatemen( return response()->json([ 'count' => $user->count(), 'status' => true, - 'gatemen' => $user, + 'gatemen' => $gatemen, ], 200); } - else { + else + { // Request is for a single gateman associated with the estate // Get the gateman if only he is truly a gateman and is associated with the estate - $user = User::join('homes', 'homes.user_id', 'users.id') + $gateman = User::join('homes', 'homes.user_id', 'users.id') ->where('users.id', $id) ->where('homes.estate_id', $estate_id) ->first([ @@ -426,7 +428,7 @@ public function estateGatemen( if($user) { return response()->json([ 'status' => true, - 'gateman' => $user + 'gateman' => $gateman ], 200); } else { @@ -438,4 +440,71 @@ public function estateGatemen( } } } + + /** + * Updates a gateman details for an estate + * + * @return \Illuminate\Http\Response + */ + public function updateEstateGateman( + $estate_id, + $id, + Request $request + ){ + // Verifies that the logged-in user is assigned to the requested estate + $user_estate = Home::whereUserIdAndEstateId($this->user->id, $estate_id)->first(); + + if (is_null($user_estate)) { + return response()->json([ + 'status' => false, + 'message'=> "Access denied", + ], 403); + } + else + { + $gateman = User::join('homes', 'homes.user_id', 'users.id') + ->where('users.id', $id) + ->where('users.user_type', 'gateman') + ->where('homes.estate_id', $estate_id) + ->get(); + + // Check if such user exists as a gateman for the estate + if (!$gateman->isEmpty()) { + // Validate the posted data + $this->validate($request, [ + 'name' => ['regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/'], + 'phone' => ['string'], + ]); + + $updatedUser = User::find($id); + + // Update user's details + $updatedUser->name = $request->name ?? $updatedUser->name; + $updatedUser->phone = $request->phone ?? $updatedUser->phone; + + // Save the update + $updatedUser->save(); + + // Prepare result to be outputted + $result = [ + 'name' => $updatedUser->name, + 'phone' => $updatedUser->phone + ]; + + // Return response + return response()->json([ + 'status' => true, + 'message' => "The gateman's record has successfully been updated", + 'result' => $updatedUser + ], 200); + } + else + { + return response()->json([ + 'status' => false, + 'message'=> "The user is not recognised as a gateman", + ], 400); + } + } + } } From 6901db9493774b6899e2f268212ff923a35617f0 Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Wed, 30 Oct 2019 18:14:16 +0100 Subject: [PATCH 08/14] added removal of gateman by an estate admin --- app/Http/Controllers/GatemanController.php | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/app/Http/Controllers/GatemanController.php b/app/Http/Controllers/GatemanController.php index f2fb08d7a..255573c60 100644 --- a/app/Http/Controllers/GatemanController.php +++ b/app/Http/Controllers/GatemanController.php @@ -507,4 +507,62 @@ public function updateEstateGateman( } } } + + /** + * Deletes a gateman record for an estate + * + * @return \Illuminate\Http\Response + */ + public function deleteEstateGateman( + $estate_id, + $id, + Request $request + ){ + // Verifies that the logged-in user is assigned to the requested estate + $user_estate = Home::whereUserIdAndEstateId($this->user->id, $estate_id)->first(); + + if (is_null($user_estate)) { + return response()->json([ + 'status' => false, + 'message'=> "Access denied", + ], 403); + } + else + { + $gateman = User::join('homes', 'homes.user_id', 'users.id') + ->where('users.id', $id) + ->where('users.user_type', 'gateman') + ->where('homes.estate_id', $estate_id) + ->get(); + + // Check if such user exists as a gateman for the estate + if (!$gateman->isEmpty()) { + // Delete the record + $update = User::find($id)->delete(); + + if ($update) { + return response()->json([ + 'status' => true, + 'message' => 'Gateman has been deleted successfully!', + ], 200); + } + else + { + // if delete action fails, send a response + return response()->json([ + 'status' => false, + 'message' => 'Sorry, this gateman could not be deleted!', + ], 500); + } + } + else + { + return response()->json([ + 'status' => false, + 'message'=> "The user is not recognised as a gateman", + ], 400); + } + } + + } } From ed3d1773d6c98002d84ee381b2784464e1262a31 Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Wed, 30 Oct 2019 18:15:15 +0100 Subject: [PATCH 09/14] added estate admin gateman management routes --- routes/v1.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/routes/v1.php b/routes/v1.php index 042c72c83..f41908b44 100644 --- a/routes/v1.php +++ b/routes/v1.php @@ -183,7 +183,17 @@ //Select Estate Route::post('/estate/choose/{id}', 'EstateController@estateMemeber'); + // Get a single gateman or all gatemen for an estate + Route::get('estate/{estate_id}/gateman/{id?}', 'GatemanController@estateGatemen')->middleware('estateAdmin'); + // Add gateman to an estate + Route::post('estate/{id}/gateman', 'GatemanController@addEstateGateman')->middleware('estateAdmin'); + + // Edit a gateman for an estate + Route::put('estate/{estate_id}/gateman/{id}', 'GatemanController@updateEstateGateman')->middleware('estateAdmin'); + + // Delete a single gateman or list of gatemen for an estate + Route::delete('estate/{estate_id}/gateman/{id}', 'GatemanController@deleteEstateGateman')->middleware('estateAdmin'); //(Users Messging) //Get message From bbdcf35c89dbe3ad57be63173c4c87d9cf97b93a Mon Sep 17 00:00:00 2001 From: Adekanye Jubril <37573577+tyroklone@users.noreply.github.com> Date: Wed, 30 Oct 2019 10:43:38 -0700 Subject: [PATCH 10/14] Search method API now works perfectly --- .../Controllers/ServiceProviderController.php | 60 ++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/ServiceProviderController.php b/app/Http/Controllers/ServiceProviderController.php index 18dc0c73a..143d65e8f 100644 --- a/app/Http/Controllers/ServiceProviderController.php +++ b/app/Http/Controllers/ServiceProviderController.php @@ -289,6 +289,51 @@ public function softDelete($id) } } + public function search($id) + { + $db = Service_Provider::find($id); + $name = $db->name ?? 'null'; + $phone = $db->phone ?? 'null'; + $cat_id = $db->category_id ?? 'null'; + $des = $db->description ?? 'null'; + $status = $db->status; + $created = $db->created_at ?? 'null'; + $updated = $db->updated_at ?? 'null'; + + if($status == 1) + { + $res["status"] = "Active"; + } + else + { + $res["status"] = "Inactive"; + } + + try { + $cat = Category::find($cat_id); + $cat_name = $cat->title; + + $res["status_code"] = 200; + $res["message"] = "Success!"; + $res["name"] = $name; + $res["phone"] = $phone; + $res["description"] = $des; + $res["created"] = $created; + $res["updated"] = $updated; + $res["category"] = $cat_name; + + return response()->json($res, $res["status_code"]); + } + catch (\Exception $e) + { + $res["status_code"] = 501; + $res["message"] = "Failed!"; + $res["error"] = $e->getMessage(); + + return response()->json($res, $res["status_code"]); + } + } + public function search($id) { try { @@ -310,16 +355,17 @@ public function search($id) $res["status"] = "Inactive"; } - // Put all data into an array - $data = array($name, $phone, $des, $created, $updated); - - $cat = Sp_Category::find($cat_id); + $cat = Category::find($cat_id); $cat_name = $cat->title; - $data[] = $cat_name; - + $res["status_code"] = 200; $res["message"] = "Success!"; - $res["data"] = $data; + $res["name"] = $name; + $res["phone"] = $phone; + $res["description"] = $des; + $res["created"] = $created; + $res["updated"] = $updated; + $res["category"] = $cat_name; return response()->json($res, $res["status_code"]); } From d9b369a7458f9afe858fd28fd584c394dc927c31 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 30 Oct 2019 19:58:39 +0100 Subject: [PATCH 11/14] Api endpoint to create service provider request --- .../Controllers/ServiceProviderController.php | 330 ++++++++---------- routes/v1.php | 2 + 2 files changed, 156 insertions(+), 176 deletions(-) diff --git a/app/Http/Controllers/ServiceProviderController.php b/app/Http/Controllers/ServiceProviderController.php index 18dc0c73a..c5dbc56f6 100644 --- a/app/Http/Controllers/ServiceProviderController.php +++ b/app/Http/Controllers/ServiceProviderController.php @@ -10,48 +10,52 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; use App\Http\Controllers\ImageController; +use App\Estate; class ServiceProviderController extends Controller { - public function showAll() { + public function showAll() + { $res = array(); - + if (Auth::check()) { - $user = Auth::user(); - $role = $user->role; - - if ($role === "1" || $role === "2") { + $user = Auth::user(); + $role = $user->role; + + if ($role === "1" || $role === "2") { $service = Service_Provider::all(); if (!$service->isEmpty()) { $res["status"] = 200; $res["message"] = "All service providers."; - $res["data"] = $service; + $res["data"] = $service; } else { $res["status"] = 200; $res["message"] = "No service providers registered"; } - } else { - $res['status'] = 401; - $res['message'] = "You must login as a resident or admin."; - } - } else { - $res['status'] = 401; - $res['message'] = "You are not logged in."; - } + } else { + $res['status'] = 401; + $res['message'] = "You must login as a resident or admin."; + } + } else { + $res['status'] = 401; + $res['message'] = "You are not logged in."; + } return response()->json($res, $res['status']); } - public function byEstate() { + public function byEstate() + { $user = Auth::user(); $user_id = $user->id; - $estate_id = Home::where('user_id',$user_id)->pluck('estate_id'); + $estate_id = Home::where('user_id', $user_id)->pluck('estate_id'); $res = array(); try { - - $service = Category::with(['service_provider' => function ($query) use ($estate_id) { - $query->whereIn('estate_id', $estate_id); }])->get(); + + $service = Category::with(['service_provider' => function ($query) use ($estate_id) { + $query->whereIn('estate_id', $estate_id); + }])->get(); if (!$service->isEmpty()) { @@ -74,12 +78,12 @@ public function byEstate() { public function show($id) { $res = array(); - + if (Auth::check()) { - $user = Auth::user(); - $role = $user->role; - - if ($role === "1" || $role === "2") { + $user = Auth::user(); + $role = $user->role; + + if ($role === "1" || $role === "2") { $service = Service_Provider::find($id); if (!is_null($service)) { $res["status"] = 200; @@ -87,9 +91,9 @@ public function show($id) $res["data"] = $service; } else { $res["status"] = 200; - $res["message"] = "No service provider found."; + $res["message"] = "No service provider found."; } - + } else { $res['status'] = 401; $res['message'] = "You must login as a resident or admin."; @@ -100,29 +104,30 @@ public function show($id) } return response()->json($res, $res['status']); } - - public function byCategory($category_id) { + + public function byCategory($category_id) + { $res = array(); - + if (Auth::check()) { $user = Auth::user(); $role = $user->role; - + if ($role === "1" || $role === "2") { try { - $services = Service_Provider::where('category_id', $category_id)->get(); - - if(!$services->isEmpty()) { + $services = Service_Provider::where('category_id', $category_id)->get(); + + if (!$services->isEmpty()) { $res['status'] = 200; $res['message'] = "Retrieved service providers"; $res['data'] = $services; - - } else { + + } else { $res['status'] = 404; $res['message'] = "No service providers in this category"; } - } catch(Exception $e) { + } catch (Exception $e) { $res['status'] = 501; $res['message'] = "An error occurred trying to retrieve service providers $e"; } @@ -139,41 +144,40 @@ public function byCategory($category_id) { public function create(Request $request, ImageController $image) { - $validator = Validator::make($request->all(), [ - 'name' => 'required|string|min:3', - 'phone' => 'required', - 'description' => 'required', - 'estate_id' => 'required|int', - 'category_id' => 'required|int' - ]); + $validator = Validator::make($request->all(), [ + 'name' => 'required|string|min:3', + 'phone' => 'required', + 'description' => 'required', + 'estate_id' => 'required|int', + 'category_id' => 'required|int' + ]); if ($validator->fails()) { - return ['message' => 'Please fill all Fields']; + return ['message' => 'Please fill all Fields']; } //start temporay transaction DB::beginTransaction(); - try{ + try { - $service = new Service_Provider; - $service->name = $request->input("name"); - $service->phone = $request->input("phone"); + $service = new Service_Provider; + $service->name = $request->input("name"); + $service->phone = $request->input("phone"); $service->description = $request->input("description"); - $service->estate_id = $request->input("estate_id"); + $service->estate_id = $request->input("estate_id"); $service->category_id = $request->input("category_id"); - //Upload image - //Upload image - if($request->hasFile('image')) { + //Upload image + //Upload image + if ($request->hasFile('image')) { $data = $this->upload($request, $image); - if($data['status_code'] != 200) { + if ($data['status_code'] != 200) { return response()->json($data, $data['status_code']); } $service->image = $data['image']; - }else { + } else { $data = null; $service->image = 'noimage.jpg'; - } -; + }; $service->save(); //if operation was successful save commit save to database @@ -184,7 +188,7 @@ public function create(Request $request, ImageController $image) $res['image_info'] = $data; return response()->json($res, 200); - }catch(\Exception $e) { + } catch (\Exception $e) { //rollback what is saved DB::rollBack(); @@ -197,48 +201,48 @@ public function create(Request $request, ImageController $image) } - public function update(Request $request, $id, ImageController $image) + public function update(Request $request, $id, ImageController $image) { $this->validate($request, [ - 'name' => 'required|string|min:3', - 'phone' => 'required', + 'name' => 'required|string|min:3', + 'phone' => 'required', 'description' => 'required', - 'estate_id' => 'required|int', + 'estate_id' => 'required|int', 'category_id' => 'required|int' ]); //start temporay transaction DB::beginTransaction(); - try{ - $service = Service_Provider::find($id); - $service->name = $request->input("name"); - $service->phone = $request->input("phone"); + try { + $service = Service_Provider::find($id); + $service->name = $request->input("name"); + $service->phone = $request->input("phone"); $service->description = $request->input("description"); - $service->estate_id = $request->input("estate_id"); + $service->estate_id = $request->input("estate_id"); $service->category_id = $request->input("category_id"); - //Upload image - if($request->hasFile('image')) { + //Upload image + if ($request->hasFile('image')) { $data = $this->upload($request, $image, $service); - if($data['status_code'] != 200) { + if ($data['status_code'] != 200) { return response()->json($data, $data['status_code']); } $service->image = $data['image']; - }else { + } else { $data = null; $service->image = 'noimage.jpg'; } - $service->save(); + $service->save(); - //if operation was successful save commit save to database + //if operation was successful save commit save to database DB::commit(); - $res["status"] = true; + $res["status"] = true; $res["message"] = "Service provider Updated Successfully!"; $res["service"] = $service; - $res['image_info'] = $data; + $res['image_info'] = $data; return response()->json($res, 200); - }catch(\Exception $e) { + } catch (\Exception $e) { //rollback what is saved DB::rollBack(); @@ -252,115 +256,89 @@ public function update(Request $request, $id, ImageController $image) public function destroy($id) { - $service = Service_Provider::destroy($id); - if($service) - { - $res['status'] = 200; - $res["message"] = "Service Provider Deleted!"; - - return response()->json($res, 200); - } - else - { - $res['status'] = 404; - $res["message"] = "Unable To Delete Service Provider!"; - - return response()->json($res, $res['status']); - } - } - - public function softDelete($id) - { - $service = Service_Provider::destroy($id); - if($service) - { - $res["status"] = 200; - $res["message"] = "Service Provider Suspended!"; - $res["data"] = $service; - - return response()->json($res, $res["status"]); - } - else - { - $res["status"] = 501; - $res["message"] = "Unable To Suspend Service Provider!"; - - return response()->json($res, $res["status"]); - } - } - - public function search($id) - { - try { - $db = Service_Provider::find($id); - $name = $db->name ?? 'null'; - $phone = $db->phone ?? 'null'; - $cat_id = $db->category_id ?? 'null'; - $des = $db->description ?? 'null'; - $status = $db->status; - $created = $db->created_at ?? 'null'; - $updated = $db->updated_at ?? 'null'; - - if($status == 1) - { - $res["status"] = "Active"; - } - else - { - $res["status"] = "Inactive"; - } - - // Put all data into an array - $data = array($name, $phone, $des, $created, $updated); - - $cat = Sp_Category::find($cat_id); - $cat_name = $cat->title; - $data[] = $cat_name; - - $res["status_code"] = 200; - $res["message"] = "Success!"; - $res["data"] = $data; - - return response()->json($res, $res["status_code"]); - } - catch (\Exception $e) - { - $res["status_code"] = 501; - $res["message"] = "Failed!"; - $res["data"] = $e->getMessage(); - - return response()->json($res, $res["status_code"]); + $service = Service_Provider::destroy($id); + + if ($service) { + $res['status'] = 200; + $res["message"] = "Service Provider Deleted!"; + return response()->json($res, 200); + } else { + $res['status'] = 404; + $res["message"] = "No service found"; + return response()->json($res, $res['status']); } - } - - public function restore($id) - { - $service = Service_Provider::onlyTrashed()->find($id); - if($service) - { - $res["status"] = 200; - $res["message"] = "Service Provider Was Unsuspended!"; - $res["data"] = $service; - - return response()->json($res, $res["status"]); } - else + + public function upload($request, $image, $table = null) { - $res["status"] = 501; - $res["message"] = "Unable To Unsuspend Service Provider!"; - - return response()->json($res, $res["status"]); - } - } - - public function upload($request, $image, $table=null) { $user = Auth::user(); $this->validate($request, [ - 'image' => "image|max:4000", + 'image' => "image|max:4000", ]); //Image Engine $res = $image->imageUpload($request, $table); return $res; } + + public function create_request(Request $request, ImageController $image) + { + $validator = Validator::make($request->all(), [ + 'name' => 'required|string|min:3', + 'phone' => 'required', + 'description' => 'required', + 'estate_id' => 'required|int', + 'category_id' => 'required|int' + ]); + + if ($validator->fails()) { + return ['message' => 'Please fill all Fields']; + } + //start temporay transaction + DB::beginTransaction(); + try { + + $service = new Service_Provider; + $service->name = $request->input("name"); + $service->phone = $request->input("phone"); + $service->description = $request->input("description"); + $service->estate_id = $request->input("estate_id"); + $service->category_id = $request->input("category_id"); + $service->status = 0; + + //Upload image + //Upload image + if ($request->hasFile('image')) { + $data = $this->upload($request, $image); + if ($data['status_code'] != 200) { + return response()->json($data, $data['status_code']); + } + $service->image = $data['image']; + } else { + $data = null; + $service->image = 'noimage.jpg'; + }; + $service->save(); + + //if operation was successful save commit save to database + DB::commit(); + $res["status"] = true; + $res["message"] = "Service Provider Request Created Successfully!"; + $res["data"] = $service; + $res['image_info'] = $data; + return response()->json($res, 200); + + } catch (\Exception $e) { + //rollback what is saved + DB::rollBack(); + + $res['status'] = false; + $res['message'] = 'An error occured, please try again!'; + $res['hint'] = $e->getMessage(); + return response()->json($res, 501); + + } + + } + } diff --git a/routes/v1.php b/routes/v1.php index 2fe006684..8e66e0cae 100644 --- a/routes/v1.php +++ b/routes/v1.php @@ -379,6 +379,8 @@ $gateman->notify(new App\Notifications\VisitorArrivalNotification($resident, $gateman, $visitor)); }); +//----------- Service provider request route ---------------------------------// +Route::post("service_provider/create_request", "ServiceProviderController@create_request"); // Route::get('init', function () { // event(new App\Events\notify('Someone')); From 643b2e09f515fe4fe21280a13d6a8493e678300d Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Wed, 30 Oct 2019 22:16:33 +0100 Subject: [PATCH 12/14] updated gateman controller --- app/Http/Controllers/GatemanController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/GatemanController.php b/app/Http/Controllers/GatemanController.php index 2ff3822be..1bcec3ae8 100644 --- a/app/Http/Controllers/GatemanController.php +++ b/app/Http/Controllers/GatemanController.php @@ -422,7 +422,7 @@ public function estateGatemen( ->get(); return response()->json([ - 'count' => $user->count(), + 'count' => $gatemen->count(), 'status' => true, 'gatemen' => $gatemen, ], 200); @@ -440,7 +440,7 @@ public function estateGatemen( 'homes.id as home_id', 'users.id as user_id' ]); - if($user) { + if($gateman) { return response()->json([ 'status' => true, 'gateman' => $gateman From 8d6df2c87728ee9fc7b67b3d1ac9ba5df9dd7b51 Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Wed, 30 Oct 2019 22:22:51 +0100 Subject: [PATCH 13/14] updated estate gateman routes --- routes/v1.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/v1.php b/routes/v1.php index 63befc068..7157675da 100644 --- a/routes/v1.php +++ b/routes/v1.php @@ -215,7 +215,7 @@ // Edit a gateman for an estate Route::put('estate/{estate_id}/gateman/{id}', 'GatemanController@updateEstateGateman')->middleware('estateAdmin'); - // Delete a single gateman or list of gatemen for an estate + // Delete a single gateman for an estate Route::delete('estate/{estate_id}/gateman/{id}', 'GatemanController@deleteEstateGateman')->middleware('estateAdmin'); //(Users Messging) From 0d63cf56cb3884566adfb45d606b0a320951ee64 Mon Sep 17 00:00:00 2001 From: gabrielandy Date: Thu, 31 Oct 2019 00:25:05 +0100 Subject: [PATCH 14/14] updated gateman controller --- app/Http/Controllers/GatemanController.php | 41 +++++++++------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/app/Http/Controllers/GatemanController.php b/app/Http/Controllers/GatemanController.php index 1bcec3ae8..2d20f8ef3 100644 --- a/app/Http/Controllers/GatemanController.php +++ b/app/Http/Controllers/GatemanController.php @@ -236,7 +236,7 @@ public function admitVisitor(Request $request) else { $res['Error'] = "Unauthorized - Access Denied!"; $res['gatema'] = $resident; - return response()->json($res, 403); + return response()->json($res, 401); } } else { @@ -335,8 +335,8 @@ public function addEstateGateman( if (is_null($user_estate)) { return response()->json([ 'status' => false, - 'message'=> "Access denied", - ], 403); + 'message'=> "Unauthorized!", + ], 401); } else { @@ -386,7 +386,7 @@ public function addEstateGateman( 'status' => false, 'message' => 'Error, the gateman could not be added', 'hint' => $e->getMessage() - ], 200); + ], 501); } } } @@ -407,8 +407,8 @@ public function estateGatemen( if (is_null($user_estate)) { return response()->json([ 'status' => false, - 'message'=> "Access denied", - ], 403); + 'message'=> "Unauthorized!", + ], 401); } else { @@ -450,7 +450,7 @@ public function estateGatemen( return response()->json([ 'status' => false, 'message' => "We cannot verify the user with id: {$id} as a gateman assigned to ". Estate::find($estate_id)->estate_name, - ], 404); + ], 406); } } } @@ -472,8 +472,8 @@ public function updateEstateGateman( if (is_null($user_estate)) { return response()->json([ 'status' => false, - 'message'=> "Access denied", - ], 403); + 'message'=> "Unauthorized!", + ], 401); } else { @@ -500,12 +500,6 @@ public function updateEstateGateman( // Save the update $updatedUser->save(); - // Prepare result to be outputted - $result = [ - 'name' => $updatedUser->name, - 'phone' => $updatedUser->phone - ]; - // Return response return response()->json([ 'status' => true, @@ -517,8 +511,8 @@ public function updateEstateGateman( { return response()->json([ 'status' => false, - 'message'=> "The user is not recognised as a gateman", - ], 400); + 'message' => "We cannot verify the user with id: {$id} as a gateman assigned to ". Estate::find($estate_id)->estate_name, + ], 406); } } } @@ -539,8 +533,8 @@ public function deleteEstateGateman( if (is_null($user_estate)) { return response()->json([ 'status' => false, - 'message'=> "Access denied", - ], 403); + 'message'=> "Unauthorized!", + ], 401); } else { @@ -566,18 +560,17 @@ public function deleteEstateGateman( // if delete action fails, send a response return response()->json([ 'status' => false, - 'message' => 'Sorry, this gateman could not be deleted!', - ], 500); + 'message' => 'Sorry, this gateman could not be deleted at the moment!', + ], 501); } } else { return response()->json([ 'status' => false, - 'message'=> "The user is not recognised as a gateman", - ], 400); + 'message' => "We cannot verify the user with id: {$id} as a gateman assigned to ". Estate::find($estate_id)->estate_name, + ], 406); } } - } }