Refactor IPC handlers to integrate new models and improve maintainability
- Replaced `Book`-based references with specialized models (`Act`, `Issue`, `World`, `GuideLine`, etc.) for enhanced modularity. - Updated synchronization, story, guideline, plot, and world-related methods to align with new models. - Removed redundant type definitions and adjusted interfaces for consistency.
This commit is contained in:
@@ -1,9 +1,18 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import { createHandler } from '../database/LocalSystem.js';
|
||||
import Book, {BookSyncCompare, CompleteBook, SyncedBook} from '../database/models/Book.js';
|
||||
import type { BookProps, GuideLine, GuideLineAI, Act, Issue, WorldProps } from '../database/models/Book.js';
|
||||
import type { BookProps } from '../database/models/Book.js';
|
||||
import Chapter from '../database/models/Chapter.js';
|
||||
import type { ChapterProps } from '../database/models/Chapter.js';
|
||||
import Act, {ActProps} from "@/electron/database/models/Act";
|
||||
import Issue, {IssueProps} from "@/electron/database/models/Issue";
|
||||
import Sync from "@/electron/database/models/Sync";
|
||||
import Download from "@/electron/database/models/Download";
|
||||
import Upload from "@/electron/database/models/Upload";
|
||||
import GuideLine, {GuideLineAI} from "@/electron/database/models/GuideLine";
|
||||
import Incident from "@/electron/database/models/Incident";
|
||||
import PlotPoint from "@/electron/database/models/PlotPoint";
|
||||
import World, {WorldProps} from "@/electron/database/models/World";
|
||||
|
||||
interface UpdateBookBasicData {
|
||||
title: string;
|
||||
@@ -29,14 +38,14 @@ interface UpdateGuideLineData {
|
||||
}
|
||||
|
||||
interface StoryData {
|
||||
acts: Act[];
|
||||
issues: Issue[];
|
||||
acts: ActProps[];
|
||||
issues: IssueProps[];
|
||||
mainChapter: ChapterProps[];
|
||||
}
|
||||
|
||||
interface UpdateStoryData {
|
||||
bookId: string;
|
||||
acts: Act[];
|
||||
acts: ActProps[];
|
||||
mainChapters: ChapterProps[];
|
||||
}
|
||||
|
||||
@@ -97,6 +106,11 @@ interface GetGuidelineData {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface UpdateWorldData {
|
||||
bookId: string;
|
||||
world: WorldProps;
|
||||
}
|
||||
|
||||
// GET /books - Get all books
|
||||
ipcMain.handle('db:book:books', createHandler<void, BookProps[]>(
|
||||
async function(userId: string, _body: void, lang: 'fr' | 'en'):Promise<BookProps[]> {
|
||||
@@ -108,14 +122,14 @@ ipcMain.handle('db:book:books', createHandler<void, BookProps[]>(
|
||||
// GET /books/synced - Get all synced books
|
||||
ipcMain.handle('db:books:synced', createHandler<void, SyncedBook[]>(
|
||||
async function(userId: string, _body: void, lang: 'fr' | 'en'):Promise<SyncedBook[]> {
|
||||
return await Book.getSyncedBooks(userId, lang);
|
||||
return await Sync.getSyncedBooks(userId, lang);
|
||||
})
|
||||
);
|
||||
|
||||
// POST /book/sync/save - Save complete book
|
||||
ipcMain.handle('db:book:syncSave', createHandler<CompleteBook, boolean>(
|
||||
async function(userId: string, data: CompleteBook, lang: 'fr' | 'en'):Promise<boolean> {
|
||||
return await Book.saveCompleteBook(userId, data, lang);
|
||||
return await Download.saveCompleteBook(userId, data, lang);
|
||||
})
|
||||
);
|
||||
|
||||
@@ -130,7 +144,7 @@ ipcMain.handle('db:book:bookBasicInformation', createHandler<string, BookProps>(
|
||||
// GET
|
||||
ipcMain.handle('db:book:uploadToServer', createHandler<string, CompleteBook>(
|
||||
async function(userId: string, bookId: string, lang: 'fr' | 'en'):Promise<CompleteBook> {
|
||||
return await Book.uploadBookForSync(userId, bookId, lang);
|
||||
return await Upload.uploadBookForSync(userId, bookId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -146,7 +160,7 @@ ipcMain.handle('db:book:updateBasicInformation', createHandler<UpdateBookBasicDa
|
||||
// GET /book/sync/to-client - Get book data to sync to client
|
||||
ipcMain.handle('db:book:sync:toServer', createHandler<BookSyncCompare, CompleteBook>(
|
||||
async function(userId: string, data:BookSyncCompare, lang: 'fr' | 'en'):Promise<CompleteBook> {
|
||||
return await Book.getCompleteSyncBook(userId, data, lang);
|
||||
return await Sync.getCompleteSyncBook(userId, data, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -154,7 +168,7 @@ ipcMain.handle('db:book:sync:toServer', createHandler<BookSyncCompare, CompleteB
|
||||
// GET /book/sync/from-server - Get book data to sync from server
|
||||
ipcMain.handle('db:book:sync:toClient', createHandler<CompleteBook, boolean>(
|
||||
async function(userId: string, data:CompleteBook, lang: 'fr' | 'en'):Promise<boolean> {
|
||||
return await Book.syncBookFromServerToClient(userId, data, lang);
|
||||
return await Sync.syncBookFromServerToClient(userId, data, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -162,7 +176,7 @@ ipcMain.handle('db:book:sync:toClient', createHandler<CompleteBook, boolean>(
|
||||
// GET /book/guide-line - Get guideline
|
||||
ipcMain.handle('db:book:guideline:get',
|
||||
createHandler<GetGuidelineData, GuideLine | null>(async function(userId: string, data: GetGuidelineData, lang: 'fr' | 'en') {
|
||||
return await Book.getGuideLine(userId, data.id, lang);
|
||||
return await GuideLine.getGuideLine(userId, data.id, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -170,7 +184,7 @@ ipcMain.handle('db:book:guideline:get',
|
||||
// POST /book/guide-line - Update guideline
|
||||
ipcMain.handle('db:book:guideline:update', createHandler<UpdateGuideLineData, boolean>(
|
||||
async function(userId: string, data: UpdateGuideLineData, lang: 'fr' | 'en') {
|
||||
return await Book.updateGuideLine(
|
||||
return await GuideLine.updateGuideLine(
|
||||
userId,
|
||||
data.bookId,
|
||||
data.tone,
|
||||
@@ -195,8 +209,8 @@ interface GetStoryData {
|
||||
}
|
||||
ipcMain.handle('db:book:story:get', createHandler<GetStoryData, StoryData>(
|
||||
async function(userId: string, data: GetStoryData, lang: 'fr' | 'en'):Promise<StoryData> {
|
||||
const acts:Act[] = await Book.getActsData(userId, data.bookid, lang);
|
||||
const issues:Issue[] = await Book.getIssuesFromBook(userId, data.bookid, lang);
|
||||
const acts:ActProps[] = await Act.getActsData(userId, data.bookid, lang);
|
||||
const issues:IssueProps[] = await Issue.getIssuesFromBook(userId, data.bookid, lang);
|
||||
const mainChapter:ChapterProps[] = Chapter.getAllChaptersFromABook(userId, data.bookid, lang);
|
||||
return {
|
||||
acts,
|
||||
@@ -210,7 +224,7 @@ ipcMain.handle('db:book:story:get', createHandler<GetStoryData, StoryData>(
|
||||
// POST /book/story - Update story (acts + mainChapters)
|
||||
ipcMain.handle('db:book:story:update', createHandler<UpdateStoryData, boolean>(
|
||||
function(userId: string, data: UpdateStoryData, lang: 'fr' | 'en'):boolean {
|
||||
return Book.updateStory(userId, data.bookId, data.acts, data.mainChapters, lang);
|
||||
return Act.updateStory(userId, data.bookId, data.acts, data.mainChapters, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -240,7 +254,7 @@ ipcMain.handle(
|
||||
'db:book:incident:add',
|
||||
createHandler<AddIncidentData, string>(
|
||||
function(userId: string, data: AddIncidentData, lang: 'fr' | 'en') {
|
||||
return Book.addNewIncident(userId, data.bookId, data.name, lang, data.incidentId);
|
||||
return Incident.addNewIncident(userId, data.bookId, data.name, lang, data.incidentId);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -253,15 +267,15 @@ interface RemoveIncidentData {
|
||||
|
||||
ipcMain.handle('db:book:incident:remove', createHandler<RemoveIncidentData, boolean>(
|
||||
function(userId: string, data: RemoveIncidentData, lang: 'fr' | 'en') {
|
||||
return Book.removeIncident(userId, data.bookId, data.incidentId, lang);
|
||||
return Incident.removeIncident(userId, data.bookId, data.incidentId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// POST /book/plot/new - Add plot point
|
||||
ipcMain.handle('db:book:plot:add', createHandler<AddPlotPointData, string>(
|
||||
function(userId: string, data: AddPlotPointData, lang: 'fr' | 'en') {
|
||||
return Book.addNewPlotPoint(
|
||||
function(userId: string, data: AddPlotPointData, lang: 'fr' | 'en'):string {
|
||||
return PlotPoint.addNewPlotPoint(
|
||||
userId,
|
||||
data.bookId,
|
||||
data.incidentId,
|
||||
@@ -281,7 +295,7 @@ ipcMain.handle(
|
||||
'db:book:plot:remove',
|
||||
createHandler<RemovePlotData, boolean>(
|
||||
function(userId: string, data: RemovePlotData, lang: 'fr' | 'en') {
|
||||
return Book.removePlotPoint(userId, data.plotId, lang);
|
||||
return PlotPoint.removePlotPoint(userId, data.plotId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -289,7 +303,7 @@ ipcMain.handle(
|
||||
// POST /book/issue/add - Add issue
|
||||
ipcMain.handle('db:book:issue:add', createHandler<AddIssueData, string>(
|
||||
function(userId: string, data: AddIssueData, lang: 'fr' | 'en') {
|
||||
return Book.addNewIssue(userId, data.bookId, data.name, lang, data.issueId);
|
||||
return Issue.addNewIssue(userId, data.bookId, data.name, lang, data.issueId);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -301,7 +315,7 @@ interface RemoveIssueData {
|
||||
}
|
||||
ipcMain.handle('db:book:issue:remove', createHandler<RemoveIssueData, boolean>(
|
||||
function(userId: string, data: RemoveIssueData, lang: 'fr' | 'en') {
|
||||
return Book.removeIssue(userId, data.issueId, lang);
|
||||
return Issue.removeIssue(userId, data.issueId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -312,7 +326,7 @@ interface GetWorldsData {
|
||||
}
|
||||
ipcMain.handle('db:book:worlds:get', createHandler<GetWorldsData, WorldProps[]>(
|
||||
function(userId: string, data: GetWorldsData, lang: 'fr' | 'en') {
|
||||
return Book.getWorlds(userId, data.bookid, lang);
|
||||
return World.getWorlds(userId, data.bookid, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -320,7 +334,7 @@ ipcMain.handle('db:book:worlds:get', createHandler<GetWorldsData, WorldProps[]>(
|
||||
// POST /book/world/add - Add world
|
||||
ipcMain.handle('db:book:world:add', createHandler<AddWorldData, string>(
|
||||
function(userId: string, data: AddWorldData, lang: 'fr' | 'en') {
|
||||
return Book.addNewWorld(userId, data.bookId, data.worldName, lang, data.id);
|
||||
return World.addNewWorld(userId, data.bookId, data.worldName, lang, data.id);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -328,7 +342,7 @@ ipcMain.handle('db:book:world:add', createHandler<AddWorldData, string>(
|
||||
// POST /book/world/element/add - Add element to world
|
||||
ipcMain.handle('db:book:world:element:add', createHandler<AddWorldElementData, string>(
|
||||
function(userId: string, data: AddWorldElementData, lang: 'fr' | 'en') {
|
||||
return Book.addNewElementToWorld(
|
||||
return World.addNewElementToWorld(
|
||||
userId,
|
||||
data.worldId,
|
||||
data.elementName,
|
||||
@@ -345,7 +359,7 @@ interface RemoveWorldElementData {
|
||||
}
|
||||
ipcMain.handle('db:book:world:element:remove', createHandler<RemoveWorldElementData, boolean>(
|
||||
function(userId: string, data: RemoveWorldElementData, lang: 'fr' | 'en') {
|
||||
return Book.removeElementFromWorld(userId, data.elementId, lang);
|
||||
return World.removeElementFromWorld(userId, data.elementId, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -367,7 +381,7 @@ interface GetAIGuidelineData {
|
||||
}
|
||||
ipcMain.handle('db:book:guideline:ai:get', createHandler<GetAIGuidelineData, GuideLineAI>(
|
||||
function(userId: string, data: GetAIGuidelineData, lang: 'fr' | 'en') {
|
||||
return Book.getGuideLineAI(userId, data.id, lang);
|
||||
return GuideLine.getGuideLineAI(userId, data.id, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -375,7 +389,7 @@ ipcMain.handle('db:book:guideline:ai:get', createHandler<GetAIGuidelineData, Gui
|
||||
// POST /book/ai/guideline (set) - Set AI guideline
|
||||
ipcMain.handle('db:book:guideline:ai:set', createHandler<SetAIGuideLineData, boolean>(
|
||||
function(userId: string, data: SetAIGuideLineData, lang: 'fr' | 'en') {
|
||||
return Book.setAIGuideLine(
|
||||
return GuideLine.setAIGuideLine(
|
||||
userId,
|
||||
data.bookId,
|
||||
data.narrativeType,
|
||||
@@ -392,14 +406,9 @@ ipcMain.handle('db:book:guideline:ai:set', createHandler<SetAIGuideLineData, boo
|
||||
);
|
||||
|
||||
// PUT /book/world/update - Update world
|
||||
interface UpdateWorldData {
|
||||
bookId: string;
|
||||
world: WorldProps;
|
||||
}
|
||||
|
||||
ipcMain.handle('db:book:world:update', createHandler<UpdateWorldData, boolean>(
|
||||
function(userId: string, data: UpdateWorldData, lang: 'fr' | 'en') {
|
||||
return Book.updateWorld(userId, data.world, lang);
|
||||
return World.updateWorld(userId, data.world, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user