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:
natreex
2025-11-18 22:47:20 -05:00
parent 75e5d71c74
commit b56f45d1ba
3 changed files with 156 additions and 0 deletions

View 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');