Skip to content

Commit f405a4c

Browse files
committed
feat: add support for POST requests
1 parent ecb3395 commit f405a4c

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
2424
JOIN series s
2525
ON s.id = cs.series_id
2626
WHERE cs.collection_id = :collectionId
27+
28+
- path: /v1/categories
29+
post: >-
30+
INSERT INTO categories(name, slug, created_at, created_by, updated_at, updated_by)
31+
VALUES (:name, :slug, NOW(), :userId, NOW(), :userId)
2732
```
2833

2934
1. Generate code
@@ -58,4 +63,6 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
5863
3
5964
$ curl http://localhost:3000/v1/collections/1/categories/count
6065
1
66+
$ curl -H 'Content-Type: application/json' -d '{"name":"Sport","slug":"sport","userId":100}' http://localhost:3000/v1/categories
67+
OK
6168
```

examples/js/app.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ app.get('/v1/collections/:collectionId/categories/count', (req, res) => {
5555
})
5656

5757
app.post('/v1/categories', (req, res) => {
58-
res.sendStatus(200)
58+
pool.query(
59+
'INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :nameRu , :slug , NOW() , :userId , NOW() , :userId )',
60+
{ "name": req.body.name, "nameRu": req.body.nameRu, "slug": req.body.slug, "userId": req.body.userId },
61+
(err, rows, fields) => {
62+
if (err) {
63+
throw err
64+
}
65+
res.sendStatus(200)
66+
}
67+
)
5968
})
6069

6170
const port = process.env.PORT || 3000;

examples/js/endpoints.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- path: /v1/categories
1313
post: >-
1414
INSERT
15-
INTO categories2
15+
INTO categories
1616
( name
1717
, name_ru
1818
, slug
@@ -23,7 +23,7 @@
2323
)
2424
VALUES
2525
( :name
26-
, :name_ru
26+
, :nameRu
2727
, :slug
2828
, NOW()
2929
, :userId

src/templates/app.js.ejs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,18 @@ app.get('<%- endpoint.path %>', (req, res) => {
4646
<%
4747
}
4848
if (endpoint.hasOwnProperty('post')) {
49+
const params = extractParams(endpoint.post);
4950
%>
5051
app.post('<%- endpoint.path %>', (req, res) => {
51-
res.sendStatus(200)
52+
pool.query(
53+
<%- formatQuery(endpoint.post) %>,<%- params.length > 0 ? '\n ' + formatParams(params, 'req.body') + ',' : '' %>
54+
(err, rows, fields) => {
55+
if (err) {
56+
throw err
57+
}
58+
res.sendStatus(200)
59+
}
60+
)
5261
})
5362
<%
5463
}

0 commit comments

Comments
 (0)