Skip to content

Commit 2daee7a

Browse files
committed
chore: fix inconsistent member names in POST/PUT and GET in examples
Previously members for POST/PUT had camel case names (but persisted to database in a snake case) and GET returned them in a snake case. There were 2 ways to fix this: - GET should use "name_ru AS nameRu" in SQL query - POST/PUT should accept members in a snake case I used the 2nd approach.
1 parent 4baf098 commit 2daee7a

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
2323
FROM categories
2424
post: >-
2525
INSERT INTO categories(name, slug, created_at, created_by, updated_at, updated_by)
26-
VALUES (:b.name, :b.slug, NOW(), :b.userId, NOW(), :b.userId)
26+
VALUES (:b.name, :b.slug, NOW(), :b.user_id, NOW(), :b.user_id)
2727

2828
- path: /v1/categories/:categoryId
2929
get: >-
@@ -32,7 +32,7 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
3232
WHERE id = :p.categoryId
3333
put: >-
3434
UPDATE categories
35-
SET name = :b.name, name_ru = :b.nameRu, slug = :b.slug, updated_at = NOW(), updated_by = :b.userId
35+
SET name = :b.name, name_ru = :b.name_ru, slug = :b.slug, updated_at = NOW(), updated_by = :b.user_id
3636
WHERE id = :p.categoryId
3737
delete: >-
3838
DELETE
@@ -72,14 +72,14 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
7272
```console
7373
$ curl http://localhost:3000/v1/categories/count
7474
{"counter":0}
75-
$ curl -i -H 'Content-Type: application/json' -d '{"name":"Sport","nameRu":"Спорт","slug":"sport","userId":100}' http://localhost:3000/v1/categories
75+
$ curl -i -H 'Content-Type: application/json' -d '{"name":"Sport","name_ru":"Спорт","slug":"sport","user_id":100}' http://localhost:3000/v1/categories
7676
HTTP/1.1 204 No Content
7777
ETag: W/"a-bAsFyilMr4Ra1hIU5PyoyFRunpI"
7878
Date: Wed, 15 Jul 2020 18:06:33 GMT
7979
Connection: keep-alive
8080
$ curl http://localhost:3000/v1/categories
8181
[{"id":1,"name":"Sport","name_ru":"Спорт","slug":"sport"}]
82-
$ curl -i -H 'Content-Type: application/json' -d '{"name":"Fauna","nameRu":"Фауна","slug":"fauna","userId":101}' -X PUT http://localhost:3000/v1/categories/1
82+
$ curl -i -H 'Content-Type: application/json' -d '{"name":"Fauna","name_ru":"Фауна","slug":"fauna","user_id":101}' -X PUT http://localhost:3000/v1/categories/1
8383
HTTP/1.1 204 No Content
8484
ETag: W/"a-bAsFyilMr4Ra1hIU5PyoyFRunpI"
8585
Date: Wed, 15 Jul 2020 18:06:34 GMT

examples/js/endpoints.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
)
3030
VALUES
3131
( :b.name
32-
, :b.nameRu
32+
, :b.name_ru
3333
, :b.slug
3434
, NOW()
35-
, :b.userId
35+
, :b.user_id
3636
, NOW()
37-
, :b.userId
37+
, :b.user_id
3838
)
3939
4040
- path: /v1/categories/:categoryId
@@ -48,10 +48,10 @@
4848
put: >-
4949
UPDATE categories
5050
SET name = :b.name
51-
, name_ru = :b.nameRu
51+
, name_ru = :b.name_ru
5252
, slug = :b.slug
5353
, updated_at = NOW()
54-
, updated_by = :b.userId
54+
, updated_by = :b.user_id
5555
WHERE id = :p.categoryId
5656
delete: >-
5757
DELETE

examples/js/routes.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ app.get('/v1/categories', (req, res) => {
4848

4949
app.post('/v1/categories', (req, res) => {
5050
pool.query(
51-
'INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :nameRu , :slug , NOW() , :userId , NOW() , :userId )',
52-
{ "name": req.body.name, "nameRu": req.body.nameRu, "slug": req.body.slug, "userId": req.body.userId },
51+
'INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :name_ru , :slug , NOW() , :user_id , NOW() , :user_id )',
52+
{ "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id },
5353
(err, rows, fields) => {
5454
if (err) {
5555
throw err
@@ -78,8 +78,8 @@ app.get('/v1/categories/:categoryId', (req, res) => {
7878

7979
app.put('/v1/categories/:categoryId', (req, res) => {
8080
pool.query(
81-
'UPDATE categories SET name = :name , name_ru = :nameRu , slug = :slug , updated_at = NOW() , updated_by = :userId WHERE id = :categoryId',
82-
{ "name": req.body.name, "nameRu": req.body.nameRu, "slug": req.body.slug, "userId": req.body.userId, "categoryId": req.params.categoryId },
81+
'UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId',
82+
{ "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id, "categoryId": req.params.categoryId },
8383
(err, rows, fields) => {
8484
if (err) {
8585
throw err

tests/crud.robot

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ${SERVER_URL} http://host.docker.internal:3000
1010

1111
** Test Cases ***
1212
POST should create an object
13-
&{payload}= Create Dictionary name=Sport slug=sport userId=1
13+
&{payload}= Create Dictionary name=Sport slug=sport user_id=1
1414
${response}= Post Request api /v1/categories json=${payload}
1515
Status Should Be 204 ${response}
1616
# checks that it was created
@@ -36,7 +36,7 @@ GET should return a list of values
3636
Dictionaries Should Be Equal ${body[0]} ${expected}
3737

3838
PUT should update an object
39-
&{payload}= Create Dictionary name=Fauna nameRu=Фауна slug=fauna userId=1
39+
&{payload}= Create Dictionary name=Fauna name_ru=Фауна slug=fauna user_id=1
4040
${response}= Put Request api /v1/categories/1 json=${payload}
4141
Status Should Be 204 ${response}
4242
# checks that it was updated

0 commit comments

Comments
 (0)