Skip to content

Commit

Permalink
Merge pull request #946 from SlovakNationalGallery/MG-48
Browse files Browse the repository at this point in the history
[item] increment view count api
  • Loading branch information
rastislav-chynoransky authored Feb 21, 2024
2 parents 6a0803b + 85babb4 commit 4db2bda
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/Http/Controllers/Api/V1/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/Api/V1/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4db2bda

Please sign in to comment.