File size: 2,562 Bytes
11c56f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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,
};