Skip to content

Commit d3ca4da

Browse files
committed
add delete environment by name
1 parent 182ee30 commit d3ca4da

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { Swagger } from "atlassian-openapi";
2+
3+
export const openapi: Swagger.SwaggerV3 = {
4+
openapi: "3.0.0",
5+
info: {
6+
title: "Ctrlplane API",
7+
version: "1.0.0",
8+
},
9+
paths: {
10+
"/v1/systems/{systemId}/environments/{name}": {
11+
delete: {
12+
summary: "Delete an environment",
13+
operationId: "deleteEnvironmentByName",
14+
parameters: [
15+
{
16+
name: "systemId",
17+
in: "path",
18+
required: true,
19+
schema: { type: "string" },
20+
description: "UUID of the system",
21+
},
22+
{
23+
name: "name",
24+
in: "path",
25+
required: true,
26+
schema: { type: "string" },
27+
description: "Name of the environment",
28+
},
29+
],
30+
responses: {
31+
"200": {
32+
description: "Environment deleted successfully",
33+
},
34+
},
35+
},
36+
},
37+
},
38+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { NextResponse } from "next/server";
2+
3+
import { and, eq } from "@ctrlplane/db";
4+
import * as schema from "@ctrlplane/db/schema";
5+
import { Permission } from "@ctrlplane/validators/auth";
6+
7+
import { authn, authz } from "~/app/api/v1/auth";
8+
import { request } from "~/app/api/v1/middleware";
9+
10+
export const DELETE = request()
11+
.use(authn)
12+
.use(
13+
authz(({ can, extra: { params } }) =>
14+
can
15+
.perform(Permission.SystemGet)
16+
.on({ type: "system", id: params.systemId }),
17+
),
18+
)
19+
.handle<unknown, { params: { systemId: string; name: string } }>(
20+
async (ctx, { params }) => {
21+
const environment = await ctx.db.query.environment.findFirst({
22+
where: and(
23+
eq(schema.environment.systemId, params.systemId),
24+
eq(schema.environment.name, params.name),
25+
),
26+
});
27+
if (environment == null)
28+
return NextResponse.json(
29+
{ error: "Environment not found" },
30+
{ status: 404 },
31+
);
32+
33+
const env = await ctx.db
34+
.delete(schema.environment)
35+
.where(
36+
and(
37+
eq(schema.environment.systemId, params.systemId),
38+
eq(schema.environment.name, params.name),
39+
),
40+
)
41+
.returning();
42+
return NextResponse.json(env);
43+
},
44+
);

openapi.v1.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,37 @@
16961696
}
16971697
}
16981698
},
1699+
"/v1/systems/{systemId}/environments/{name}": {
1700+
"delete": {
1701+
"summary": "Delete an environment",
1702+
"operationId": "deleteEnvironmentByName",
1703+
"parameters": [
1704+
{
1705+
"name": "systemId",
1706+
"in": "path",
1707+
"required": true,
1708+
"schema": {
1709+
"type": "string"
1710+
},
1711+
"description": "UUID of the system"
1712+
},
1713+
{
1714+
"name": "name",
1715+
"in": "path",
1716+
"required": true,
1717+
"schema": {
1718+
"type": "string"
1719+
},
1720+
"description": "Name of the environment"
1721+
}
1722+
],
1723+
"responses": {
1724+
"200": {
1725+
"description": "Environment deleted successfully"
1726+
}
1727+
}
1728+
}
1729+
},
16991730
"/v1/systems/{systemId}": {
17001731
"get": {
17011732
"summary": "Get a system",

0 commit comments

Comments
 (0)