diff --git a/resources/server/photo/photo.controller.ts b/resources/server/photo/photo.controller.ts new file mode 100644 index 0000000..984034d --- /dev/null +++ b/resources/server/photo/photo.controller.ts @@ -0,0 +1,25 @@ +import { getSource } from '../utils/miscUtils'; +import { GalleryPhoto, PhotoEvents } from '../../../typings/photo'; +import PhotoService from './photo.service'; +import { photoLogger } from './photo.utils'; + +onNet(PhotoEvents.UPLOAD_PHOTO, async (image: string) => { + const src = getSource(); + PhotoService.handleUploadPhoto(src, image).catch((e) => + photoLogger.error(`Error occurred in upload photo event (${src}), Error: ${e.message}`), + ); +}); + +onNet(PhotoEvents.FETCH_PHOTOS, async () => { + const src = getSource(); + PhotoService.handleFetchPhotos(src).catch((e) => + photoLogger.error(`Error occurred in upload photo event (${src}), Error: ${e.message}`), + ); +}); + +onNet(PhotoEvents.DELETE_PHOTO, async (photo: GalleryPhoto) => { + const src = getSource(); + PhotoService.handleDeletePhoto(src, photo).catch((e) => + photoLogger.error(`Error occurred in delete photo event (${src}), Error: ${e.message}`), + ); +}); diff --git a/resources/server/photo/photo.db.ts b/resources/server/photo/photo.db.ts new file mode 100644 index 0000000..95b19b0 --- /dev/null +++ b/resources/server/photo/photo.db.ts @@ -0,0 +1,25 @@ +import { GalleryPhoto } from '../../../typings/photo'; +import { pool } from '../db'; + +export class _PhotoDB { + async uploadPhoto(identifier: string, image: string): Promise { + const query = 'INSERT INTO npwd_phone_gallery (identifier, image) VALUES (?, ?)'; + const [results] = (await pool.query(query, [identifier, image])) as any; + return { id: results.insertId, image }; + } + + async getPhotosByIdentifier(identifier: string): Promise { + const query = 'SELECT id, image FROM npwd_phone_gallery WHERE identifier = ? ORDER BY id DESC'; + const [results] = await pool.query(query, [identifier]); + return results; + } + + async deletePhoto(photo: GalleryPhoto, identifier: string) { + const query = 'DELETE FROM npwd_phone_gallery WHERE image = ? AND identifier = ?'; + await pool.query(query, [photo.image, identifier]); + } +} + +const PhotoDB = new _PhotoDB(); + +export default PhotoDB; diff --git a/resources/server/photo/photo.service.ts b/resources/server/photo/photo.service.ts new file mode 100644 index 0000000..cf913b5 --- /dev/null +++ b/resources/server/photo/photo.service.ts @@ -0,0 +1,53 @@ +import PlayerService from '../players/player.service'; +import { GalleryPhoto, PhotoEvents } from '../../../typings/photo'; +import PhotoDB, { _PhotoDB } from './photo.db'; +import { photoLogger } from './photo.utils'; + +class _PhotoService { + private readonly photoDB: _PhotoDB; + + constructor() { + this.photoDB = PhotoDB; + photoLogger.debug('Photo service started'); + } + + async handleUploadPhoto(src: number, image: string) { + try { + const identifier = PlayerService.getIdentifier(src); + const photo = await this.photoDB.uploadPhoto(identifier, image); + emitNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, src, photo); + } catch (e) { + photoLogger.error(`Failed to upload photo, ${e.message}`, { + source: src, + }); + } + } + + async handleFetchPhotos(src: number) { + try { + const identifier = PlayerService.getIdentifier(src); + const photos = await this.photoDB.getPhotosByIdentifier(identifier); + emitNet(PhotoEvents.SEND_PHOTOS, src, photos); + } catch (e) { + photoLogger.error(`Failed to fetch photos, ${e.message}`, { + source: src, + }); + } + } + + async handleDeletePhoto(src: number, photo: GalleryPhoto) { + try { + const identifier = PlayerService.getIdentifier(src); + await this.photoDB.deletePhoto(photo, identifier); + emitNet(PhotoEvents.DELETE_PHOTO_SUCCESS, src); + } catch (e) { + photoLogger.error(`Failed to fetch photos, ${e.message}`, { + source: src, + }); + } + } +} + +const PhotoService = new _PhotoService(); + +export default PhotoService; diff --git a/resources/server/photo/photo.utils.ts b/resources/server/photo/photo.utils.ts new file mode 100644 index 0000000..e0f5584 --- /dev/null +++ b/resources/server/photo/photo.utils.ts @@ -0,0 +1,3 @@ +import { mainLogger } from '../sv_logger'; + +export const photoLogger = mainLogger.child({ module: 'photo' }); diff --git a/resources/server/sv_photo.ts b/resources/server/sv_photo.ts deleted file mode 100644 index 2f308d2..0000000 --- a/resources/server/sv_photo.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { pool } from './db'; -import { GalleryPhoto, PhotoEvents } from '../../typings/photo'; -import { mainLogger } from './sv_logger'; -import { getSource } from './utils/miscUtils'; -import PlayerService from './players/player.service'; - -const photoLogger = mainLogger.child({ module: 'photo' }); - -async function uploadPhoto(identifier: string, image: string): Promise { - const query = 'INSERT INTO npwd_phone_gallery (identifier, image) VALUES (?, ?)'; - const [results] = (await pool.query(query, [identifier, image])) as any; - return { id: results.insertId, image }; -} - -async function getPhotosByIdentifier(identifier: string): Promise { - const query = 'SELECT id, image FROM npwd_phone_gallery WHERE identifier = ? ORDER BY id DESC'; - const [results] = await pool.query(query, [identifier]); - return results; -} - -async function deletePhoto(photo: GalleryPhoto, identifier: string) { - const query = 'DELETE FROM npwd_phone_gallery WHERE image = ? AND identifier = ?'; - await pool.query(query, [photo.image, identifier]); -} - -onNet(PhotoEvents.UPLOAD_PHOTO, async (image: string) => { - const _source = getSource(); - try { - const identifier = PlayerService.getIdentifier(_source); - const photo = await uploadPhoto(identifier, image); - emitNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, _source, photo); - } catch (e) { - photoLogger.error(`Failed to upload photo, ${e.message}`, { - source: _source, - }); - } -}); - -onNet(PhotoEvents.FETCH_PHOTOS, async () => { - const _source = getSource(); - try { - const identifier = PlayerService.getIdentifier(_source); - const photos = await getPhotosByIdentifier(identifier); - emitNet(PhotoEvents.SEND_PHOTOS, _source, photos); - } catch (e) { - photoLogger.error(`Failed to fetch photos, ${e.message}`, { - source: _source, - }); - } -}); - -onNet(PhotoEvents.DELETE_PHOTO, async (photo: GalleryPhoto) => { - const _source = getSource(); - try { - const identifier = PlayerService.getIdentifier(_source); - await deletePhoto(photo, identifier); - emitNet(PhotoEvents.DELETE_PHOTO_SUCCESS, _source); - } catch (e) { - photoLogger.error(`Failed to fetch photos, ${e.message}`, { - source: _source, - }); - } -});