- Introduce IPC handlers in `ipc/character.ipc.ts` and `ipc/location.ipc.ts` for CRUD operations, attributes management, and related actions. - Register character and location IPC modules in `main.ts` for global accessibility. - Utilize `createHandler` pattern for consistent and modular IPC handler implementation. - Enhance type definitions for character and location functionalities, improving code consistency.
66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
import { ipcMain } from 'electron';
|
|
import { createHandler } from '../database/LocalSystem.js';
|
|
import Character from '../database/models/Character.js';
|
|
import type { CharacterProps, CharacterPropsPost, CharacterAttribute } from '../database/models/Character.js';
|
|
|
|
interface AddCharacterData {
|
|
character: CharacterPropsPost;
|
|
bookId: string;
|
|
}
|
|
|
|
interface AddAttributeData {
|
|
characterId: string;
|
|
type: string;
|
|
name: string;
|
|
}
|
|
|
|
// GET /character/list - Get character list
|
|
ipcMain.handle('db:character:list', createHandler<string, CharacterProps[]>(
|
|
function(userId: string, bookId: string, lang: 'fr' | 'en'): CharacterProps[] {
|
|
return Character.getCharacterList(userId, bookId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// GET /character/attribute - Get character attributes
|
|
ipcMain.handle('db:character:attributes', createHandler<string, CharacterAttribute[]>(
|
|
function(userId: string, characterId: string, lang: 'fr' | 'en'): CharacterAttribute[] {
|
|
return Character.getAttributes(characterId, userId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// POST /character/add - Add new character
|
|
ipcMain.handle('db:character:create', createHandler<AddCharacterData, string>(
|
|
function(userId: string, data: AddCharacterData, lang: 'fr' | 'en'): string {
|
|
return Character.addNewCharacter(userId, data.character, data.bookId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// POST /character/attribute/add - Add attribute to character
|
|
ipcMain.handle('db:character:attribute:add', createHandler<AddAttributeData, string>(
|
|
function(userId: string, data: AddAttributeData, lang: 'fr' | 'en'): string {
|
|
return Character.addNewAttribute(data.characterId, userId, data.type, data.name, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// DELETE /character/attribute/delete - Delete character attribute
|
|
ipcMain.handle('db:character:attribute:delete', createHandler<string, any>(
|
|
function(userId: string, attributeId: string, lang: 'fr' | 'en') {
|
|
return Character.deleteAttribute(userId, attributeId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// POST /character/update - Update character
|
|
ipcMain.handle('db:character:update', createHandler<CharacterPropsPost, boolean>(
|
|
function(userId: string, character: CharacterPropsPost, lang: 'fr' | 'en'): boolean {
|
|
return Character.updateCharacter(userId, character, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
console.log('[IPC] Character handlers registered');
|