diff --git a/src/ExampleOutput/Middleware.ts b/src/ExampleOutput/Middleware.ts index 006a148..c5c2a5b 100644 --- a/src/ExampleOutput/Middleware.ts +++ b/src/ExampleOutput/Middleware.ts @@ -32,6 +32,30 @@ res.send(toPayload(node)); }); + route.post("/nodes/:id(\\d+)", (req, res) => { + const id = Number.parseInt(req.params.id, undefined); + const existingItem = findSingle(graph, id); + const parsed = parseGraphNodeInput(req.body); + if(!parsed.isSuccessful) { + res.status(400).send(parsed.errorMessage); + return; + } + const inputItem = parsed.value; + if(existingItem == null) { + res.status(404).send("Not found"); + return; + } + else { + // update + existingItem.name = inputItem.name; + existingItem.description = inputItem.description; + DataAccess.save(fileName, graph); + const payload = toPayload(existingItem); + res.status(200).send(payload); + return; + } + }); + 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 006a148..c5c2a5b 100644 --- a/src/ExampleOutput/Middleware.ts +++ b/src/ExampleOutput/Middleware.ts @@ -32,6 +32,30 @@ res.send(toPayload(node)); }); + route.post("/nodes/:id(\\d+)", (req, res) => { + const id = Number.parseInt(req.params.id, undefined); + const existingItem = findSingle(graph, id); + const parsed = parseGraphNodeInput(req.body); + if(!parsed.isSuccessful) { + res.status(400).send(parsed.errorMessage); + return; + } + const inputItem = parsed.value; + if(existingItem == null) { + res.status(404).send("Not found"); + return; + } + else { + // update + existingItem.name = inputItem.name; + existingItem.description = inputItem.description; + DataAccess.save(fileName, graph); + const payload = toPayload(existingItem); + res.status(200).send(payload); + return; + } + }); + route.get("/nodes", (req, res) => { const nodes = graph.nodes; res.send(nodes.map(toPayload)); diff --git a/src/MiddlewareGenerator.ts b/src/MiddlewareGenerator.ts index 96e0704..903d8c1 100644 --- a/src/MiddlewareGenerator.ts +++ b/src/MiddlewareGenerator.ts @@ -51,6 +51,29 @@ res.send(toPayload(item)); }); + route.post("${route}/:${entityId}(\\\\d+)", (req, res) => { + const id = Number.parseInt(req.params.${entityId}, undefined); + const existingItem = findSingle(${col}, id); + const parsed = parse${collection.entities.name}Input(req.body); + if(!parsed.isSuccessful) { + res.status(400).send(parsed.errorMessage); + return; + } + const inputItem = parsed.value; + if(existingItem == null) { + res.status(404).send("Not found"); + return; + } + else { + // update + ${collection.entities.properties.map(p => `existingItem.${p.key} = inputItem.${p.key};`).join("\n\t\t\t")} + DataAccess.save(fileName, ${col}); + const payload = toPayload(existingItem); + res.status(200).send(payload); + return; + } + }); + 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 006a148..c5c2a5b 100644 --- a/src/ExampleOutput/Middleware.ts +++ b/src/ExampleOutput/Middleware.ts @@ -32,6 +32,30 @@ res.send(toPayload(node)); }); + route.post("/nodes/:id(\\d+)", (req, res) => { + const id = Number.parseInt(req.params.id, undefined); + const existingItem = findSingle(graph, id); + const parsed = parseGraphNodeInput(req.body); + if(!parsed.isSuccessful) { + res.status(400).send(parsed.errorMessage); + return; + } + const inputItem = parsed.value; + if(existingItem == null) { + res.status(404).send("Not found"); + return; + } + else { + // update + existingItem.name = inputItem.name; + existingItem.description = inputItem.description; + DataAccess.save(fileName, graph); + const payload = toPayload(existingItem); + res.status(200).send(payload); + return; + } + }); + route.get("/nodes", (req, res) => { const nodes = graph.nodes; res.send(nodes.map(toPayload)); diff --git a/src/MiddlewareGenerator.ts b/src/MiddlewareGenerator.ts index 96e0704..903d8c1 100644 --- a/src/MiddlewareGenerator.ts +++ b/src/MiddlewareGenerator.ts @@ -51,6 +51,29 @@ res.send(toPayload(item)); }); + route.post("${route}/:${entityId}(\\\\d+)", (req, res) => { + const id = Number.parseInt(req.params.${entityId}, undefined); + const existingItem = findSingle(${col}, id); + const parsed = parse${collection.entities.name}Input(req.body); + if(!parsed.isSuccessful) { + res.status(400).send(parsed.errorMessage); + return; + } + const inputItem = parsed.value; + if(existingItem == null) { + res.status(404).send("Not found"); + return; + } + else { + // update + ${collection.entities.properties.map(p => `existingItem.${p.key} = inputItem.${p.key};`).join("\n\t\t\t")} + DataAccess.save(fileName, ${col}); + const payload = toPayload(existingItem); + res.status(200).send(payload); + return; + } + }); + route.get("${route}", (req, res) => { const items = ${col}.items; res.send(items.map(toPayload)); diff --git a/src/OpenApiGenerator.ts b/src/OpenApiGenerator.ts index c060754..b747ebe 100644 --- a/src/OpenApiGenerator.ts +++ b/src/OpenApiGenerator.ts @@ -137,6 +137,7 @@ const route = itemRoute(collection); const path: OpenAPIV3.PathItemObject = { get: generateGetSpecificOperation(collection), + post: generatePostSpecificOperation(collection), }; return [route, path]; } @@ -153,16 +154,7 @@ const entity: Entity = collection.entities; const operation: OpenAPIV3.OperationObject = { tags: [collection.name], - parameters: [ - { - in: "path", - name: idParameterName(entity), - required: true, - schema: { - type: "integer", - }, - }, - ], + parameters: [entityPathParameter(entity)], responses: { "200": { description: "Returns one specific " + entity.name, @@ -182,6 +174,55 @@ return operation; } +function generatePostSpecificOperation(collection: Collection): OpenAPIV3.OperationObject { + const entity: Entity = collection.entities; + const operation: OpenAPIV3.OperationObject = { + tags: [collection.name], + parameters: [entityPathParameter(entity)], + requestBody: { + required: true, + description: "Data to replace a specific " + collection.entities.name, + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/" + collection.entities.name + "Input" + } + } + } + }, + responses: { + "200": { + description: `Successfully updated the ${entity.name}`, + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/" + entity.name, + } + } + } + }, + "400": { + $ref: "#/components/responses/BadRequest", + }, + "404": { + $ref: "#/components/responses/NotFound", + } + } + }; + return operation; +} + +function entityPathParameter(entity: Entity): OpenAPIV3.ParameterObject { + return { + in: "path", + name: idParameterName(entity), + required: true, + schema: { + type: "integer", + }, + }; +} + const NotFoundResponse: OpenAPIV3.ResponseObject = { description: "The specified resource does not exist.", content: {