import { Project } from '../models/Project';
import { ProjectService } from '../services/ProjectService';
export class ProjectController {
private projectService: ProjectService;
constructor(projectService: ProjectService) {
this.projectService = projectService;
}
async getProjectInfo(projectName: string): Promise<{ content: Array<{ type: "text"; text: string }> }> {
try {
const project = await this.projectService.getProjectInfo(projectName);
if (!project) {
return {
content: [
{
type: "text" as const,
text: `未找到名为"${projectName}"的项目。`,
},
],
};
}
return {
content: [
{
type: "text" as const,
text: JSON.stringify(project),
},
],
};
} catch (error) {
return {
content: [
{
type: "text" as const,
text: `查询项目时出错: ${error instanceof Error ? error.message : '未知错误'}`,
},
],
};
}
}
getProjectCount(): { content: Array<{ type: "text"; text: string }> } {
const count = this.projectService.getProjectCount();
return {
content: [
{
type: 'text',
text: `项目总数: ${count}`,
},
],
};
}
getAllProjects(): { content: Array<{ type: "text"; text: string }> } {
const projects = this.projectService.getAllProjects();
return {
content: [
{
type: 'text',
text: JSON.stringify(projects),
},
],
};
}
async addProject(project: Project): Promise<{ content: Array<{ type: "text"; text: string }> }> {
try {
await this.projectService.addProject(project);
return {
content: [
{
type: 'text',
text: `新增项目成功: ${JSON.stringify(project)}`,
},
],
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `新增项目失败: ${error instanceof Error ? error.message : '未知错误'}`,
},
],
};
}
}
}