diff --git a/backend/app/Http/Controllers/PostController.php b/backend/app/Http/Controllers/PostController.php index 7f63815..265fa27 100644 --- a/backend/app/Http/Controllers/PostController.php +++ b/backend/app/Http/Controllers/PostController.php @@ -14,15 +14,25 @@ class PostController extends Controller $request->validate( [ 'lastPost' => 'required|integer', + 'firstPost' => 'required|integer', + 'order' => 'required|string|in:new,old', ] ); $lastPost = $request->lastPost; + $firstPost = $request->firstPost; - $posts = Post::query() - ->where(column: 'sequence', operator: '>', value: $lastPost) - ->take(10) - ->get(); + $postsQuery = Post::query()->with('user')->orderByDesc(column: 'sequence'); + + if ($request->order == 'new') { + $postsQuery->where(column: 'sequence', operator: '>', value: $firstPost); + } + + if ($request->order == 'old' && $lastPost != 0) { + $postsQuery->where(column: 'sequence', operator: '<', value: $lastPost); + } + + $posts = $postsQuery->take(10)->get(); return response( [ diff --git a/backend/app/Models/Post.php b/backend/app/Models/Post.php index c2645c6..5c793e8 100644 --- a/backend/app/Models/Post.php +++ b/backend/app/Models/Post.php @@ -5,10 +5,16 @@ namespace App\Models; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class Post extends Model { use HasFactory, HasUuids; protected $fillable = ['title', 'description']; + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } } diff --git a/frontend/components/PostCard.vue b/frontend/components/PostCard.vue index 54339eb..e16ff49 100644 --- a/frontend/components/PostCard.vue +++ b/frontend/components/PostCard.vue @@ -1,7 +1,11 @@