Skip to content

Commit

Permalink
应用相关路由
Browse files Browse the repository at this point in the history
  • Loading branch information
LAMMUpro committed Jan 7, 2024
1 parent 50d1a15 commit acbef4a
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Trpc/src/routes/appEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { router } from "../trpc";

export const appEnvRouter = router({

})
66 changes: 55 additions & 11 deletions Trpc/src/routes/application.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@
import { z } from 'zod';
import { prisma } from '../../prisma/prisma.service';
import { procedure, router } from '../trpc';
import { ApplicationZod } from '../types/zod';
import { addApplication, deleteApplicationById, getApplicationList, updateApplicationById } from '../service/application';
import { ApplicationCreateZod, ApplicationUpdateZod } from '../types/zodExt/Application';
import { getDefaultFailResponse, getDefaultPageResponse, getDefaultResponse } from '../utils';
import { ApiPageResponseZod, ApiResponseZod, PageInfoQueryZod } from '../types';

export const applicationRouter = router({
/** 获取应用列表 */
pageList: procedure
/** 新增应用 */
addRecord: procedure
.meta({
description: "获取应用列表"
description: "新增应用"
})
.output(z.array(ApplicationZod))
.input(ApplicationCreateZod)
.mutation(async ({ input }) => {
const res = await addApplication(input);
if (res?.id) return getDefaultResponse();
return getDefaultFailResponse();
}),
/** 根据id删除应用 */
deleteRecodeById: procedure
.meta({
description: "删除应用"
})
.input(ApplicationZod.pick({ id: true }))
.mutation(async ({ input }) => {
const res = await deleteApplicationById(input.id);
if (res.id === input.id) return getDefaultResponse();
return getDefaultFailResponse();
}),
/** 获取全部应用列表 */
getList: procedure
.meta({
description: "获取全部应用列表"
})
.output(ApiResponseZod(z.array(ApplicationZod)))
.query(async () => {
const list = await prisma.application.findMany({
where: {
isDeleted: false
}
});
return list;
const list = await getApplicationList();
return getDefaultResponse(list);
}),
/** 分页获取应用列表 */
getListByPage: procedure
.meta({
description: "分页获取应用列表"
})
.input(PageInfoQueryZod)
.output(ApiPageResponseZod(ApplicationZod))
.query(async ({ input }) => {
const list = await getApplicationList();
return getDefaultPageResponse(input, list, 1000);
}),
/** 更新应用信息 */
updateRecord: procedure
.meta({
description: '更新应用信息'
})
.input(ApplicationUpdateZod)
.output(ApiResponseZod())
.mutation(async ({ input }) => {
const res = await updateApplicationById(input);
if (res.id == input.id) return getDefaultResponse();
return getDefaultFailResponse();
}),
});
3 changes: 3 additions & 0 deletions Trpc/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { router } from "../trpc";
import { appEnvRouter } from "./appEnv";
import { applicationRouter } from './application';
import { userRouter } from "./user";

Expand All @@ -7,6 +8,8 @@ export const appRouter = router({
user: userRouter,
/** 应用 */
application: applicationRouter,
/** 应用环境 */
appEnv: appEnvRouter,
})

export type AppRouter = typeof appRouter;
91 changes: 91 additions & 0 deletions Trpc/src/service/application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { prisma } from "../../prisma/prisma.service";
import { ApplicationCreateDto, ApplicationUpdateDto } from "../types/zodExt/Application";

export async function addApplication(data: ApplicationCreateDto) {
return await prisma.$transaction(async (mysql) => {
const res = await mysql.application.create({ data });
if (!res.id) return;
/**
* 应用默认版本1.0.0
*/
const versionData = {
applicationId: res.id,
version: '1.0.0',
isAuditing: false,
isPass: false,
auditContent: '',
isDeleted: false
};
const res_version = await mysql.appVersion.create({ data: versionData});
/**
* 应用默认环境test、pre、master
*/
const envData = {
applicationId: res.id,
env: 'test',
envCh: '测试',
canDelete: false,
appVersionId: res_version.id,
version: '1.0.0',
isDeleted: false
};
await mysql.appEnv.create({ data: envData });
envData.env = 'pre';
envData.envCh = '预发';
await mysql.appEnv.create({ data: envData });
envData.env = 'master';
envData.envCh = '正式';
envData.appVersionId = 0; // 正式环境不绑定版本!
envData.version = ''; // 正式环境须审核通过才能绑定!
await mysql.appEnv.create({ data: envData });
const pageNode = {
name: 'root',
nameCh: '根节点',
describe: '根节点, 有且唯一',
applicationId: res.id,
version: '1.0.0'
};
return await mysql.pageNode.create({
data: pageNode
});
})
}

export async function deleteApplicationById(id: number) {
const res = await prisma.application.update({
where: { id },
data: {
isDeleted: true
}
})
console.log(res);
return res;
}

export async function getApplicationList() {
return await prisma.application.findMany({
where: {
isDeleted: false,
}
})
}

export async function getPageList() {
return await prisma.application.findMany({
where: {
isDeleted: false,
}
})
}

export async function updateApplicationById(data: ApplicationUpdateDto) {
return await prisma.application.update({
where: {
id: data.id
},
data: {
name: data.name,
describe: data.describe
}
})
}
Empty file added Trpc/src/types/zodExt/AppEnv.ts
Empty file.
1 change: 1 addition & 0 deletions Trpc/src/types/zodExt/AppVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { z } from "zod";
18 changes: 18 additions & 0 deletions Trpc/src/types/zodExt/Application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { z } from "zod";
import { ApplicationZod } from "../zod/Application";

export const ApplicationCreateZod = ApplicationZod.pick({
name: true,
describe: true,
});

export type ApplicationCreateDto = z.infer<typeof ApplicationCreateZod>;

export const ApplicationUpdateZod = ApplicationZod.pick({
id: true,
name: true,
describe: true,
});

export type ApplicationUpdateDto = z.infer<typeof ApplicationUpdateZod>;

0 comments on commit acbef4a

Please sign in to comment.