refactor(notes): Refactor to new promise event format

This commit is contained in:
Taso committed 2021-06-08 23:10:16 -04:00
1 parent bb81ab6fe2
commit 44a9595463
16 files changed
+394 -332

No files matched your search

+31 -46
View File
@@ -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' });
}
}
}