diff --git a/src/ExampleOutput/Middleware.ts b/src/ExampleOutput/Middleware.ts index c5c2a5b..23c2f70 100644 --- a/src/ExampleOutput/Middleware.ts +++ b/src/ExampleOutput/Middleware.ts @@ -1,6 +1,6 @@ import express from "express"; import * as DataAccess from "./DataAccess"; -import { append, findSingle, GraphNodeInput, toPayload } from "./Model"; +import { append, findSingle, GraphNodeInput, remove, toPayload } from "./Model"; import { andThen, compose, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, resolve, Result } from "./util"; @@ -56,6 +56,13 @@ } }); + route.delete("/nodes/:id(\\d+)", (req, res) => { + const id = Number.parseInt(req.params.id, undefined); + remove(graph, id); + DataAccess.save(fileName, graph); + res.status(204).send(); + }); + route.get("/nodes", (req, res) => { const nodes = graph.nodes; res.send(nodes.map(toPayload)); diff --git a/src/ExampleOutput/Middleware.ts b/src/ExampleOutput/Middleware.ts index c5c2a5b..23c2f70 100644 --- a/src/ExampleOutput/Middleware.ts +++ b/src/ExampleOutput/Middleware.ts @@ -1,6 +1,6 @@ import express from "express"; import * as DataAccess from "./DataAccess"; -import { append, findSingle, GraphNodeInput, toPayload } from "./Model"; +import { append, findSingle, GraphNodeInput, remove, toPayload } from "./Model"; import { andThen, compose, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, resolve, Result } from "./util"; @@ -56,6 +56,13 @@ } }); + route.delete("/nodes/:id(\\d+)", (req, res) => { + const id = Number.parseInt(req.params.id, undefined); + remove(graph, id); + DataAccess.save(fileName, graph); + res.status(204).send(); + }); + route.get("/nodes", (req, res) => { const nodes = graph.nodes; res.send(nodes.map(toPayload)); diff --git a/src/ExampleOutput/Model.ts b/src/ExampleOutput/Model.ts index 146a8c3..c8372bd 100644 --- a/src/ExampleOutput/Model.ts +++ b/src/ExampleOutput/Model.ts @@ -33,3 +33,9 @@ collection.nodes.push(newItem); return newItem; } + +export function remove(collection: Graph, id: number): void { + const index = collection.nodes.findIndex(i => i.id === id); + if(index === -1) return; + collection.nodes.splice(index, 1); +} diff --git a/src/ExampleOutput/Middleware.ts b/src/ExampleOutput/Middleware.ts index c5c2a5b..23c2f70 100644 --- a/src/ExampleOutput/Middleware.ts +++ b/src/ExampleOutput/Middleware.ts @@ -1,6 +1,6 @@ import express from "express"; import * as DataAccess from "./DataAccess"; -import { append, findSingle, GraphNodeInput, toPayload } from "./Model"; +import { append, findSingle, GraphNodeInput, remove, toPayload } from "./Model"; import { andThen, compose, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, resolve, Result } from "./util"; @@ -56,6 +56,13 @@ } }); + route.delete("/nodes/:id(\\d+)", (req, res) => { + const id = Number.parseInt(req.params.id, undefined); + remove(graph, id); + DataAccess.save(fileName, graph); + res.status(204).send(); + }); + route.get("/nodes", (req, res) => { const nodes = graph.nodes; res.send(nodes.map(toPayload)); diff --git a/src/ExampleOutput/Model.ts b/src/ExampleOutput/Model.ts index 146a8c3..c8372bd 100644 --- a/src/ExampleOutput/Model.ts +++ b/src/ExampleOutput/Model.ts @@ -33,3 +33,9 @@ collection.nodes.push(newItem); return newItem; } + +export function remove(collection: Graph, id: number): void { + const index = collection.nodes.findIndex(i => i.id === id); + if(index === -1) return; + collection.nodes.splice(index, 1); +} diff --git a/src/MiddlewareGenerator.ts b/src/MiddlewareGenerator.ts index 903d8c1..7e95816 100644 --- a/src/MiddlewareGenerator.ts +++ b/src/MiddlewareGenerator.ts @@ -27,7 +27,7 @@ function defineImports(collection: Collection): string { return `import express from "express"; import * as DataAccess from "./${dataAccessModuleName(collection)}"; -import { append, findSingle, ${collection.entities.name}Input, toPayload } from "./${modelModuleName(collection)}"; +import { append, findSingle, ${collection.entities.name}Input, remove, toPayload } from "./${modelModuleName(collection)}"; import { andThen, compose, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, resolve, Result } from "./util";`; } @@ -74,6 +74,13 @@ } }); + route.delete("${route}/:${entityId}(\\\\d+)", (req, res) => { + const id = Number.parseInt(req.params.${entityId}, undefined); + remove(${col}, id); + DataAccess.save(fileName, ${col}); + res.status(204).send(); + }); + route.get("${route}", (req, res) => { const items = ${col}.items; res.send(items.map(toPayload)); diff --git a/src/ExampleOutput/Middleware.ts b/src/ExampleOutput/Middleware.ts index c5c2a5b..23c2f70 100644 --- a/src/ExampleOutput/Middleware.ts +++ b/src/ExampleOutput/Middleware.ts @@ -1,6 +1,6 @@ import express from "express"; import * as DataAccess from "./DataAccess"; -import { append, findSingle, GraphNodeInput, toPayload } from "./Model"; +import { append, findSingle, GraphNodeInput, remove, toPayload } from "./Model"; import { andThen, compose, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, resolve, Result } from "./util"; @@ -56,6 +56,13 @@ } }); + route.delete("/nodes/:id(\\d+)", (req, res) => { + const id = Number.parseInt(req.params.id, undefined); + remove(graph, id); + DataAccess.save(fileName, graph); + res.status(204).send(); + }); + route.get("/nodes", (req, res) => { const nodes = graph.nodes; res.send(nodes.map(toPayload)); diff --git a/src/ExampleOutput/Model.ts b/src/ExampleOutput/Model.ts index 146a8c3..c8372bd 100644 --- a/src/ExampleOutput/Model.ts +++ b/src/ExampleOutput/Model.ts @@ -33,3 +33,9 @@ collection.nodes.push(newItem); return newItem; } + +export function remove(collection: Graph, id: number): void { + const index = collection.nodes.findIndex(i => i.id === id); + if(index === -1) return; + collection.nodes.splice(index, 1); +} diff --git a/src/MiddlewareGenerator.ts b/src/MiddlewareGenerator.ts index 903d8c1..7e95816 100644 --- a/src/MiddlewareGenerator.ts +++ b/src/MiddlewareGenerator.ts @@ -27,7 +27,7 @@ function defineImports(collection: Collection): string { return `import express from "express"; import * as DataAccess from "./${dataAccessModuleName(collection)}"; -import { append, findSingle, ${collection.entities.name}Input, toPayload } from "./${modelModuleName(collection)}"; +import { append, findSingle, ${collection.entities.name}Input, remove, toPayload } from "./${modelModuleName(collection)}"; import { andThen, compose, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, resolve, Result } from "./util";`; } @@ -74,6 +74,13 @@ } }); + route.delete("${route}/:${entityId}(\\\\d+)", (req, res) => { + const id = Number.parseInt(req.params.${entityId}, undefined); + remove(${col}, id); + DataAccess.save(fileName, ${col}); + res.status(204).send(); + }); + route.get("${route}", (req, res) => { const items = ${col}.items; res.send(items.map(toPayload)); diff --git a/src/ModelGenerator.ts b/src/ModelGenerator.ts index f4afba6..2ca7487 100644 --- a/src/ModelGenerator.ts +++ b/src/ModelGenerator.ts @@ -20,6 +20,7 @@ definePayload(collection), defineFindFunction(collection), defineAppendFunction(collection), + defineRemoveFunction(collection), ]; return { location: fileName, @@ -74,3 +75,11 @@ return newItem; }`; } + +function defineRemoveFunction(collection: Collection): string { + return `export function remove(collection: ${collection.name}, id: number): void { + const index = collection.items.findIndex(i => i.id === id); + if(index === -1) return; + collection.items.splice(index, 1); +}`; +} diff --git a/src/ExampleOutput/Middleware.ts b/src/ExampleOutput/Middleware.ts index c5c2a5b..23c2f70 100644 --- a/src/ExampleOutput/Middleware.ts +++ b/src/ExampleOutput/Middleware.ts @@ -1,6 +1,6 @@ import express from "express"; import * as DataAccess from "./DataAccess"; -import { append, findSingle, GraphNodeInput, toPayload } from "./Model"; +import { append, findSingle, GraphNodeInput, remove, toPayload } from "./Model"; import { andThen, compose, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, resolve, Result } from "./util"; @@ -56,6 +56,13 @@ } }); + route.delete("/nodes/:id(\\d+)", (req, res) => { + const id = Number.parseInt(req.params.id, undefined); + remove(graph, id); + DataAccess.save(fileName, graph); + res.status(204).send(); + }); + route.get("/nodes", (req, res) => { const nodes = graph.nodes; res.send(nodes.map(toPayload)); diff --git a/src/ExampleOutput/Model.ts b/src/ExampleOutput/Model.ts index 146a8c3..c8372bd 100644 --- a/src/ExampleOutput/Model.ts +++ b/src/ExampleOutput/Model.ts @@ -33,3 +33,9 @@ collection.nodes.push(newItem); return newItem; } + +export function remove(collection: Graph, id: number): void { + const index = collection.nodes.findIndex(i => i.id === id); + if(index === -1) return; + collection.nodes.splice(index, 1); +} diff --git a/src/MiddlewareGenerator.ts b/src/MiddlewareGenerator.ts index 903d8c1..7e95816 100644 --- a/src/MiddlewareGenerator.ts +++ b/src/MiddlewareGenerator.ts @@ -27,7 +27,7 @@ function defineImports(collection: Collection): string { return `import express from "express"; import * as DataAccess from "./${dataAccessModuleName(collection)}"; -import { append, findSingle, ${collection.entities.name}Input, toPayload } from "./${modelModuleName(collection)}"; +import { append, findSingle, ${collection.entities.name}Input, remove, toPayload } from "./${modelModuleName(collection)}"; import { andThen, compose, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, resolve, Result } from "./util";`; } @@ -74,6 +74,13 @@ } }); + route.delete("${route}/:${entityId}(\\\\d+)", (req, res) => { + const id = Number.parseInt(req.params.${entityId}, undefined); + remove(${col}, id); + DataAccess.save(fileName, ${col}); + res.status(204).send(); + }); + route.get("${route}", (req, res) => { const items = ${col}.items; res.send(items.map(toPayload)); diff --git a/src/ModelGenerator.ts b/src/ModelGenerator.ts index f4afba6..2ca7487 100644 --- a/src/ModelGenerator.ts +++ b/src/ModelGenerator.ts @@ -20,6 +20,7 @@ definePayload(collection), defineFindFunction(collection), defineAppendFunction(collection), + defineRemoveFunction(collection), ]; return { location: fileName, @@ -74,3 +75,11 @@ return newItem; }`; } + +function defineRemoveFunction(collection: Collection): string { + return `export function remove(collection: ${collection.name}, id: number): void { + const index = collection.items.findIndex(i => i.id === id); + if(index === -1) return; + collection.items.splice(index, 1); +}`; +} diff --git a/src/OpenApiGenerator.ts b/src/OpenApiGenerator.ts index b747ebe..c455246 100644 --- a/src/OpenApiGenerator.ts +++ b/src/OpenApiGenerator.ts @@ -80,6 +80,7 @@ function generateGetAllOperation(collection: Collection): OpenAPIV3.OperationObject { const operation: OpenAPIV3.OperationObject = { + summary: "Returns all " + collection.name, tags: [collection.name], responses: { "200": { @@ -102,6 +103,7 @@ function generateCreateOperation(collection: Collection): OpenAPIV3.OperationObject { const operation: OpenAPIV3.OperationObject = { + summary: "Creates a new " + collection.entities.name, tags: [collection.name], requestBody: { required: true, @@ -138,6 +140,7 @@ const path: OpenAPIV3.PathItemObject = { get: generateGetSpecificOperation(collection), post: generatePostSpecificOperation(collection), + delete: generateDeleteSpecific(collection), }; return [route, path]; } @@ -153,6 +156,7 @@ function generateGetSpecificOperation(collection: Collection): OpenAPIV3.OperationObject { const entity: Entity = collection.entities; const operation: OpenAPIV3.OperationObject = { + summary: "Returns a specific " + entity.name, tags: [collection.name], parameters: [entityPathParameter(entity)], responses: { @@ -177,6 +181,7 @@ function generatePostSpecificOperation(collection: Collection): OpenAPIV3.OperationObject { const entity: Entity = collection.entities; const operation: OpenAPIV3.OperationObject = { + summary: "Replaces a specific " + entity.name, tags: [collection.name], parameters: [entityPathParameter(entity)], requestBody: { @@ -212,6 +217,21 @@ return operation; } +function generateDeleteSpecific(collection: Collection): OpenAPIV3.OperationObject { + const entity: Entity = collection.entities; + const operation: OpenAPIV3.OperationObject = { + summary: "Deletes a specific " + entity.name, + tags: [collection.name], + parameters: [entityPathParameter(entity)], + responses: { + "204": { + description: `Successfully deleted the ${entity.name} or it never existed`, + } + } + }; + return operation; +} + function entityPathParameter(entity: Entity): OpenAPIV3.ParameterObject { return { in: "path", @@ -253,6 +273,7 @@ info: { title: "API for " + description.collections.map(c => c.name).join(", "), version: "1.0.0", + description: "Generated by ServerGenerator at " + new Date().toISOString(), }, servers: [{ url: "http://localhost:8080" }], components: {