feat: Introduce DTOs for API validation and new deployment features, including a Git controller and UI components.
This commit is contained in:
19
apps/server/controllers/deployment/dto.ts
Normal file
19
apps/server/controllers/deployment/dto.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const listDeploymentsQuerySchema = z.object({
|
||||
page: z.coerce.number().int().min(1).optional().default(1),
|
||||
pageSize: z.coerce.number().int().min(1).max(100).optional().default(10),
|
||||
projectId: z.coerce.number().int().positive().optional(),
|
||||
});
|
||||
|
||||
export const createDeploymentSchema = z.object({
|
||||
projectId: z.number().int().positive({ message: '项目ID必须是正整数' }),
|
||||
pipelineId: z.number().int().positive({ message: '流水线ID必须是正整数' }),
|
||||
branch: z.string().min(1, { message: '分支不能为空' }),
|
||||
commitHash: z.string().min(1, { message: '提交哈希不能为空' }),
|
||||
commitMessage: z.string().min(1, { message: '提交信息不能为空' }),
|
||||
env: z.string().optional(),
|
||||
});
|
||||
|
||||
export type ListDeploymentsQuery = z.infer<typeof listDeploymentsQuerySchema>;
|
||||
export type CreateDeploymentInput = z.infer<typeof createDeploymentSchema>;
|
||||
@@ -1,45 +1,61 @@
|
||||
import { Controller, Get, Post } from '../../decorators/route.ts';
|
||||
import type { Prisma } from '../../generated/prisma/index.js';
|
||||
import type { Prisma } from '../../generated/client.ts';
|
||||
import { prisma } from '../../libs/prisma.ts';
|
||||
import type { Context } from 'koa';
|
||||
import { listDeploymentsQuerySchema, createDeploymentSchema } from './dto.ts';
|
||||
|
||||
@Controller('/deployments')
|
||||
export class DeploymentController {
|
||||
@Get('')
|
||||
async list(ctx: Context) {
|
||||
const { page = 1, pageSize = 10 } = ctx.query;
|
||||
const { page, pageSize, projectId } = listDeploymentsQuerySchema.parse(ctx.query);
|
||||
const where: Prisma.DeploymentWhereInput = {
|
||||
valid: 1,
|
||||
};
|
||||
|
||||
if (projectId) {
|
||||
where.projectId = projectId;
|
||||
}
|
||||
|
||||
const result = await prisma.deployment.findMany({
|
||||
where: {
|
||||
valid: 1,
|
||||
},
|
||||
take: Number(pageSize),
|
||||
skip: (Number(page) - 1) * Number(pageSize),
|
||||
where,
|
||||
take: pageSize,
|
||||
skip: (page - 1) * pageSize,
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
const total = await prisma.deployment.count();
|
||||
const total = await prisma.deployment.count({ where });
|
||||
|
||||
return {
|
||||
data: result,
|
||||
page: Number(page),
|
||||
pageSize: Number(pageSize),
|
||||
total: total,
|
||||
page,
|
||||
pageSize,
|
||||
total,
|
||||
};
|
||||
}
|
||||
|
||||
@Post('')
|
||||
async create(ctx: Context) {
|
||||
const body = ctx.request.body as Prisma.DeploymentCreateInput;
|
||||
const body = createDeploymentSchema.parse(ctx.request.body);
|
||||
|
||||
prisma.deployment.create({
|
||||
const result = await prisma.deployment.create({
|
||||
data: {
|
||||
branch: body.branch,
|
||||
commitHash: body.commitHash,
|
||||
commitMessage: body.commitMessage,
|
||||
|
||||
status: 'pending',
|
||||
Project: {
|
||||
connect: { id: body.projectId },
|
||||
},
|
||||
pipelineId: body.pipelineId,
|
||||
env: body.env || 'dev',
|
||||
buildLog: '',
|
||||
createdBy: 'system', // TODO: get from user
|
||||
updatedBy: 'system',
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user