111 lines
2.7 KiB
Markdown
111 lines
2.7 KiB
Markdown
|
|
# Bookmark Check API Documentation
|
||
|
|
|
||
|
|
## Overview
|
||
|
|
API endpoint untuk mengecek apakah suatu artikel sudah di-bookmark oleh user yang sedang login.
|
||
|
|
|
||
|
|
## Endpoint
|
||
|
|
|
||
|
|
### Check Bookmark by Article ID
|
||
|
|
**GET** `/bookmarks/check/{articleId}`
|
||
|
|
|
||
|
|
#### Description
|
||
|
|
Mengecek apakah artikel dengan ID tertentu sudah di-bookmark oleh user yang sedang login.
|
||
|
|
|
||
|
|
#### Parameters
|
||
|
|
- **articleId** (path, required): ID artikel yang akan dicek status bookmarknya
|
||
|
|
|
||
|
|
#### Headers
|
||
|
|
- **Authorization** (required): Bearer token untuk autentikasi user
|
||
|
|
|
||
|
|
#### Response
|
||
|
|
|
||
|
|
##### Success Response (200)
|
||
|
|
```json
|
||
|
|
{
|
||
|
|
"success": true,
|
||
|
|
"messages": ["Successfully checked bookmark status"],
|
||
|
|
"data": {
|
||
|
|
"isBookmarked": true,
|
||
|
|
"articleId": 123,
|
||
|
|
"bookmarkId": 456
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
##### Response Fields
|
||
|
|
- **isBookmarked** (boolean): Status apakah artikel sudah di-bookmark atau belum
|
||
|
|
- **articleId** (integer): ID artikel yang dicek
|
||
|
|
- **bookmarkId** (integer, optional): ID bookmark jika artikel sudah di-bookmark
|
||
|
|
|
||
|
|
#### Error Responses
|
||
|
|
|
||
|
|
##### 400 Bad Request
|
||
|
|
```json
|
||
|
|
{
|
||
|
|
"success": false,
|
||
|
|
"messages": ["Invalid article ID"]
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
##### 401 Unauthorized
|
||
|
|
```json
|
||
|
|
{
|
||
|
|
"success": false,
|
||
|
|
"messages": ["user not found"]
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
##### 500 Internal Server Error
|
||
|
|
```json
|
||
|
|
{
|
||
|
|
"success": false,
|
||
|
|
"messages": ["article not found"]
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
## Usage Examples
|
||
|
|
|
||
|
|
### cURL Example
|
||
|
|
```bash
|
||
|
|
curl -X GET "http://localhost:8080/bookmarks/check/123" \
|
||
|
|
-H "Authorization: Bearer YOUR_TOKEN_HERE"
|
||
|
|
```
|
||
|
|
|
||
|
|
### JavaScript Example
|
||
|
|
```javascript
|
||
|
|
const checkBookmark = async (articleId) => {
|
||
|
|
try {
|
||
|
|
const response = await fetch(`/bookmarks/check/${articleId}`, {
|
||
|
|
method: 'GET',
|
||
|
|
headers: {
|
||
|
|
'Authorization': `Bearer ${token}`,
|
||
|
|
'Content-Type': 'application/json'
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
const data = await response.json();
|
||
|
|
|
||
|
|
if (data.success) {
|
||
|
|
console.log('Is bookmarked:', data.data.isBookmarked);
|
||
|
|
if (data.data.bookmarkId) {
|
||
|
|
console.log('Bookmark ID:', data.data.bookmarkId);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error checking bookmark:', error);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
```
|
||
|
|
|
||
|
|
## Use Cases
|
||
|
|
1. **Frontend Bookmark Status**: Mengecek status bookmark untuk menampilkan icon bookmark yang sesuai (filled/unfilled)
|
||
|
|
2. **Conditional UI**: Menampilkan tombol "Remove Bookmark" atau "Add Bookmark" berdasarkan status
|
||
|
|
3. **Bookmark Counter**: Menghitung jumlah bookmark yang dimiliki user
|
||
|
|
4. **Bookmark Management**: Validasi sebelum melakukan operasi bookmark lainnya
|
||
|
|
|
||
|
|
## Notes
|
||
|
|
- Endpoint ini menggunakan middleware `UserMiddleware` untuk mengekstrak informasi user dari JWT token
|
||
|
|
- Jika artikel tidak ditemukan, akan mengembalikan error 500
|
||
|
|
- Jika user tidak ditemukan dari token, akan mengembalikan error 401
|
||
|
|
- Field `bookmarkId` hanya akan ada jika `isBookmarked` bernilai `true`
|