mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 22:16:54 +00:00
refactor(resources/photo): refactor photos
This commit is contained in:
1 parent
3470700fc1
commit
10e28b911e
5 files changed
+106
-63
No files matched your search
@@ -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}`),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { GalleryPhoto } from '../../../typings/photo';
|
||||
import { pool } from '../db';
|
||||
|
||||
export class _PhotoDB {
|
||||
async uploadPhoto(identifier: string, image: string): Promise<GalleryPhoto> {
|
||||
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<GalleryPhoto[]> {
|
||||
const query = 'SELECT id, image FROM npwd_phone_gallery WHERE identifier = ? ORDER BY id DESC';
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
return <GalleryPhoto[]>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;
|
||||
@@ -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;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { mainLogger } from '../sv_logger';
|
||||
|
||||
export const photoLogger = mainLogger.child({ module: 'photo' });
|
||||
@@ -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<GalleryPhoto> {
|
||||
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<GalleryPhoto[]> {
|
||||
const query = 'SELECT id, image FROM npwd_phone_gallery WHERE identifier = ? ORDER BY id DESC';
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
return <GalleryPhoto[]>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,
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user