-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement ProductController with CRUD operat
- Loading branch information
1 parent
b279c4a
commit a26e784
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Product; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class ProductController extends Controller | ||
{ | ||
public function create(Request $request) | ||
{ | ||
$validatedData = $request->validate([ | ||
'name' => 'required|string|max:255', | ||
'description' => 'required|string', | ||
'price' => 'required|numeric', | ||
'category' => 'required|string|max:255', | ||
'inventory_count' => 'required|integer', | ||
]); | ||
|
||
$product = Product::create($validatedData); | ||
|
||
return response()->json($product, Response::HTTP_CREATED); | ||
} | ||
|
||
public function list() | ||
{ | ||
$products = Product::all(); | ||
|
||
return response()->json($products); | ||
} | ||
|
||
public function show($id) | ||
{ | ||
$product = Product::find($id); | ||
|
||
if (!$product) { | ||
return response()->json(['message' => 'Product not found'], Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
return response()->json($product); | ||
} | ||
|
||
public function update(Request $request, $id) | ||
{ | ||
$product = Product::find($id); | ||
|
||
if (!$product) { | ||
return response()->json(['message' => 'Product not found'], Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
$validatedData = $request->validate([ | ||
'name' => 'string|max:255', | ||
'description' => 'string', | ||
'price' => 'numeric', | ||
'category' => 'string|max:255', | ||
'inventory_count' => 'integer', | ||
]); | ||
|
||
$product->update($validatedData); | ||
|
||
return response()->json($product); | ||
} | ||
|
||
public function delete($id) | ||
{ | ||
$product = Product::find($id); | ||
|
||
if (!$product) { | ||
return response()->json(['message' => 'Product not found'], Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
$product->delete(); | ||
|
||
return response()->json(['message' => 'Product deleted successfully']); | ||
} | ||
} |