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 = {
OK: 'OK',
ERR: 'ERR',
END: 'END',
HEARTBEAT: 'HEARTBEAT',
ALIVE: 'ALIVE',
SET_PUBLIC_KEY: 'SET_PUBLIC_KEY',
@@ -31,9 +31,27 @@ export class KeyOperations implements OperationPlugin {
: { 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 {
operationHandler.registerHandler(KeyOperations.operationCodes.GET_KEYS, KeyOperations.handleGetKeys);
operationHandler.registerHandler(KeyOperations.operationCodes.CREATE_KEY, KeyOperations.handleCreateKey);
operationHandler.registerHandler(KeyOperations.operationCodes.FIND_KEY_BY_USER_ID, KeyOperations.handleFindKeyByUserId);
operationHandler.registerHandler(KeyOperations.operationCodes.DELETE_KEY, KeyOperations.handleDeleteKey);
}
}
@@ -6,10 +6,10 @@ import { operationCodes} from "../operation_codes";
export class UserOperations implements OperationPlugin {
public static readonly operationCodes = {
GET_USERS: 'GET_USERS',
CREATE_USER: 'CREATE_USER',
MODIFY_USER: 'MODIFY_USER',
DELETE_USER: 'DELETE_USER',
GET_USERS: 'GET_USERS',
FIND_BY_ID: 'FIND_BY_ID',
FIND_BY_EMAIL: 'FIND_BY_EMAIL',
};
@@ -35,9 +35,45 @@ export class UserOperations implements OperationPlugin {
: { 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 {
operationHandler.registerHandler(UserOperations.operationCodes.GET_USERS, UserOperations.handleGetUsers);
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_EMAIL, UserOperations.handleGetUserByEmail);
}
}
+1 -1
View File
@@ -50,7 +50,7 @@ export class UdpServer {
const communicator = new UdpSocketCommunicator(this.udpServer, ip, port, this.operationHandler);
communicator.handleIncomingMessage(msg.toString());
await communicator.handleIncomingMessage(msg.toString());
const communicatorResult = communicator.getHandlerResult();
if (communicatorResult) {
@@ -7,7 +7,6 @@ export class GeneralOperations implements OperationPlugin {
public static readonly operationCodes = {
OK: 'OK',
ERR: 'ERR',
END: 'END',
HEARTBEAT: 'HEARTBEAT',
ALIVE: 'ALIVE',
SET_PUBLIC_KEY: 'SET_PUBLIC_KEY',