Files
foka-ci/apps/server/controllers/user/index.ts
hurole d22fdc9618 feat: 实现环境变量预设功能 & 移除稀疏检出
## 后端改动
- 添加 Project.envPresets 字段(JSON 格式)
- 移除 Deployment.env 字段,统一使用 envVars
- 更新部署 DTO,支持 envVars (Record<string, string>)
- pipeline-runner 支持解析并注入 envVars 到环境
- 移除稀疏检出模板和相关环境变量
- 优化代码格式(Biome lint & format)

## 前端改动
- 新增 EnvPresetsEditor 组件(支持单选/多选/输入框类型)
- 项目创建/编辑界面集成环境预设编辑器
- 部署界面基于预设动态生成环境变量表单
- 移除稀疏检出表单项
- 项目详情页添加环境变量预设配置 tab
- 优化部署界面布局(基本参数 & 环境变量分区)

## 文档
- 添加完整文档目录结构(docs/)
- 创建设计文档 design-0005(部署流程重构)
- 添加 API 文档、架构设计文档等

## 数据库
- 执行 prisma db push 同步 schema 变更
2026-01-03 22:59:20 +08:00

129 lines
2.8 KiB
TypeScript

import type { Context } from 'koa';
import { Controller, Delete, Get, Post, Put } from '../../decorators/route.ts';
import { BusinessError } from '../../middlewares/exception.ts';
import {
createUserSchema,
searchUserQuerySchema,
updateUserSchema,
userIdSchema,
} from './dto.ts';
/**
* 用户控制器
*/
@Controller('/user')
export class UserController {
@Get('/list')
async list(_ctx: Context) {
// 模拟用户列表数据
const users = [
{ id: 1, name: 'Alice', email: 'alice@example.com', status: 'active' },
{ id: 2, name: 'Bob', email: 'bob@example.com', status: 'inactive' },
{
id: 3,
name: 'Charlie',
email: 'charlie@example.com',
status: 'active',
},
];
return users;
}
@Get('/detail/:id')
async detail(ctx: Context) {
const { id } = userIdSchema.parse(ctx.params);
// 模拟根据ID查找用户
const user = {
id,
name: `User ${id}`,
email: `user${id}@example.com`,
status: 'active',
createdAt: new Date().toISOString(),
};
if (id > 100) {
throw new BusinessError('用户不存在', 2001, 404);
}
return user;
}
@Post('')
async create(ctx: Context) {
const body = createUserSchema.parse(ctx.request.body);
// 模拟创建用户
const newUser = {
id: Date.now(),
...body,
createdAt: new Date().toISOString(),
status: body.status,
};
return newUser;
}
@Put('/:id')
async update(ctx: Context) {
const { id } = userIdSchema.parse(ctx.params);
const body = updateUserSchema.parse(ctx.request.body);
// 模拟更新用户
const updatedUser = {
id,
...body,
updatedAt: new Date().toISOString(),
};
return updatedUser;
}
@Delete('/:id')
async delete(ctx: Context) {
const { id } = userIdSchema.parse(ctx.params);
if (id === 1) {
throw new BusinessError('管理员账户不能删除', 2002, 403);
}
// 模拟删除操作
return {
success: true,
message: `用户 ${id} 已删除`,
deletedAt: new Date().toISOString(),
};
}
@Get('/search')
async search(ctx: Context) {
const { keyword, status } = searchUserQuerySchema.parse(ctx.query);
// 模拟搜索逻辑
let results = [
{ id: 1, name: 'Alice', email: 'alice@example.com', status: 'active' },
{ id: 2, name: 'Bob', email: 'bob@example.com', status: 'inactive' },
];
if (keyword) {
results = results.filter(
(user) =>
user.name.toLowerCase().includes(keyword.toLowerCase()) ||
user.email.toLowerCase().includes(keyword.toLowerCase()),
);
}
if (status) {
results = results.filter((user) => user.status === status);
}
return {
keyword,
status,
total: results.length,
results,
};
}
}