feature: add api for categories

feature: add initial docker build and compose for development mode
This commit is contained in:
jon brookes 2026-01-15 15:29:42 +00:00
parent 6bf486e52b
commit f980be8e28
14 changed files with 520 additions and 0 deletions

View file

@ -0,0 +1,61 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreCategoryRequest;
use App\Http\Requests\UpdateCategoryRequest;
use App\Http\Resources\CategoryResource;
use App\Models\Category;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return CategoryResource::collection(Category::all());
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreCategoryRequest $request)
{
return new CategoryResource(Category::create($request->validated()));
}
/**
* Display the specified resource.
*/
public function show(Category $category)
{
return new CategoryResource($category);
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateCategoryRequest $request, Category $category)
{
$category->update($request->validated());
return new CategoryResource($category);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Category $category)
{
$user = auth()->user();
if (! $user || $user->email !== config('app.admin_email')) {
return response()->json(['message' => 'Forbidden'], 403);
}
$category->delete();
return response()->noContent();
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreCategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user() && $this->user()->email === config('app.admin_email');
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string|max:255|unique:categories,name',
];
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateCategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user() && $this->user()->email === config('app.admin_email');
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'sometimes|required|string|max:255|unique:categories,name,'.$this->category->id,
];
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class CategoryResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}