mira-app-server
Version:
Mira Server - standalone server application using mira-app-core
135 lines • 5.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TagRouter = void 0;
const express_1 = require("express");
const BaseRouter_1 = require("./BaseRouter");
class TagRouter extends BaseRouter_1.BaseRouter {
constructor(backend) {
super(backend);
this.router = (0, express_1.Router)();
this.setupRoutes();
}
setupRoutes() {
// 获取所有标签
this.router.get('/all', async (req, res) => {
await this.handleCrudOperation(req, res, 'getAll', 'getAllTags');
});
// 查询标签
this.router.post('/query', async (req, res) => {
await this.handleCrudOperation(req, res, 'query', 'queryTag');
});
// 创建标签
this.router.post('/create', async (req, res) => {
await this.handleCrudOperation(req, res, 'create', 'createTag', {
successMessage: 'Tag created successfully',
onSuccess: (result, libraryId) => {
this.broadcastTagEvent('tag::created', libraryId, { result, libraryId });
}
});
});
// 更新标签
this.router.put('/update', async (req, res) => {
await this.handleCrudOperation(req, res, 'update', 'updateTag', {
requiresId: true,
successMessage: 'Tag updated successfully',
onSuccess: (_result, libraryId) => {
this.broadcastTagEvent('tag::updated', libraryId, { id: req.body.id, title: req.body.title, libraryId });
}
});
});
// 批量设置标签排序 index
this.router.put('/sort-index', async (req, res) => {
try {
const libraryId = req.body.libraryId || req.query.libraryId;
const validation = await this.validateLibrary(libraryId);
if (!validation.success) {
res.status(validation.error.code).json(validation.error);
return;
}
const items = req.body.items;
if (!Array.isArray(items)) {
this.sendError(res, 400, 'items must be an array');
return;
}
const db = validation.library.libraryService;
for (const item of items) {
await db.updateTag(item.id, { sort_index: item.sort_index });
}
this.broadcastTagEvent('tag::updated', libraryId, { items, libraryId });
this.sendSuccess(res, { updated: items.length }, 'Sort index updated');
}
catch (error) {
console.error('Update tag sort-index error:', error);
this.sendError(res, 500, 'Internal server error');
}
});
// 删除标签
this.router.delete('/delete', async (req, res) => {
await this.handleCrudOperation(req, res, 'delete', 'deleteTag', {
requiresId: true,
successMessage: 'Tag deleted successfully',
onSuccess: (_, libraryId) => {
const id = req.body.id;
this.broadcastTagEvent('tag::deleted', libraryId, { id, libraryId });
}
});
});
// 为文件设置标签
this.router.post('/file/set', async (req, res) => {
try {
const libraryId = req.body.libraryId || req.query.libraryId;
const fileId = req.body.fileId;
const tags = req.body.tags;
if (!fileId) {
res.status(400).json({ code: 400, message: 'File ID is required', data: null });
return;
}
const validation = await this.validateLibrary(libraryId);
if (!validation.success) {
res.status(validation.error.code).json(validation.error);
return;
}
const { library } = validation;
const db = library.libraryService;
const tagIds = [];
for (const tag of tags) {
// 已经是纯数字(标签ID),直接用
if (/^\d+$/.test(tag)) {
tagIds.push(tag);
continue;
}
// 按名称查找
const found = await db.queryTag({ title: tag });
if (found.length > 0) {
tagIds.push(String(found[0].id));
}
else {
const newId = await db.createTag({ title: tag });
tagIds.push(String(newId));
}
}
const result = await db.setFileTags(fileId, tagIds);
this.sendSuccess(res, { fileId, tags: tagIds, success: result.success, old_data: result.oldData }, 'File tags set successfully');
}
catch (error) {
console.error('Set file tags error:', error);
this.sendError(res, 500, 'Internal server error');
}
});
// 获取文件的标签
this.router.get('/file/:fileId', async (req, res) => {
await this.handleFileAssociation(req, res, 'get', 'getFileTags', 'tags');
});
}
broadcastTagEvent(eventName, libraryId, data) {
if (!this.backend.webSocketServer)
return;
this.backend.webSocketServer.broadcastPluginEvent(eventName, data);
this.backend.webSocketServer.broadcastLibraryEvent(libraryId, eventName, data);
}
getRouter() {
return this.router;
}
}
exports.TagRouter = TagRouter;
//# sourceMappingURL=TagRouter.js.map