# Gallery API Documentation Dokumentasi API untuk collection `gallery` yang berisi foto-foto dengan caption. ## Base Endpoint ``` /api/gallery ``` ## Authentication Endpoint ini bersifat **public read-only**, tidak memerlukan authentication untuk membaca data. --- ## Endpoints ### GET `/api/gallery` Mengambil daftar gallery items dengan pagination dan filtering. **Query Parameters:** - `page` - Nomor halaman (default: 1) - `limit` - Jumlah item per halaman (default: 10) - `sort` - Sorting field (contoh: `-createdAt` untuk newest first, `createdAt` untuk oldest first) - `depth` - Depth untuk populate image relationship (default: 0, gunakan 1 untuk populate image object dengan URL) **Response Structure:** ```json { "docs": [ { "id": 1, "image": 123, // ID media (jika depth=0) atau object Media (jika depth=1) "caption": "Event foto description", "createdAt": "2025-01-15T10:00:00.000Z", "updatedAt": "2025-01-15T10:00:00.000Z" } ], "totalDocs": 50, "limit": 10, "totalPages": 5, "page": 1, "hasPrevPage": false, "hasNextPage": true } ``` **Gallery Object Fields:** - `id` - Unique identifier (number) - `image` - Media ID (number) atau Media object (jika depth=1) - `caption` - Caption/deskripsi foto (string, required) - `createdAt` - Creation timestamp (ISO date string) - `updatedAt` - Last update timestamp (ISO date string) ### GET `/api/gallery/:id` Mengambil single gallery item berdasarkan ID. **Path Parameters:** - `id` - Gallery item ID **Query Parameters:** - `depth` - Depth untuk populate image relationship (recommended: 1 untuk mendapatkan URL gambar) **Response:** Single gallery object --- ## Cara Menggunakan di Frontend ### 1. Fetch List Gallery Items ```javascript // Mengambil 12 gambar terbaru const response = await fetch('http://localhost:3001/api/gallery?limit=12&sort=-createdAt&depth=1') const data = await response.json() // data.docs berisi array gallery items data.docs.forEach(item => { console.log(item.caption) console.log(item.image.url) // URL gambar jika depth=1 }) ``` ### 2. Fetch Single Gallery Item ```javascript const response = await fetch('http://localhost:3001/api/gallery/123?depth=1') const item = await response.json() console.log(item.caption) console.log(item.image.url) // URL gambar ``` ### 3. Pagination ```javascript // Halaman 2, 20 item per halaman const response = await fetch('http://localhost:3001/api/gallery?page=2&limit=20&depth=1') const data = await response.json() // Check apakah ada halaman selanjutnya if (data.hasNextPage) { // Load more... } ``` ### 4. Display Gallery Grid **React Component Example:** ```jsx function GalleryGrid() { const [items, setItems] = useState([]) const [loading, setLoading] = useState(true) useEffect(() => { fetch('http://localhost:3001/api/gallery?limit=20&sort=-createdAt&depth=1') .then(res => res.json()) .then(data => { setItems(data.docs) setLoading(false) }) }, []) if (loading) return
{item.caption}