mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 14:38:45 +00:00
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import { pool } from '../db';
|
|
import { Contact, PreDBContact } from '../../../typings/contact';
|
|
import { ResultSetHeader } from 'mysql2';
|
|
|
|
export class _ContactsDB {
|
|
async fetchAllContacts(identifier: string): Promise<Contact[]> {
|
|
const query = 'SELECT * FROM npwd_phone_contacts WHERE identifier = ? ORDER BY display ASC';
|
|
const [results] = await pool.query(query, [identifier]);
|
|
return <Contact[]>results;
|
|
}
|
|
|
|
async addContact(
|
|
identifier: string,
|
|
{ display, avatar, number }: PreDBContact,
|
|
): Promise<Contact> {
|
|
const query =
|
|
'INSERT INTO npwd_phone_contacts (identifier, number, display, avatar) VALUES (?, ?, ?, ?)';
|
|
|
|
const [setResult] = await pool.query(query, [identifier, number, display, avatar]);
|
|
|
|
return {
|
|
id: (<ResultSetHeader>setResult).insertId,
|
|
number,
|
|
avatar,
|
|
display,
|
|
};
|
|
}
|
|
|
|
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;
|