Skip to content

Commit

Permalink
feature: Server edit and delete endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed Sep 28, 2023
1 parent ee84978 commit 99696cf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
69 changes: 69 additions & 0 deletions app/Http/Controllers/API/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace App\Http\Controllers\API;

use App\Connectors\GenericConnector;
use App\Exceptions\JsonResponseException;
use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use App\Models\Certificate;
use App\Models\Permission;
use App\Models\Server;
use App\Models\ServerKey;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use mervick\aesEverywhere\AES256;

class ServerController extends Controller
Expand Down Expand Up @@ -67,6 +69,73 @@ public function create(Request $request)
return $this->grantPermissions($server);
}

/**
* Update server name and IP address
*
* @param Request $request
* @return JsonResponse
*/
public function update(Request $request) {
$server = Server::find($request->server_id);
if (! $server) {
throw new JsonResponseException([
'message' => 'Sunucu bulunamadı.'
], Response::HTTP_NOT_FOUND);
}

if (! Permission::can(auth('api')->user()->id, 'liman', 'id', 'server_details')) {
throw new JsonResponseException([
'message' => 'Bu işlemi yapmak için yetkiniz yok!'
], '', Response::HTTP_FORBIDDEN);
}

$server->name = $request->name;
$server->ip_address = $request->ip_address;
$server->save();

return response()->json([
'message' => 'İşlem başarılı.'
]);
}

/**
* Delete server from system
*
* @param Request $request
* @return JsonResponse
*/
public function delete(Request $request) {
$server = Server::find($request->server_id);
if (! $server) {
throw new JsonResponseException([
'message' => 'Sunucu bulunamadı.'
], Response::HTTP_NOT_FOUND);
}

if (
$server->user_id != auth('api')->id() &&
! auth('api')
->user()
->isAdmin()
) {
throw new JsonResponseException([
'message' => 'Bu işlemi yapmak için yetkiniz yok!'
], '', Response::HTTP_FORBIDDEN);
}

if (! Permission::can(auth('api')->user()->id, 'liman', 'id', 'server_details')) {
throw new JsonResponseException([
'message' => 'Bu işlemi yapmak için yetkiniz yok!'
], '', Response::HTTP_FORBIDDEN);
}

$server->delete();

return response()->json([
'message' => 'İşlem başarılı.'
]);
}

/**
* Grant server certificate
*
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
// Server Details
Route::get('/', [Server\DetailsController::class, 'index']);
Route::post('/', [ServerController::class, 'create']);
Route::patch('/{server_id}', [ServerController::class, 'update']);
Route::delete('/{server_id}', [ServerController::class, 'delete']);
Route::post('/{server_id}/favorites', [Server\DetailsController::class, 'favorite']);

// Server Creation Validations
Expand Down

0 comments on commit 99696cf

Please sign in to comment.