mirror of
https://github.com/KeyZox71/knl_meowscendence.git
synced 2025-12-31 21:56:41 +01:00
merged image API in user API
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import fastifyJWT from '@fastify/jwt';
|
||||
import fastifyCookie from '@fastify/cookie';
|
||||
import Database from 'better-sqlite3';
|
||||
import multipart from '@fastify/multipart';
|
||||
|
||||
import { gUsers } from './gUsers.js';
|
||||
import { gUser } from './gUser.js';
|
||||
@ -20,6 +21,7 @@ import { dFriend } from './dFriend.js';
|
||||
import { dMatchHistory } from './dMatchHistory.js';
|
||||
import { pAvatar } from './pAvatar.js';
|
||||
import { gAvatar } from './gAvatar.js';
|
||||
import { uAvatar } from './uAvatar.js';
|
||||
import { dAvatar } from './dAvatar.js';
|
||||
import { pPing } from './pPing.js';
|
||||
import { gPing } from './gPing.js';
|
||||
@ -80,6 +82,14 @@ function prepareDB() {
|
||||
time TEXT
|
||||
) STRICT
|
||||
`);
|
||||
database.exec(`
|
||||
CREATE TABLE IF NOT EXISTS images (
|
||||
imageId INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
fileName TEXT,
|
||||
mimeType TEXT,
|
||||
data BLOB
|
||||
) STRICT
|
||||
`);
|
||||
}
|
||||
|
||||
prepareDB();
|
||||
@ -93,6 +103,7 @@ const incLossesPong = database.prepare('UPDATE userData SET pongLosses = pongLos
|
||||
const incWinsTetris = database.prepare('UPDATE userData SET tetrisWins = tetrisWins + 1 WHERE username = ?;');
|
||||
const incLossesTetris = database.prepare('UPDATE userData SET tetrisLosses = tetrisLosses + 1 WHERE username = ?');
|
||||
const setAvatarId = database.prepare('UPDATE userData SET avatarId = ? WHERE username = ?;');
|
||||
const postImage = database.prepare('INSERT INTO images (fileName, mimeType, data) VALUES (?, ?, ?);');
|
||||
const setActivityTime = database.prepare(`
|
||||
INSERT INTO activityTime (username, time)
|
||||
VALUES (?, ?)
|
||||
@ -113,6 +124,7 @@ const getNumberUsers = database.prepare('SELECT COUNT (DISTINCT username) AS n_u
|
||||
const getNumberFriends = database.prepare('SELECT COUNT (DISTINCT friendName) AS n_friends FROM friends WHERE username = ?;');
|
||||
const getNumberMatches = database.prepare('SELECT COUNT (DISTINCT id) AS n_matches FROM matchHistory WHERE game = ? AND ? IN (player1, player2);');
|
||||
const getAvatarId = database.prepare('SELECT avatarId FROM userData WHERE username = ?;');
|
||||
const getImage = database.prepare('SELECT fileName, mimeType, data FROM images WHERE imageId = ?;');
|
||||
const getActivityTime = database.prepare('SELECT time FROM activityTime WHERE username = ?;')
|
||||
|
||||
// DELETE
|
||||
@ -123,6 +135,7 @@ const deleteMatchHistory = database.prepare('DELETE FROM matchHistory WHERE game
|
||||
const deleteStatsPong = database.prepare('UPDATE userData SET pongWins = 0, pongLosses = 0 WHERE username = ?;');
|
||||
const deleteStatsTetris = database.prepare('UPDATE userData SET tetrisWins = 0, tetrisLosses = 0 WHERE username = ?;');
|
||||
const deleteAvatarId = database.prepare('UPDATE userData SET avatarId = -1 WHERE username = ?;');
|
||||
const deleteImage = database.prepare('DELETE FROM images WHERE imageId = ?;');
|
||||
|
||||
const querySchema = { type: 'object', required: ['iStart', 'iEnd'], properties: { iStart: { type: 'integer', minimum: 0 }, iEnd: { type: 'integer', minimum: 0 } } }
|
||||
const querySchemaMatchHistory = { type: 'object', required: ['game', 'iStart', 'iEnd'], properties: { game: { type: 'string' }, iStart: { type: 'integer', minimum: 0 }, iEnd: { type: 'integer', minimum: 0 } } }
|
||||
@ -137,6 +150,7 @@ export default async function(fastify, options) {
|
||||
},
|
||||
});
|
||||
fastify.register(fastifyCookie);
|
||||
fastify.register(multipart, { limits: { fileSize: 2 * 1024 * 1024 + 1 } });
|
||||
|
||||
fastify.decorate('authenticate', async function(request, reply) {
|
||||
try {
|
||||
@ -181,7 +195,7 @@ export default async function(fastify, options) {
|
||||
return gNumberMatches(request, reply, fastify, getUserInfo, getNumberMatches);
|
||||
});
|
||||
fastify.get('/users/:userId/avatar', { preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return gAvatar(request, reply, fastify, getUserInfo, getAvatarId);
|
||||
return gAvatar(request, reply, fastify, getUserInfo, getAvatarId, getImage);
|
||||
});
|
||||
fastify.get('/ping/:userId', { preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return gPing(request, reply, fastify, getActivityTime);
|
||||
@ -197,14 +211,17 @@ export default async function(fastify, options) {
|
||||
fastify.post('/users/:userId/matchHistory', { preHandler: [fastify.authenticate], schema: { body: bodySchemaMatchHistory } }, async (request, reply) => {
|
||||
return pMatchHistory(request, reply, fastify, getUserInfo, addMatch, incWinsPong, incLossesPong, incWinsTetris, incLossesTetris);
|
||||
});
|
||||
fastify.post('/users/:userId/avatar', { preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return pAvatar(request, reply, fastify, getUserInfo, setAvatarId);
|
||||
fastify.post('/users/:userId/avatar',/* { preHandler: [fastify.authenticate] },*/ async (request, reply) => {
|
||||
return pAvatar(request, reply, fastify, getUserInfo, setAvatarId, postImage);
|
||||
});
|
||||
fastify.post('/ping', { preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return pPing(request, reply, fastify, setActivityTime);
|
||||
})
|
||||
|
||||
// PATCH
|
||||
fastify.patch('/users/:userId/avatar', { preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return uAvatar(request, reply, fastify, getUserInfo, setAvatarId, getAvatarId, deleteAvatarId, postImage, deleteImage);
|
||||
});
|
||||
fastify.patch('/users/:userId/:member', { preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return uMember(request, reply, fastify, getUserInfo, changeDisplayName, changeAvatarId);
|
||||
});
|
||||
@ -226,6 +243,6 @@ export default async function(fastify, options) {
|
||||
return dMatchHistory(request, reply, fastify, getUserInfo, deleteMatchHistory, deleteStatsPong, deleteStatsTetris);
|
||||
});
|
||||
fastify.delete('/users/:userId/avatar', { preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return dAvatar(request, reply, fastify, getUserInfo, deleteAvatarId);
|
||||
return dAvatar(request, reply, fastify, getUserInfo, getAvatarId, deleteAvatarId, deleteImage);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user