File size: 2,070 Bytes
58c1398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * db.js
 * API-based wrapper for Aadhaar History (Server-side SQLite)
 */

const dbService = {
    async init() {
        // No client-side initialization needed for API, but keeping the signature
        return Promise.resolve();
    },

    async getAll() {
        try {
            const response = await fetch('/api/history');
            if (!response.ok) throw new Error('API Error: ' + response.status);
            const history = await response.json();
            return history;
        } catch (e) {
            console.error('Failed to fetch history:', e);
            return [];
        }
    },

    async save(item) {
        try {
            const response = await fetch('/api/history', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(item)
            });
            if (!response.ok) throw new Error('API Error: ' + response.status);
            return await response.json();
        } catch (e) {
            console.error('Failed to save history:', e);
            throw e;
        }
    },

    async delete(id) {
        try {
            const response = await fetch(`/api/history?id=${encodeURIComponent(id)}`, {
                method: 'DELETE'
            });
            if (!response.ok) throw new Error('API Error: ' + response.status);
            return await response.json();
        } catch (e) {
            console.error('Failed to delete history:', e);
            throw e;
        }
    },

    async clear() {
        // Clearing all history not implemented in API yet for safety, 
        // but could be added if needed.
        console.warn('Clear all not supported via API currently.');
        return Promise.resolve();
    },

    async getLimit(limit = 50) {
        const all = await this.getAll();
        return all.slice(0, limit);
    },

    async enforceLimit(limit = 50) {
        // Enforce limit is handled by the server-side API automatically
        return Promise.resolve();
    }
};

window.dbService = dbService;