ChandimaPrabath's picture
api routes
11c56f5
import {BASE_URL} from "@/lib/config";
// Function to get music store
async function getMusicStore() {
try {
const response = await fetch(`${BASE_URL}/api/get/music/store`);
if (!response.ok) {
throw new Error("Failed to fetch music store");
}
return await response.json();
} catch (error) {
console.error("Error fetching music store:", error);
throw error;
}
}
// Function to get all music with optional pagination
async function getAllMusic(page = null, limit = null) {
let url = `${BASE_URL}/api/get/music/all`;
const params = new URLSearchParams();
if (page) params.append("page", page);
if (limit) params.append("limit", limit);
if (params.toString()) {
url += `?${params.toString()}`;
}
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Failed to fetch music");
}
return await response.json();
} catch (error) {
console.error("Error fetching all music:", error);
throw error;
}
}
// Function to get categories
async function getCategories() {
try {
const response = await fetch(`${BASE_URL}/api/get/category/all`);
if (!response.ok) {
throw new Error("Failed to fetch categories");
}
return await response.json();
} catch (error) {
console.error("Error fetching categories:", error);
throw error;
}
}
// Function to get music by category with pagination
async function getMusicByCategory(category, page = 1, limit = 10) {
try {
const response = await fetch(`${BASE_URL}/api/get/category/${category}?page=${page}&limit=${limit}`);
if (!response.ok) {
throw new Error("Failed to fetch music from category");
}
return await response.json();
} catch (error) {
console.error("Error fetching music from category:", error);
throw error;
}
}
// Function to get a specific music file by name
async function getMusicByFileName(fileName) {
try {
const response = await fetch(`${BASE_URL}/api/get/music/${encodeURIComponent(fileName)}`);
if (!response.ok) {
throw new Error("Failed to fetch music file");
}
return await response.json();
} catch (error) {
console.error("Error fetching music file:", error);
throw error;
}
}
export {
getMusicStore,
getAllMusic,
getCategories,
getMusicByCategory,
getMusicByFileName,
};