network chunk v13

This commit is contained in:
andrei-mihnea-cerbu
2024-11-13 13:50:18 +02:00
parent 39d007e34f
commit 116676db77
5 changed files with 62 additions and 10 deletions
@@ -7,7 +7,6 @@ export class GeneralOperations implements OperationPlugin {
public static readonly operationCodes = { public static readonly operationCodes = {
OK: 'OK', OK: 'OK',
ERR: 'ERR', ERR: 'ERR',
END: 'END',
HEARTBEAT: 'HEARTBEAT', HEARTBEAT: 'HEARTBEAT',
ALIVE: 'ALIVE', ALIVE: 'ALIVE',
SET_PUBLIC_KEY: 'SET_PUBLIC_KEY', SET_PUBLIC_KEY: 'SET_PUBLIC_KEY',
@@ -31,9 +31,27 @@ export class KeyOperations implements OperationPlugin {
: { operationCode: operationCodes.ERR, metaInfo: { message: 'Key not found.' } }; : { operationCode: operationCodes.ERR, metaInfo: { message: 'Key not found.' } };
} }
public static async handleDeleteKey(parsedMessage: ParsedMessage): Promise<ParsedMessage> {
const { userId } = parsedMessage.metaInfo || {};
if (!userId) {
return {
operationCode: operationCodes.ERR,
metaInfo: { message: 'User ID is required for deletion.' },
};
}
const isSuccess = keyDatabase.deleteKeysByUserId(userId);
return {
operationCode: isSuccess ? operationCodes.OK : operationCodes.ERR,
metaInfo: { message: isSuccess ? 'Key deleted successfully.' : 'Failed to delete key.' },
};
}
public register(operationHandler: OperationHandler): void { public register(operationHandler: OperationHandler): void {
operationHandler.registerHandler(KeyOperations.operationCodes.GET_KEYS, KeyOperations.handleGetKeys); operationHandler.registerHandler(KeyOperations.operationCodes.GET_KEYS, KeyOperations.handleGetKeys);
operationHandler.registerHandler(KeyOperations.operationCodes.CREATE_KEY, KeyOperations.handleCreateKey); operationHandler.registerHandler(KeyOperations.operationCodes.CREATE_KEY, KeyOperations.handleCreateKey);
operationHandler.registerHandler(KeyOperations.operationCodes.FIND_KEY_BY_USER_ID, KeyOperations.handleFindKeyByUserId); operationHandler.registerHandler(KeyOperations.operationCodes.FIND_KEY_BY_USER_ID, KeyOperations.handleFindKeyByUserId);
operationHandler.registerHandler(KeyOperations.operationCodes.DELETE_KEY, KeyOperations.handleDeleteKey);
} }
} }
@@ -1,15 +1,15 @@
import { ParsedMessage } from '../message_handler'; import {ParsedMessage} from '../message_handler';
import { userDatabase } from '../../db_managers/db'; import {userDatabase} from '../../db_managers/db';
import { OperationHandler } from '../operations_base/operation_handler'; import {OperationHandler} from '../operations_base/operation_handler';
import { OperationPlugin } from '../operations_base/operation_plugin'; import {OperationPlugin} from '../operations_base/operation_plugin';
import { operationCodes} from "../operation_codes"; import {operationCodes} from "../operation_codes";
export class UserOperations implements OperationPlugin { export class UserOperations implements OperationPlugin {
public static readonly operationCodes = { public static readonly operationCodes = {
GET_USERS: 'GET_USERS',
CREATE_USER: 'CREATE_USER', CREATE_USER: 'CREATE_USER',
MODIFY_USER: 'MODIFY_USER', MODIFY_USER: 'MODIFY_USER',
DELETE_USER: 'DELETE_USER', DELETE_USER: 'DELETE_USER',
GET_USERS: 'GET_USERS',
FIND_BY_ID: 'FIND_BY_ID', FIND_BY_ID: 'FIND_BY_ID',
FIND_BY_EMAIL: 'FIND_BY_EMAIL', FIND_BY_EMAIL: 'FIND_BY_EMAIL',
}; };
@@ -35,9 +35,45 @@ export class UserOperations implements OperationPlugin {
: { operationCode: operationCodes.ERR, metaInfo: { message: 'Failed to modify user.' } }; : { operationCode: operationCodes.ERR, metaInfo: { message: 'Failed to modify user.' } };
} }
// Delete a user by ID
public static async handleDeleteUser(parsedMessage: ParsedMessage): Promise<ParsedMessage> {
const { id } = parsedMessage.metaInfo || {};
if (!id) {
return {
operationCode: operationCodes.ERR,
metaInfo: { message: 'User ID is required for deletion.' },
};
}
const isSuccess = userDatabase.deleteUser(id);
return {
operationCode: isSuccess ? operationCodes.OK : operationCodes.ERR,
metaInfo: { message: isSuccess ? 'User deleted successfully.' : 'Failed to delete user.' },
};
}
public static async handleGetUserByEmail(parsedMessage: ParsedMessage) : Promise<ParsedMessage>{
const { email } = parsedMessage.metaInfo || {};
if(!email){
return {
operationCode: operationCodes.ERR,
metaInfo: { message: 'Email is required.' },
};
}
const user = userDatabase.findByEmail(email);
return {
operationCode: user === null ? operationCodes.ERR : operationCodes.OK,
metaInfo: user === null ? {message: 'User not found.'} : user
};
}
public register(operationHandler: OperationHandler): void { public register(operationHandler: OperationHandler): void {
operationHandler.registerHandler(UserOperations.operationCodes.GET_USERS, UserOperations.handleGetUsers);
operationHandler.registerHandler(UserOperations.operationCodes.MODIFY_USER, UserOperations.handleModifyUser); operationHandler.registerHandler(UserOperations.operationCodes.MODIFY_USER, UserOperations.handleModifyUser);
operationHandler.registerHandler(UserOperations.operationCodes.DELETE_USER, UserOperations.handleDeleteUser);
operationHandler.registerHandler(UserOperations.operationCodes.GET_USERS, UserOperations.handleGetUsers);
operationHandler.registerHandler(UserOperations.operationCodes.FIND_BY_ID, UserOperations.handleGetUserById); operationHandler.registerHandler(UserOperations.operationCodes.FIND_BY_ID, UserOperations.handleGetUserById);
operationHandler.registerHandler(UserOperations.operationCodes.FIND_BY_EMAIL, UserOperations.handleGetUserByEmail);
} }
} }
+1 -1
View File
@@ -50,7 +50,7 @@ export class UdpServer {
const communicator = new UdpSocketCommunicator(this.udpServer, ip, port, this.operationHandler); const communicator = new UdpSocketCommunicator(this.udpServer, ip, port, this.operationHandler);
communicator.handleIncomingMessage(msg.toString()); await communicator.handleIncomingMessage(msg.toString());
const communicatorResult = communicator.getHandlerResult(); const communicatorResult = communicator.getHandlerResult();
if (communicatorResult) { if (communicatorResult) {
@@ -7,7 +7,6 @@ export class GeneralOperations implements OperationPlugin {
public static readonly operationCodes = { public static readonly operationCodes = {
OK: 'OK', OK: 'OK',
ERR: 'ERR', ERR: 'ERR',
END: 'END',
HEARTBEAT: 'HEARTBEAT', HEARTBEAT: 'HEARTBEAT',
ALIVE: 'ALIVE', ALIVE: 'ALIVE',
SET_PUBLIC_KEY: 'SET_PUBLIC_KEY', SET_PUBLIC_KEY: 'SET_PUBLIC_KEY',