Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedYahyaE committed Dec 23, 2023
1 parent 4cc34b6 commit 469b688
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
10 changes: 6 additions & 4 deletions app/Http/Controllers/V1/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Models\Album;
use Illuminate\Http\Request;

use App\Http\Resources\V1\AlbumResource;

class AlbumController extends Controller
{
/**
Expand All @@ -18,7 +20,7 @@ class AlbumController extends Controller
public function index(Request $request) // Get all albums of the authenticated/logged-in user
{
// Note: The Resource collection() method wraps your response in a 'data' key. Data Wrapping: https://laravel.com/docs/9.x/eloquent-resources#data-wrapping
return \App\Http\Resources\V1\AlbumResource::collection(\App\Models\Album::where('user_id', $request->user()->id)->paginate()); // Return all albums (that ONLY belong to the authenticated/logged-in user)
return AlbumResource::collection(Album::where('user_id', $request->user()->id)->paginate()); // Return all albums (that ONLY belong to the authenticated/logged-in user)
}

/**
Expand All @@ -41,7 +43,7 @@ public function store(StoreAlbumRequest $request) // Create an Album (POST reque
// return $album;

// Using AlbumResource.php (instead of return $album; )
return new \App\Http\Resources\V1\AlbumResource($album); // We return the newly created album to the user
return new AlbumResource($album); // We return the newly created album to the user
}

/**
Expand All @@ -57,7 +59,7 @@ public function show(Request $request, Album $album) // Get a Single album (that
}


return new \App\Http\Resources\V1\AlbumResource($album); // We return the album to the user
return new AlbumResource($album); // We return the album to the user
}

/**
Expand All @@ -80,7 +82,7 @@ public function update(UpdateAlbumRequest $request, Album $album) // Update an A
$album->update($request->all());


return new \App\Http\Resources\V1\AlbumResource($album); // We return the album after being updated to the user
return new AlbumResource($album); // We return the album after being updated to the user
}

/**
Expand Down
18 changes: 10 additions & 8 deletions app/Http/Controllers/V1/ImageManipulationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Models\ImageManipulation;
use App\Http\Requests\ResizeImageRequest;
use Illuminate\Http\Request;
use App\Models\Album;
use App\Http\Resources\V1\ImageManipulationResource;
// use App\Http\Requests\UpdateImageManipulationRequest; // We deleted this class as we won't have this UPDATE feature in our API


Expand All @@ -20,21 +22,21 @@ class ImageManipulationController extends Controller
public function index(Request $request) // Get ALL images (of the authenticated/logged-in user)
{
// Note: The Resource collection() method wraps your response in a 'data' key. Data Wrapping: https://laravel.com/docs/9.x/eloquent-resources#data-wrapping
return \App\Http\Resources\V1\ImageManipulationResource::collection(\App\Models\ImageManipulation::where('user_id', $request->user()->id)->paginate()); // Return all albums (that ONLY belong to the authenticated/logged-in user)
return ImageManipulationResource::collection(ImageManipulation::where('user_id', $request->user()->id)->paginate()); // Return all albums (that ONLY belong to the authenticated/logged-in user)
}



// Get images by album (of the authenticated/logged-in user)
public function byAlbum(Request $request, \App\Models\Album $album) {
public function byAlbum(Request $request, Album $album) {
if ($request->user()->id != $album->user_id) { // if the currently authenticated user is not the owner of the album in the URL, the user is unauthorized to show that album's images
return abort(403, 'Unauthorized'); // '403' HTTP status code means 'Forbidden'
}

$where = [
'album_id' => $album->id,
];
return \App\Http\Resources\V1\ImageManipulationResource::collection(\App\Models\ImageManipulation::where($where)->paginate()); // We return the album images that belong to the authenticated/logged-in user
return ImageManipulationResource::collection(ImageManipulation::where($where)->paginate()); // We return the album images that belong to the authenticated/logged-in user
}


Expand All @@ -59,14 +61,14 @@ public function resize(ResizeImageRequest $request)
// $data array is the data that we're going to save in the `image_manipulations` database table (at the end of this method)
$data = [
// table column names => data to be saved in the table
'type' => \App\Models\ImageManipulation::TYPE_RESIZE, // the constant that we created in ImageManipulation.php model
'type' => ImageManipulation::TYPE_RESIZE, // the constant that we created in ImageManipulation.php model
'data' => json_encode($all), // save the whole data in `data` column as JSON format
'user_id' => $request->user()->id, // `user_id` is the authenticated/logged-in user `id`
];

// Since 'album_id' is OPTIONAL, if user submits an 'album_id' key in their HTTP Request Body (JSON Body in Postman if the image is a URL, or form-data Body in Postman if the image is an uploaded file) along with the image's physical file (in form-data Body in Postman) or URL (in JSON Body in Postman), we need to check if that 'album_id' belongs to the authenticated/logged-in user
if (isset($all['album_id'])) { // if an 'album_id' key is submitted by the user in their HTTP Request Body (JSON Body in Postman if the image is a URL, or form-data Body in Postman if the image is an uploaded file) along with the image's physical file (in form-data Body in Postman) or URL (in JSON Body in Postman), append that 'album_id' to the $data array
$album = \App\Models\Album::find($all['album_id']);
$album = Album::find($all['album_id']);
if ($request->user()->id != $album->user_id) { // if the currently authenticated/logged-in user is not the owner of that album, they aren't authorized to resize that image of that album that doesn't belong to them
return abort(403, 'Unauthorized'); // '403' HTTP status code means 'Forbidden'
}
Expand Down Expand Up @@ -144,10 +146,10 @@ public function resize(ResizeImageRequest $request)

$data['output_path'] = $dir . $resizedFilename; // Append the resized image path to the $data array to store it in the `output_path` column in `image_manipulations` database table

$imageManipulation = \App\Models\ImageManipulation::create($data); // INSERT $data array into `image_manipulations` database table // Note: The create() method returns an instance of the inserted instance (data) in model / database table. This can be done by assigning the create() method to a variable e.g. $flight = Flight::create([ 'name' => 'London to Paris' ]); where $flight variable is the newly created instance. Check https://laravel.com/docs/9.x/eloquent#mass-assignment:~:text=single%20PHP%20statement.-,The%20inserted%20model%20instance%20will%20be%20returned%20to%20you%20by%20the,-create AND https://laravel.com/docs/9.x/eloquent-relationships#the-create-method:~:text=The%20newly%20created,method
$imageManipulation = ImageManipulation::create($data); // INSERT $data array into `image_manipulations` database table // Note: The create() method returns an instance of the inserted instance (data) in model / database table. This can be done by assigning the create() method to a variable e.g. $flight = Flight::create([ 'name' => 'London to Paris' ]); where $flight variable is the newly created instance. Check https://laravel.com/docs/9.x/eloquent#mass-assignment:~:text=single%20PHP%20statement.-,The%20inserted%20model%20instance%20will%20be%20returned%20to%20you%20by%20the,-create AND https://laravel.com/docs/9.x/eloquent-relationships#the-create-method:~:text=The%20newly%20created,method


return new \App\Http\Resources\V1\ImageManipulationResource($imageManipulation); // We return the newly resized image to the user
return new ImageManipulationResource($imageManipulation); // We return the newly resized image to the user
}

/**
Expand All @@ -163,7 +165,7 @@ public function show(Request $request, ImageManipulation $image) // Get a Single
}


return new \App\Http\Resources\V1\ImageManipulationResource($image);
return new ImageManipulationResource($image);
}

/**
Expand Down

0 comments on commit 469b688

Please sign in to comment.