mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
refactor(server/contacts): Implement singleton architecture
This commit is contained in:
1 parent
16471ed1df
commit
f8f722c8d2
9 files changed
+200
-136
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { pool } from '../db';
|
||||
import { Contact, PreDBContact } from '../../../typings/contact';
|
||||
import { FetchDefaultLimits } from '../utils/ServerConstants';
|
||||
|
||||
export class _ContactsDB {
|
||||
async fetchAllContacts(
|
||||
identifier: string,
|
||||
limit = FetchDefaultLimits.CONTACTS_FETCH_LIMIT,
|
||||
): Promise<Contact[]> {
|
||||
const query =
|
||||
'SELECT * FROM npwd_phone_contacts WHERE identifier = ? ORDER BY display ASC LIMIT ?';
|
||||
const [results] = await pool.query(query, [identifier, limit]);
|
||||
return <Contact[]>results;
|
||||
}
|
||||
|
||||
async addContact(identifier: string, { display, avatar, number }: PreDBContact): Promise<void> {
|
||||
const query =
|
||||
'INSERT INTO npwd_phone_contacts (identifier, number, display, avatar) VALUES (?, ?, ?, ?)';
|
||||
|
||||
await pool.query(query, [identifier, number, display, avatar]);
|
||||
}
|
||||
|
||||
async updateContact(contact: Contact, identifier: string): Promise<any> {
|
||||
const query =
|
||||
'UPDATE npwd_phone_contacts SET number = ?, display = ?, avatar = ? WHERE id = ? AND identifier = ?';
|
||||
await pool.query(query, [
|
||||
contact.number,
|
||||
contact.display,
|
||||
contact.avatar,
|
||||
contact.id,
|
||||
identifier,
|
||||
]);
|
||||
}
|
||||
|
||||
async deleteContact(contactId: number, identifier: string): Promise<void> {
|
||||
const query = 'DELETE FROM npwd_phone_contacts WHERE id = ? AND identifier = ?';
|
||||
await pool.query(query, [contactId, identifier]);
|
||||
}
|
||||
}
|
||||
|
||||
const ContactsDB = new _ContactsDB();
|
||||
export default ContactsDB;
|
||||
Reference in new issue
Block a user