Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/pack stickers #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 47 additions & 27 deletions app/Http/Controllers/StickerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,84 @@

namespace App\Http\Controllers;

use App\Models\Pack;
use App\Models\Sticker;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\File;

use function PHPUnit\Framework\stringStartsWith;

class StickerController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*/
public function create()
public function index(Pack $pack)
{
//
return $pack->stickers;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use json responses!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

laravel is atuomaticly convert model to json response so it will return like thi
{
"image": <image_path>,
"pack_id": <pack_id>
}

source: link

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the following format

  1. If successful
    {"status": bool, "data": dict}
  2. If it fails
    {"status": bool, "message": string}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have to follow the standards? because it will make it easier to process data

}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
public function store(Request $request, Pack $pack)
{
//
$validator = Validator::make($request->all(), [
'name' => 'required|max:32',
'image' => File::image()->max('10MB'),
]);

if ($validator->fails()) {
return response()->json([
'message' => 'The given data was invalid.',
'errors' => $validator->errors(),
], 422);
}

// store image
$path = $request->file('image')->store('stickers');
$request->merge(['image' => $path]);

$sticker = $pack->stickers()->create($validator->validated());

return response()->json([
'message' => 'Sticker created successfully',
'sticker' => $sticker
]);
}

/**
* Display the specified resource.
*/
public function show(Sticker $sticker)
public function show(Request $request, Pack $pack, Sticker $sticker)
{
//
}
if ($request->has('download')) {
if (stringStartsWith($sticker->image, 'http')) {
$stickerExtension = explode('.', $sticker->image);
$stickerExtension = $stickerExtension[1];
$filename = 'sticker' . '.' . $stickerExtension;
$tempImage = tempnam(sys_get_temp_dir(), $filename);
copy($sticker->image, $tempImage);

/**
* Show the form for editing the specified resource.
*/
public function edit(Sticker $sticker)
{
//
}
return response()->download($tempImage, $filename);
}
return response()->download(Storage::path($sticker->image));
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, Sticker $sticker)
{
//
return $sticker;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this?

}

/**
* Remove the specified resource from storage.
*/
public function destroy(Sticker $sticker)
public function destroy(Pack $pack, Sticker $sticker)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete the pack variable if it is not used

{
//
$sticker->delete();
return response()->json(['message' => 'Sticker deleted successfully.']);
}
}
4 changes: 4 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use App\Http\Controllers\auth\LogoutController;
use App\Http\Controllers\auth\RegisterController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\PackCategoryController;
use App\Http\Controllers\PackController;
use App\Http\Controllers\StickerController;
use Illuminate\Support\Facades\Route;


Expand All @@ -20,4 +22,6 @@

Route::apiResource('category', CategoryController::class);
Route::apiResource('pack', PackController::class);
Route::apiResource('pack.sticker', StickerController::class)
->except('update');
});