diff --git a/app/Http/Controllers/Api/V1/ItemController.php b/app/Http/Controllers/Api/V1/ItemController.php index f4cdec02c..5ae6a9086 100644 --- a/app/Http/Controllers/Api/V1/ItemController.php +++ b/app/Http/Controllers/Api/V1/ItemController.php @@ -190,6 +190,11 @@ public function detail(Request $request, $id) return $items->documents()->first(); } + public function incrementViewCount($id) + { + Item::findOrFail($id)->increment('view_count'); + } + protected function createQueryBuilder($q, $filter) { $builder = Query::bool(); diff --git a/routes/api.php b/routes/api.php index 9a2731589..45634ba88 100644 --- a/routes/api.php +++ b/routes/api.php @@ -31,6 +31,8 @@ 'items.aggregations' ); Route::get('items/{id}', [V1ItemController::class, 'detail'])->name('items.show'); + Route::post('items/{id}/views', [V1ItemController::class, 'incrementViewCount']) + ->name('items.views'); }); Route::prefix('v2')->group(function () { diff --git a/tests/Feature/Api/V1/ItemsTest.php b/tests/Feature/Api/V1/ItemsTest.php index daadcace7..66ae9c404 100644 --- a/tests/Feature/Api/V1/ItemsTest.php +++ b/tests/Feature/Api/V1/ItemsTest.php @@ -152,4 +152,14 @@ public function test_formatted_authors() $response['data'][0]['content']['authors_formatted'][0] ); } + + public function test_increment_view_count() + { + $item = Item::factory()->create(['view_count' => 0]); + + $this->postJson(route('api.v1.items.views', ['id' => $item->id])) + ->assertOk(); + + $this->assertEquals(1, $item->fresh()->view_count); + } }