Files
new-phone-who-dis/resources/server/sv_photo.ts
T
Taso 0f61088999 refactor(events): Events are now tied to a common enum typings (#167)
* chore(scripts/twitter): Refactored twitter to use event enums

* chore(scripts/marketplace): Refactored marketplace to use event enums

* chore(scripts/messages): Refactored messages to use event enums

* chore(scripts/contacts): Refactored contacts to use event enums

* chore(scripts/calls): Refactored calls to use event enums

* chore(scripts/notes): Refactored notes to use event enums

* chore(scripts/photos): Refactored photos to use event enums

* chore(scripts/bank): Refactored bank to use event enums

* chore(scripts/phone): Refactored phone to use event enums

* chore(events): Removed old events object

* refactor(phone/nui-events): Attached NUI events to respective enum

* tweak(event-enums): Added npwd prefix to all events

* tweak(phone/serve): Added craco to override CRA without eject

Also bumped react-scripts to 4.0.1, with seemingly no issues

* fix(typings/twitter): Fixed method enums for twitter
2021-03-22 13:37:07 -04:00

62 lines
2.0 KiB
TypeScript

import { pool } from './db';
import { getIdentifier, getSource } from './functions';
import { IPhoto, PhotoEvents } from '../../typings/photo';
import { mainLogger } from './sv_logger';
const photoLogger = mainLogger.child({ module: 'photo' });
async function uploadPhoto(identifier: string, image: string) {
const query = 'INSERT INTO npwd_phone_gallery (identifier, image) VALUES (?, ?)';
await pool.query(query, [identifier, image]);
}
async function getPhotosByIdentifier(identifier: string): Promise<IPhoto[]> {
const query = 'SELECT id, image FROM npwd_phone_gallery WHERE identifier = ? ORDER BY id DESC';
const [results] = await pool.query(query, [identifier]);
return <IPhoto[]>results;
}
async function deletePhoto(photo: IPhoto, 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 = getIdentifier(_source);
await uploadPhoto(identifier, image);
emitNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, _source);
} catch (e) {
photoLogger.error(`Failed to upload photo, ${e.message}`, {
source: _source,
});
}
});
onNet(PhotoEvents.FETCH_PHOTOS, async () => {
const _source = getSource();
try {
const identifier = 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: IPhoto) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await deletePhoto(photo, identifier);
emitNet(PhotoEvents.DELETE_PHOTO_SUCCESS, _source);
} catch (e) {
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
source: _source,
});
}
});