feat: 项目的增删改查
This commit is contained in:
167
apps/server/controllers/project/index.ts
Normal file
167
apps/server/controllers/project/index.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
import type { Context } from 'koa';
|
||||
import prisma from '../../libs/db.ts';
|
||||
import { log } from '../../libs/logger.ts';
|
||||
import { BusinessError } from '../../middlewares/exception.ts';
|
||||
import { Controller, Get, Post, Put, Delete } from '../../decorators/route.ts';
|
||||
import {
|
||||
createProjectSchema,
|
||||
updateProjectSchema,
|
||||
listProjectQuerySchema,
|
||||
projectIdSchema,
|
||||
} from './schema.ts';
|
||||
|
||||
@Controller('/projects')
|
||||
export class ProjectController {
|
||||
// GET /api/projects - 获取项目列表
|
||||
@Get('')
|
||||
async list(ctx: Context) {
|
||||
const query = listProjectQuerySchema.parse(ctx.query);
|
||||
|
||||
const whereCondition: any = {
|
||||
valid: 1,
|
||||
};
|
||||
|
||||
// 如果提供了名称搜索参数
|
||||
if (query?.name) {
|
||||
whereCondition.name = {
|
||||
contains: query.name,
|
||||
};
|
||||
}
|
||||
|
||||
const [total, projects] = await Promise.all([
|
||||
prisma.project.count({ where: whereCondition }),
|
||||
prisma.project.findMany({
|
||||
where: whereCondition,
|
||||
skip: query ? (query.page - 1) * query.limit : 0,
|
||||
take: query?.limit,
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
})
|
||||
]);
|
||||
|
||||
return {
|
||||
data: projects,
|
||||
pagination: {
|
||||
page: query?.page || 1,
|
||||
limit: query?.limit || 10,
|
||||
total,
|
||||
totalPages: Math.ceil(total / (query?.limit || 10)),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// GET /api/projects/:id - 获取单个项目
|
||||
@Get(':id')
|
||||
async show(ctx: Context) {
|
||||
const { id } = projectIdSchema.parse(ctx.params);
|
||||
|
||||
const project = await prisma.project.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
throw new BusinessError('项目不存在', 1002, 404);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
// POST /api/projects - 创建项目
|
||||
@Post('')
|
||||
async create(ctx: Context) {
|
||||
const validatedData = createProjectSchema.parse(ctx.request.body);
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: {
|
||||
name: validatedData.name,
|
||||
description: validatedData.description || '',
|
||||
repository: validatedData.repository,
|
||||
createdBy: 'system',
|
||||
updatedBy: 'system',
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
log.info('project', 'Created new project: %s', project.name);
|
||||
return project;
|
||||
}
|
||||
|
||||
// PUT /api/projects/:id - 更新项目
|
||||
@Put(':id')
|
||||
async update(ctx: Context) {
|
||||
const { id } = projectIdSchema.parse(ctx.params);
|
||||
const validatedData = updateProjectSchema.parse(ctx.request.body);
|
||||
|
||||
// 检查项目是否存在
|
||||
const existingProject = await prisma.project.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingProject) {
|
||||
throw new BusinessError('项目不存在', 1002, 404);
|
||||
}
|
||||
|
||||
// 只更新提供的字段
|
||||
const updateData: any = {
|
||||
updatedBy: 'system',
|
||||
};
|
||||
|
||||
if (validatedData.name !== undefined) {
|
||||
updateData.name = validatedData.name;
|
||||
}
|
||||
if (validatedData.description !== undefined) {
|
||||
updateData.description = validatedData.description;
|
||||
}
|
||||
if (validatedData.repository !== undefined) {
|
||||
updateData.repository = validatedData.repository;
|
||||
}
|
||||
|
||||
const project = await prisma.project.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
log.info('project', 'Updated project: %s', project.name);
|
||||
return project;
|
||||
}
|
||||
|
||||
// DELETE /api/projects/:id - 删除项目(软删除)
|
||||
@Delete(':id')
|
||||
async destroy(ctx: Context) {
|
||||
const { id } = projectIdSchema.parse(ctx.params);
|
||||
|
||||
// 检查项目是否存在
|
||||
const existingProject = await prisma.project.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingProject) {
|
||||
throw new BusinessError('项目不存在', 1002, 404);
|
||||
}
|
||||
|
||||
// 软删除:将 valid 设置为 0
|
||||
await prisma.project.update({
|
||||
where: { id },
|
||||
data: {
|
||||
valid: 0,
|
||||
updatedBy: 'system',
|
||||
},
|
||||
});
|
||||
|
||||
log.info('project', 'Deleted project: %s', existingProject.name);
|
||||
|
||||
// RESTful 删除成功返回 204 No Content
|
||||
ctx.status = 204;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
57
apps/server/controllers/project/schema.ts
Normal file
57
apps/server/controllers/project/schema.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* 创建项目验证架构
|
||||
*/
|
||||
export const createProjectSchema = z.object({
|
||||
name: z.string({
|
||||
message: '项目名称必须是字符串',
|
||||
}).min(2, { message: '项目名称至少2个字符' }).max(50, { message: '项目名称不能超过50个字符' }),
|
||||
|
||||
description: z.string({
|
||||
message: '项目描述必须是字符串',
|
||||
}).max(200, { message: '项目描述不能超过200个字符' }).optional(),
|
||||
|
||||
repository: z.string({
|
||||
message: '仓库地址必须是字符串',
|
||||
}).url({ message: '请输入有效的仓库地址' }).min(1, { message: '仓库地址不能为空' }),
|
||||
});
|
||||
|
||||
/**
|
||||
* 更新项目验证架构
|
||||
*/
|
||||
export const updateProjectSchema = z.object({
|
||||
name: z.string({
|
||||
message: '项目名称必须是字符串',
|
||||
}).min(2, { message: '项目名称至少2个字符' }).max(50, { message: '项目名称不能超过50个字符' }).optional(),
|
||||
|
||||
description: z.string({
|
||||
message: '项目描述必须是字符串',
|
||||
}).max(200, { message: '项目描述不能超过200个字符' }).optional(),
|
||||
|
||||
repository: z.string({
|
||||
message: '仓库地址必须是字符串',
|
||||
}).url({ message: '请输入有效的仓库地址' }).min(1, { message: '仓库地址不能为空' }).optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* 项目列表查询参数验证架构
|
||||
*/
|
||||
export const listProjectQuerySchema = z.object({
|
||||
page: z.coerce.number().int().min(1, { message: '页码必须大于0' }).optional().default(1),
|
||||
limit: z.coerce.number().int().min(1, { message: '每页数量必须大于0' }).max(100, { message: '每页数量不能超过100' }).optional().default(10),
|
||||
name: z.string().optional(),
|
||||
}).optional();
|
||||
|
||||
/**
|
||||
* 项目ID验证架构
|
||||
*/
|
||||
export const projectIdSchema = z.object({
|
||||
id: z.coerce.number().int().positive({ message: '项目 ID 必须是正整数' }),
|
||||
});
|
||||
|
||||
// TypeScript 类型导出
|
||||
export type CreateProjectInput = z.infer<typeof createProjectSchema>;
|
||||
export type UpdateProjectInput = z.infer<typeof updateProjectSchema>;
|
||||
export type ListProjectQuery = z.infer<typeof listProjectQuerySchema>;
|
||||
export type ProjectIdParams = z.infer<typeof projectIdSchema>;
|
||||
Reference in New Issue
Block a user