Skip to content

Commit

Permalink
feat: Implement ProductController with CRUD operat
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Mar 11, 2024
1 parent b279c4a commit a26e784
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions app/Http/Controllers/ProductController.php
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']);
}
}

0 comments on commit a26e784

Please sign in to comment.