Skip to content

Commit

Permalink
add delete environment by name
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 20, 2024
1 parent 182ee30 commit d3ca4da
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Swagger } from "atlassian-openapi";

export const openapi: Swagger.SwaggerV3 = {
openapi: "3.0.0",
info: {
title: "Ctrlplane API",
version: "1.0.0",
},
paths: {
"/v1/systems/{systemId}/environments/{name}": {
delete: {
summary: "Delete an environment",
operationId: "deleteEnvironmentByName",
parameters: [
{
name: "systemId",
in: "path",
required: true,
schema: { type: "string" },
description: "UUID of the system",
},
{
name: "name",
in: "path",
required: true,
schema: { type: "string" },
description: "Name of the environment",
},
],
responses: {
"200": {
description: "Environment deleted successfully",
},
},
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { NextResponse } from "next/server";

import { and, eq } from "@ctrlplane/db";
import * as schema from "@ctrlplane/db/schema";
import { Permission } from "@ctrlplane/validators/auth";

import { authn, authz } from "~/app/api/v1/auth";
import { request } from "~/app/api/v1/middleware";

export const DELETE = request()
.use(authn)
.use(
authz(({ can, extra: { params } }) =>
can
.perform(Permission.SystemGet)
.on({ type: "system", id: params.systemId }),
),
)
.handle<unknown, { params: { systemId: string; name: string } }>(
async (ctx, { params }) => {
const environment = await ctx.db.query.environment.findFirst({
where: and(
eq(schema.environment.systemId, params.systemId),
eq(schema.environment.name, params.name),
),
});
if (environment == null)
return NextResponse.json(
{ error: "Environment not found" },
{ status: 404 },
);

const env = await ctx.db
.delete(schema.environment)
.where(
and(
eq(schema.environment.systemId, params.systemId),
eq(schema.environment.name, params.name),
),
)
.returning();
return NextResponse.json(env);
},
);
31 changes: 31 additions & 0 deletions openapi.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,37 @@
}
}
},
"/v1/systems/{systemId}/environments/{name}": {
"delete": {
"summary": "Delete an environment",
"operationId": "deleteEnvironmentByName",
"parameters": [
{
"name": "systemId",
"in": "path",
"required": true,
"schema": {
"type": "string"
},
"description": "UUID of the system"
},
{
"name": "name",
"in": "path",
"required": true,
"schema": {
"type": "string"
},
"description": "Name of the environment"
}
],
"responses": {
"200": {
"description": "Environment deleted successfully"
}
}
}
},
"/v1/systems/{systemId}": {
"get": {
"summary": "Get a system",
Expand Down

0 comments on commit d3ca4da

Please sign in to comment.