Add ProductController and Producto model with CRUD operations and migration
This commit is contained in:
67
app/Http/Controllers/ProductController.php
Normal file
67
app/Http/Controllers/ProductController.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Producto;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return response()->json(Producto::query()->latest()->get());
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate($this->rules());
|
||||
|
||||
$producto = Producto::create($validated);
|
||||
|
||||
return response()->json($producto, 201);
|
||||
}
|
||||
|
||||
public function show(Producto $producto): JsonResponse
|
||||
{
|
||||
return response()->json($producto);
|
||||
}
|
||||
|
||||
public function update(Request $request, Producto $producto): JsonResponse
|
||||
{
|
||||
$validated = $request->validate($this->rules($producto->id));
|
||||
|
||||
$producto->update($validated);
|
||||
|
||||
return response()->json($producto);
|
||||
}
|
||||
|
||||
public function destroy(Producto $producto): Response
|
||||
{
|
||||
$producto->delete();
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function rules(?int $productoId = null): array
|
||||
{
|
||||
return [
|
||||
'tenant_id' => ['required', 'integer'],
|
||||
'categoria_id' => ['required', 'integer'],
|
||||
'slug' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('productos', 'slug')->ignore($productoId),
|
||||
],
|
||||
'nombre' => ['required', 'string', 'max:255'],
|
||||
'descripcion' => ['nullable', 'string'],
|
||||
'precio' => ['required', 'numeric', 'min:0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
31
app/Models/Producto.php
Normal file
31
app/Models/Producto.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
#[Fillable([
|
||||
'tenant_id',
|
||||
'categoria_id',
|
||||
'slug',
|
||||
'nombre',
|
||||
'descripcion',
|
||||
'precio',
|
||||
])]
|
||||
class Producto extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'productos';
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'tenant_id' => 'integer',
|
||||
'categoria_id' => 'integer',
|
||||
'precio' => 'decimal:2',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('productos', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('tenant_id');
|
||||
$table->unsignedBigInteger('categoria_id');
|
||||
$table->string('slug')->unique();
|
||||
$table->string('nombre');
|
||||
$table->text('descripcion')->nullable();
|
||||
$table->decimal('precio', 10, 2);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('productos');
|
||||
}
|
||||
};
|
||||
@@ -1,8 +1,11 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\ProductController;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
})->middleware('auth:sanctum');
|
||||
|
||||
Route::apiResource('productos', ProductController::class);
|
||||
|
||||
Reference in New Issue
Block a user