tweak(notes/server): imports of interfaces

This commit is contained in:
chip committed 2020-11-01 19:26:24 +01:00
1 parent b085d6b708
commit 9fb4bb97cf
1 file changed
+34 -53
+34 -53
View File
@@ -1,91 +1,72 @@
import events from '../utils/events'; import events from "../utils/events";
import { ESX, getSource } from './server'; import { ESX, getSource } from "./server";
import { pool } from './db'; import { pool } from "./db";
import { useIdentifier } from './functions'; import { useIdentifier } from "./functions";
import { Note, NoteId } from "../../phone/src/common/interfaces/notes";
interface Note {
id?: number;
title: string;
content: string;
}
interface NoteId {
id: number
}
async function addNote(identifier: string, note: Note): Promise<any> { async function addNote(identifier: string, note: Note): Promise<any> {
const query = "INSERT INTO npwd_notes (identifier, title, content) VALUES (?, ?, ?)" const query =
await pool.query(query, "INSERT INTO npwd_notes (identifier, title, content) VALUES (?, ?, ?)";
[ await pool.query(query, [identifier, note.title, note.content]);
identifier,
note.title,
note.content
]
)
} }
async function fetchAllNotes(identifier: string): Promise<Note[]> { async function fetchAllNotes(identifier: string): Promise<Note[]> {
const query = "SELECT * FROM npwd_notes WHERE identifier = ? ORDER BY id DESC" const query =
const [ result ] = await pool.query(query, [identifier]); "SELECT * FROM npwd_notes WHERE identifier = ? ORDER BY id DESC";
const [result] = await pool.query(query, [identifier]);
const notes = <Note[]>result; const notes = <Note[]>result;
return notes; return notes;
} }
async function deleteNote(noteId: number, identifier: string) { async function deleteNote(noteId: number, identifier: string) {
const query = "DELETE FROM npwd_notes WHERE id = ? AND identifier = ?"; const query = "DELETE FROM npwd_notes WHERE id = ? AND identifier = ?";
await pool.query(query, [noteId, identifier]) await pool.query(query, [noteId, identifier]);
} }
async function updateNote(note: Note, identifier: string): Promise<any> { async function updateNote(note: Note, identifier: string): Promise<any> {
const query = "UPDATE npwd_notes SET title = ?, content = ? WHERE id = ? AND identifier = ?" const query =
await pool.query(query, [ "UPDATE npwd_notes SET title = ?, content = ? WHERE id = ? AND identifier = ?";
note.title, await pool.query(query, [note.title, note.content, note.id, identifier]);
note.content,
note.id,
identifier
])
} }
onNet(events.NOTE_ADD_NOTE, async (note: Note) => { onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
try { try {
console.log(note.content, note.title); const _identifier = await useIdentifier();
const _identifier = await useIdentifier() addNote(_identifier, note);
console.log(_identifier) } catch (error) {
addNote(_identifier, note) console.log("NOTES ERROR: ", error);
} }
catch(error) { });
console.log("NOTES ERROR: ", error)
}
})
onNet(events.NOTE_FETCH_ALL_NOTES, async () => { onNet(events.NOTE_FETCH_ALL_NOTES, async () => {
try { try {
const _source = (global as any).source; const _source = (global as any).source;
const _identifier = await useIdentifier() const _identifier = await useIdentifier();
const notes = await fetchAllNotes(_identifier); const notes = await fetchAllNotes(_identifier);
emitNet(events.NOTE_SEND_NOTE, _source, notes) emitNet(events.NOTE_SEND_NOTE, _source, notes);
emitNet(events.NOTE_SEND_NOTE_SUCCESS, _source);
} catch (error) { } catch (error) {
console.log("NOTE ERROR: ", error) console.log("NOTE ERROR: ", error);
} }
}) });
onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => { onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => {
try { try {
const _identifier = await useIdentifier(); const _identifier = await useIdentifier();
const id = noteId.id const id = noteId.id;
await deleteNote(id, _identifier) await deleteNote(id, _identifier);
} catch(error) { } catch (error) {
console.log("NOTE ERROR:", error) console.log("NOTE ERROR:", error);
} }
}) });
onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => { onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => {
const _source = (global as any).source; const _source = (global as any).source;
try { try {
const _identifier = await useIdentifier(); const _identifier = await useIdentifier();
updateNote(note, _identifier) updateNote(note, _identifier);
emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, _source) emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, _source);
} catch (error) { } catch (error) {
emitNet(events.NOTE_UPDATE_NOTE_FAILURE, _source) emitNet(events.NOTE_UPDATE_NOTE_FAILURE, _source);
} }
}) });