-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
apps/webservice/src/app/api/v1/systems/[systemId]/environments/[name]/openapi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; |
44 changes: 44 additions & 0 deletions
44
apps/webservice/src/app/api/v1/systems/[systemId]/environments/[name]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters