feat: meal post visibility
This commit is contained in:
parent
7b8ede2a1a
commit
1964b60949
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum MealPostVisibility: string
|
||||
{
|
||||
case Private = 'private';
|
||||
case Public = 'public';
|
||||
case Followers = 'followers';
|
||||
}
|
||||
|
|
@ -13,7 +13,6 @@ use Illuminate\Http\Request;
|
|||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class MealPostController extends Controller
|
||||
{
|
||||
|
|
@ -116,7 +115,7 @@ class MealPostController extends Controller
|
|||
|
||||
if ($request->hasFile('image')) {
|
||||
$path = $request->file('image')->store('meal-posts', 'public');
|
||||
$data['image_url'] = asset(Storage::url($path));
|
||||
$data['image_url'] = $path;
|
||||
}
|
||||
|
||||
unset($data['image']);
|
||||
|
|
|
|||
|
|
@ -2,22 +2,26 @@
|
|||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\MealPostVisibility;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MealPostsRequest extends FormRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
$isCreating = $this->isMethod('post');
|
||||
|
||||
return [
|
||||
'image' => ['sometimes', 'nullable', 'image', 'max:4096'],
|
||||
'image_url' => ['required_without:image', 'nullable', 'string', 'url', 'max:2048'],
|
||||
'caption' => ['nullable', 'string', 'max:1000'],
|
||||
'calories' => ['nullable', 'integer', 'min:0'],
|
||||
'proteins' => ['nullable', 'numeric', 'min:0'],
|
||||
'carbs' => ['nullable', 'numeric', 'min:0'],
|
||||
'fats' => ['nullable', 'numeric', 'min:0'],
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
'eaten_at' => ['required', 'date'],
|
||||
'title' => [$isCreating ? 'required' : 'sometimes', 'string', 'max:255'],
|
||||
'eaten_at' => [$isCreating ? 'required' : 'sometimes', 'date'],
|
||||
'visibility' => ['sometimes', Rule::enum(MealPostVisibility::class)],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -31,6 +35,7 @@ class MealPostsRequest extends FormRequest
|
|||
return [
|
||||
'image.max' => "L'image ne doit pas dépasser 4 Mo.",
|
||||
'image_url.required_without' => 'Ajoute une image au repas.',
|
||||
'visibility' => 'Choisis une visibilité valide.',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Enums\MealPostVisibility;
|
||||
use App\Models\MealPosts;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
|
@ -16,13 +17,14 @@ class MealPostsResource extends JsonResource
|
|||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'imageUrl' => $this->image_url,
|
||||
'imageUrl' => $this->image_url ? asset(Storage::url($this->image_url)) : null,
|
||||
'caption' => $this->caption,
|
||||
'calories' => $this->calories,
|
||||
'proteins' => $this->proteins,
|
||||
'carbs' => $this->carbs,
|
||||
'fats' => $this->fats,
|
||||
'title' => $this->title,
|
||||
'visibility' => $this->visibility,
|
||||
'eatenAt' => $this->eaten_at,
|
||||
'createdAt' => $this->created_at,
|
||||
'updatedAt' => $this->updated_at,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\MealPostVisibility;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
|
@ -22,6 +23,11 @@ class MealPosts extends Model
|
|||
'fats',
|
||||
'title',
|
||||
'eaten_at',
|
||||
'visibility',
|
||||
];
|
||||
|
||||
protected $attributes = [
|
||||
'visibility' => MealPostVisibility::Private->value,
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
|
|
@ -33,6 +39,7 @@ class MealPosts extends Model
|
|||
{
|
||||
return [
|
||||
'eaten_at' => 'datetime',
|
||||
'visibility' => MealPostVisibility::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\MealPostVisibility;
|
||||
use App\Models\MealPosts;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
|
@ -22,6 +23,7 @@ class MealPostsFactory extends Factory
|
|||
'fats' => $this->faker->randomFloat(),
|
||||
'title' => $this->faker->word(),
|
||||
'eaten_at' => Carbon::now(),
|
||||
'visibility' => MealPostVisibility::Private,
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now(),
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('meal_posts', function (Blueprint $table): void {
|
||||
$table
|
||||
->string('visibility', 20)
|
||||
->default('private')
|
||||
->index();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('meal_posts', function (Blueprint $table): void {
|
||||
$table->dropIndex(['visibility']);
|
||||
$table->dropColumn('visibility');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -24,5 +24,7 @@ Route::middleware('auth:sanctum')->group(function (): void {
|
|||
|
||||
Route::middleware('auth:sanctum')->group(function (): void {
|
||||
Route::get('meal-posts/stats', [MealPostController::class, 'stats']);
|
||||
Route::apiResource('meal-posts', MealPostController::class);
|
||||
Route::apiResource('meal-posts', MealPostController::class)->except(['update']);
|
||||
Route::put('meal-posts/{mealPost}', [MealPostController::class, 'update'])->name('meal-posts.update');
|
||||
Route::patch('meal-posts/{mealPost}', [MealPostController::class, 'update'])->name('meal-posts.patch');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
use App\Enums\MealPostVisibility;
|
||||
use App\Models\MealPosts;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('stores and returns the selected meal post visibility', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/meal-posts', [
|
||||
'image_url' => 'https://example.com/meal.jpg',
|
||||
'title' => 'Bowl saumon avocat',
|
||||
'eaten_at' => '2026-05-16T12:00:00.000Z',
|
||||
'visibility' => MealPostVisibility::Followers->value,
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertCreated()
|
||||
->assertJsonPath('data.visibility', MealPostVisibility::Followers->value);
|
||||
|
||||
$this->assertDatabaseHas('meal_posts', [
|
||||
'id' => $response->json('data.id'),
|
||||
'user_id' => $user->id,
|
||||
'visibility' => MealPostVisibility::Followers->value,
|
||||
]);
|
||||
});
|
||||
|
||||
it('defaults meal post visibility to private', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->postJson('/api/meal-posts', [
|
||||
'image_url' => 'https://example.com/meal.jpg',
|
||||
'title' => 'Bowl saumon avocat',
|
||||
'eaten_at' => '2026-05-16T12:00:00.000Z',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertCreated()
|
||||
->assertJsonPath('data.visibility', MealPostVisibility::Private->value);
|
||||
});
|
||||
|
||||
it('validates meal post visibility', function () {
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$response = $this->postJson('/api/meal-posts', [
|
||||
'image_url' => 'https://example.com/meal.jpg',
|
||||
'title' => 'Bowl saumon avocat',
|
||||
'eaten_at' => '2026-05-16T12:00:00.000Z',
|
||||
'visibility' => 'friends',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['visibility']);
|
||||
});
|
||||
|
||||
it('patches meal post visibility without requiring creation fields', function () {
|
||||
$user = User::factory()->create();
|
||||
$mealPost = MealPosts::factory()->for($user, 'user')->create([
|
||||
'visibility' => MealPostVisibility::Private,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->patchJson("/api/meal-posts/{$mealPost->id}", [
|
||||
'visibility' => MealPostVisibility::Public->value,
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertJsonPath('data.visibility', MealPostVisibility::Public->value);
|
||||
|
||||
$this->assertDatabaseHas('meal_posts', [
|
||||
'id' => $mealPost->id,
|
||||
'visibility' => MealPostVisibility::Public->value,
|
||||
]);
|
||||
});
|
||||
Loading…
Reference in New Issue