mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
refactor(notes): Refactor to new promise event format
This commit is contained in:
1 parent
bb81ab6fe2
commit
44a9595463
16 files changed
+394
-332
No files matched your search
@@ -1,32 +1,36 @@
|
||||
import { Note, NotesEvents } from '../../../typings/notes';
|
||||
import { getSource } from '../utils/miscUtils';
|
||||
import { BeforeDBNote, DeleteNoteDTO, NoteItem, NotesEvents } from '../../../typings/notes';
|
||||
import NotesService from './notes.service';
|
||||
import { notesLogger } from './notes.utils';
|
||||
import { onNetPromise } from '../utils/PromiseNetEvents/onNetPromise';
|
||||
|
||||
onNet(NotesEvents.ADD_NOTE, (note: Note) => {
|
||||
const src = getSource();
|
||||
NotesService.handleAddNote(src, note).catch((e) =>
|
||||
notesLogger.error(`Error occured in add note event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
onNetPromise<BeforeDBNote, NoteItem>(NotesEvents.ADD_NOTE, (reqObj, resp) => {
|
||||
NotesService.handleAddNote(reqObj, resp).catch((e) => {
|
||||
notesLogger.error(`Error occured in add note event (${reqObj.source}), Error: ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'UNKNOWN_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(NotesEvents.FETCH_ALL_NOTES, async (limit?: number) => {
|
||||
const src = getSource();
|
||||
NotesService.handleFetchNotes(src, limit).catch((e) =>
|
||||
notesLogger.error(`Error occurred in fetch note event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
onNetPromise<void, NoteItem[]>(NotesEvents.FETCH_ALL_NOTES, (reqObj, resp) => {
|
||||
NotesService.handleFetchNotes(reqObj, resp).catch((e) => {
|
||||
notesLogger.error(
|
||||
`Error occurred in fetch note event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'UNKNOWN_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(NotesEvents.DELETE_NOTE, async (noteId: number) => {
|
||||
const src = getSource();
|
||||
NotesService.handleDeleteNote(src, noteId).catch((e) =>
|
||||
notesLogger.error(`Error occured in delete note event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
onNetPromise<DeleteNoteDTO, DeleteNoteDTO>(NotesEvents.DELETE_NOTE, async (reqObj, resp) => {
|
||||
NotesService.handleDeleteNote(reqObj, resp).catch((e) => {
|
||||
notesLogger.error(
|
||||
`Error occured in delete note event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'UNKNOWN_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(NotesEvents.UPDATE_NOTE, async (note: Note) => {
|
||||
const src = getSource();
|
||||
NotesService.handleUpdateNote(src, note).catch((e) =>
|
||||
notesLogger.error(`Error occured in fetch note event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
onNetPromise<NoteItem, void>(NotesEvents.UPDATE_NOTE, async (reqObj, resp) => {
|
||||
NotesService.handleUpdateNote(reqObj, resp).catch((e) => {
|
||||
notesLogger.error(`Error occured in fetch note event (${reqObj.source}), Error: ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'UNKNOWN_ERROR' });
|
||||
});
|
||||
});
|
||||
@@ -1,20 +1,19 @@
|
||||
import { Note } from '../../../typings/notes';
|
||||
import { BeforeDBNote, NoteItem } from '../../../typings/notes';
|
||||
import { pool } from '../db';
|
||||
import { FetchDefaultLimits } from '../utils/ServerConstants';
|
||||
import { ResultSetHeader } from 'mysql2';
|
||||
|
||||
export class _NotesDB {
|
||||
async addNote(identifier: string, note: Note): Promise<void> {
|
||||
async addNote(identifier: string, note: BeforeDBNote): Promise<number> {
|
||||
const query = 'INSERT INTO npwd_notes (identifier, title, content) VALUES (?, ?, ?)';
|
||||
await pool.query(query, [identifier, note.title, note.content]);
|
||||
const [result] = await pool.query(query, [identifier, note.title, note.content]);
|
||||
return (<ResultSetHeader>result).insertId;
|
||||
}
|
||||
|
||||
async fetchNotes(
|
||||
identifier: string,
|
||||
limit = FetchDefaultLimits.NOTES_FETCH_LIMIT,
|
||||
): Promise<Note[]> {
|
||||
const query = 'SELECT * FROM npwd_notes WHERE identifier = ? ORDER BY id DESC LIMIT ?';
|
||||
const [result] = await pool.query(query, [identifier, limit]);
|
||||
return <Note[]>result;
|
||||
async fetchNotes(identifier: string): Promise<NoteItem[]> {
|
||||
const query = 'SELECT * FROM npwd_notes WHERE identifier = ? ORDER BY id DESC';
|
||||
const [result] = await pool.query(query, [identifier]);
|
||||
return <NoteItem[]>result;
|
||||
}
|
||||
|
||||
async deleteNote(noteId: number, identifier: string): Promise<void> {
|
||||
@@ -22,7 +21,7 @@ export class _NotesDB {
|
||||
await pool.query(query, [noteId, identifier]);
|
||||
}
|
||||
|
||||
async updateNote(note: Note, identifier: string): Promise<void> {
|
||||
async updateNote(note: NoteItem, identifier: string): Promise<void> {
|
||||
const query = 'UPDATE npwd_notes SET title = ?, content = ? WHERE id = ? AND identifier = ?';
|
||||
await pool.query(query, [note.title, note.content, note.id, identifier]);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Note, NotesEvents } from '../../../typings/notes';
|
||||
import { BeforeDBNote, DeleteNoteDTO, NoteItem } from '../../../typings/notes';
|
||||
import PlayerService from '../players/player.service';
|
||||
import NotesDB, { _NotesDB } from './notes.db';
|
||||
import { notesLogger } from './notes.utils';
|
||||
import { PromiseEventResp, PromiseRequest } from '../utils/PromiseNetEvents/promise.types';
|
||||
|
||||
class _NotesService {
|
||||
private readonly notesDB: _NotesDB;
|
||||
@@ -11,57 +12,47 @@ class _NotesService {
|
||||
notesLogger.debug('Notes service started');
|
||||
}
|
||||
|
||||
async handleAddNote(src: number, note: Note): Promise<void> {
|
||||
async handleAddNote(
|
||||
reqObj: PromiseRequest<BeforeDBNote>,
|
||||
resp: PromiseEventResp<NoteItem>,
|
||||
): Promise<void> {
|
||||
notesLogger.debug('Handling add note, note:');
|
||||
notesLogger.debug(note);
|
||||
notesLogger.debug(reqObj.data);
|
||||
|
||||
const identifer = PlayerService.getIdentifier(src);
|
||||
const identifer = PlayerService.getIdentifier(reqObj.source);
|
||||
|
||||
try {
|
||||
await this.notesDB.addNote(identifer, note);
|
||||
|
||||
emitNet(NotesEvents.SEND_NOTE_SUCCESS, src);
|
||||
emitNet(NotesEvents.ACTION_RESULT, src, {
|
||||
message: 'NOTES_ADD_SUCCESS',
|
||||
type: 'success',
|
||||
const noteId = await this.notesDB.addNote(identifer, reqObj.data);
|
||||
resp({
|
||||
status: 'ok',
|
||||
data: { id: noteId, content: reqObj.data.content, title: reqObj.data.title },
|
||||
});
|
||||
} catch (e) {
|
||||
notesLogger.error(`Error in handleAddNote, ${e.message}`);
|
||||
|
||||
emitNet(NotesEvents.ACTION_RESULT, src, {
|
||||
message: 'NOTES_ADD_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleFetchNotes(src: number, limit?: number) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleFetchNotes(reqObj: PromiseRequest<void>, resp: PromiseEventResp<NoteItem[]>) {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
const notes = await this.notesDB.fetchNotes(identifier, limit);
|
||||
emitNet(NotesEvents.SEND_NOTE, src, notes);
|
||||
const notes = await this.notesDB.fetchNotes(identifier);
|
||||
resp({ status: 'ok', data: notes });
|
||||
} catch (e) {
|
||||
notesLogger.error(`Error in handleFetchNote, ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleUpdateNote(src: number, note: Note) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleUpdateNote(reqObj: PromiseRequest<NoteItem>, resp: PromiseEventResp<void>) {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
await this.notesDB.updateNote(note, identifier);
|
||||
emitNet(NotesEvents.UPDATE_NOTE_SUCCESS, src);
|
||||
|
||||
emitNet(NotesEvents.ACTION_RESULT, src, {
|
||||
message: 'NOTES_UPDATE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
await this.notesDB.updateNote(reqObj.data, identifier);
|
||||
resp({ status: 'ok' });
|
||||
} catch (e) {
|
||||
emitNet(NotesEvents.UPDATE_NOTE_FAILURE, src);
|
||||
emitNet(NotesEvents.ACTION_RESULT, src, {
|
||||
message: 'NOTES_UPDATE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
notesLogger.error(`Error in handleUpdateNote, ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,23 +60,17 @@ class _NotesService {
|
||||
// // To implement someday
|
||||
// }
|
||||
|
||||
async handleDeleteNote(src: number, noteId: number) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleDeleteNote(
|
||||
reqObj: PromiseRequest<DeleteNoteDTO>,
|
||||
resp: PromiseEventResp<DeleteNoteDTO>,
|
||||
) {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
await this.notesDB.deleteNote(noteId, identifier);
|
||||
|
||||
emitNet(NotesEvents.DELETE_NOTE_SUCCESS, src);
|
||||
|
||||
emitNet(NotesEvents.ACTION_RESULT, src, {
|
||||
message: 'NOTES_DELETE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
await this.notesDB.deleteNote(reqObj.data.id, identifier);
|
||||
resp({ status: 'ok', data: reqObj.data });
|
||||
} catch (e) {
|
||||
notesLogger.error(`Error in handleDeleteNote, ${e.message}`);
|
||||
emitNet(NotesEvents.ACTION_RESULT, src, {
|
||||
message: 'NOTES_DELETE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user