Project.ts•1.51 kB
import { v4 as uuidv4 } from 'uuid';
/**
* 项目基本信息接口
*/
export interface IProject {
id: string;
name: string;
description: string;
createdAt: Date;
updatedAt: Date;
rules?: string[];
}
/**
* 项目类,用于管理项目信息
*/
export class Project implements IProject {
id: string;
name: string;
description: string;
createdAt: Date;
updatedAt: Date;
rules?: string[];
constructor(name: string, description: string, rules?: string[]) {
this.id = uuidv4();
this.name = name;
this.description = description;
this.createdAt = new Date();
this.updatedAt = new Date();
this.rules = rules || [];
}
/**
* 从JSON对象创建项目实例
*/
static fromJSON(json: any): Project {
const project = new Project(json.name, json.description, json.rules);
project.id = json.id;
project.createdAt = new Date(json.createdAt);
project.updatedAt = new Date(json.updatedAt);
return project;
}
/**
* 转换为JSON对象
*/
toJSON(): IProject {
return {
id: this.id,
name: this.name,
description: this.description,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
rules: this.rules
};
}
/**
* 更新项目信息
*/
update(name?: string, description?: string, rules?: string[]): void {
if (name) this.name = name;
if (description) this.description = description;
if (rules) this.rules = rules;
this.updatedAt = new Date();
}
}