Files
ERitors-Scribe-Desktop/electron/ipc/location.ipc.ts
natreex 71d13e2b12 Refactor IPC handlers to enforce consistent return types and enhance type definitions
- Update return types for IPC handlers in `character.ipc.ts`, `chapter.ipc.ts`, and `location.ipc.ts` for stricter type safety.
- Introduce new type definitions for better structure and clarity.
- Remove redundant console logs and unnecessary comments for cleaner code.
2025-11-18 23:01:52 -05:00

95 lines
3.3 KiB
TypeScript

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 UpdateLocationResponse {
valid: boolean;
message: string;
}
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, string>(
function(userId: string, data: AddLocationElementData, lang: 'fr' | 'en'): string {
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, string>(
function(userId: string, data: AddLocationSubElementData, lang: 'fr' | 'en'): string {
return Location.addLocationSubElement(userId, data.elementId, data.subElementName, lang);
}
)
);
// POST /location/update - Update location section
ipcMain.handle('db:location:update', createHandler<UpdateLocationData, UpdateLocationResponse>(
function(userId: string, data: UpdateLocationData, lang: 'fr' | 'en'): UpdateLocationResponse {
return Location.updateLocationSection(userId, data.locations, lang);
}
)
);
// DELETE /location/delete - Delete location section
ipcMain.handle('db:location:delete', createHandler<string, boolean>(
function(userId: string, locationId: string, lang: 'fr' | 'en'): boolean {
return Location.deleteLocationSection(userId, locationId, lang);
}
)
);
// DELETE /location/element/delete - Delete location element
ipcMain.handle('db:location:element:delete', createHandler<string, boolean>(
function(userId: string, elementId: string, lang: 'fr' | 'en'): boolean {
return Location.deleteLocationElement(userId, elementId, lang);
}
)
);
// DELETE /location/sub-element/delete - Delete location sub-element
ipcMain.handle('db:location:subelement:delete', createHandler<string, boolean>(
function(userId: string, subElementId: string, lang: 'fr' | 'en'): boolean {
return Location.deleteLocationSubElement(userId, subElementId, lang);
}
)
);
console.log('[IPC] Location handlers registered');