Add ipc/character.ipc.ts and ipc/location.ipc.ts for character and location database interactions
- 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.
This commit is contained in:
65
electron/ipc/character.ipc.ts
Normal file
65
electron/ipc/character.ipc.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
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');
|
||||
89
electron/ipc/location.ipc.ts
Normal file
89
electron/ipc/location.ipc.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import { createHandler } from '../database/LocalSystem.js';
|
||||
import Location from '../database/models/Location.js';
|
||||
import type { LocationProps } from '../database/models/Location.js';
|
||||
|
||||
interface AddLocationSectionData {
|
||||
locationName: string;
|
||||
bookId: string;
|
||||
}
|
||||
|
||||
interface AddLocationElementData {
|
||||
locationId: string;
|
||||
elementName: string;
|
||||
}
|
||||
|
||||
interface AddLocationSubElementData {
|
||||
elementId: string;
|
||||
subElementName: string;
|
||||
}
|
||||
|
||||
interface UpdateLocationData {
|
||||
locations: LocationProps[];
|
||||
}
|
||||
|
||||
// GET /location/all - Get all locations
|
||||
ipcMain.handle('db:location:all', createHandler<string, LocationProps[]>(
|
||||
function(userId: string, bookId: string, lang: 'fr' | 'en'): LocationProps[] {
|
||||
return Location.getAllLocations(userId, bookId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// POST /location/section/add - Add location section
|
||||
ipcMain.handle('db:location:section:add', createHandler<AddLocationSectionData, string>(
|
||||
function(userId: string, data: AddLocationSectionData, lang: 'fr' | 'en'): string {
|
||||
return Location.addLocationSection(userId, data.locationName, data.bookId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// POST /location/element/add - Add location element
|
||||
ipcMain.handle('db:location:element:add', createHandler<AddLocationElementData, any>(
|
||||
function(userId: string, data: AddLocationElementData, lang: 'fr' | 'en') {
|
||||
return Location.addLocationElement(userId, data.locationId, data.elementName, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// POST /location/sub-element/add - Add location sub-element
|
||||
ipcMain.handle('db:location:subelement:add', createHandler<AddLocationSubElementData, any>(
|
||||
function(userId: string, data: AddLocationSubElementData, lang: 'fr' | 'en') {
|
||||
return Location.addLocationSubElement(userId, data.elementId, data.subElementName, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// POST /location/update - Update location section
|
||||
ipcMain.handle('db:location:update', createHandler<UpdateLocationData, any>(
|
||||
function(userId: string, data: UpdateLocationData, lang: 'fr' | 'en') {
|
||||
return Location.updateLocationSection(userId, data.locations, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// DELETE /location/delete - Delete location section
|
||||
ipcMain.handle('db:location:delete', createHandler<string, any>(
|
||||
function(userId: string, locationId: string, lang: 'fr' | 'en') {
|
||||
return Location.deleteLocationSection(userId, locationId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// DELETE /location/element/delete - Delete location element
|
||||
ipcMain.handle('db:location:element:delete', createHandler<string, any>(
|
||||
function(userId: string, elementId: string, lang: 'fr' | 'en') {
|
||||
return Location.deleteLocationElement(userId, elementId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// DELETE /location/sub-element/delete - Delete location sub-element
|
||||
ipcMain.handle('db:location:subelement:delete', createHandler<string, any>(
|
||||
function(userId: string, subElementId: string, lang: 'fr' | 'en') {
|
||||
return Location.deleteLocationSubElement(userId, subElementId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
console.log('[IPC] Location handlers registered');
|
||||
@@ -10,6 +10,8 @@ import { getDatabaseService } from './database/database.service.js';
|
||||
import './ipc/book.ipc.js';
|
||||
import './ipc/user.ipc.js';
|
||||
import './ipc/chapter.ipc.js';
|
||||
import './ipc/character.ipc.js';
|
||||
import './ipc/location.ipc.js';
|
||||
|
||||
// Fix pour __dirname en ES modules
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
|
||||
Reference in New Issue
Block a user