Skip to content

Commit

Permalink
fix(cli): playground describes its endpoints upon startup
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-molak committed Jul 3, 2020
1 parent e406099 commit 0b55c12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
43 changes: 22 additions & 21 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function api (pathToDbJson: string) {
// storage: 'InMemoryStorageService',
});
})

// get all items
.get('/api/todos', (req: express.Request, res: express.Response) => {
res.json(db.get('todos').value());
Expand All @@ -51,27 +52,6 @@ export function api (pathToDbJson: string) {

res.json(serialised);
})
// update an item
.put('/api/todos/:id', (req: express.Request, res: express.Response) => {

const serialised = Todo.fromJSON(req.body).toJSON();

db.get('todos')
.find({ id: req.params.id })
.assign(serialised)
.write();

res.json(serialised);
})
// remove an item
.delete('/api/todos/:id', (req: express.Request, res: express.Response) => {

db.get('todos')
.remove({ id: req.params.id })
.write();

res.status(200).send();
})
// change status of all todos
.patch('/api/todos', (req: express.Request, res: express.Response) => {

Expand Down Expand Up @@ -103,5 +83,26 @@ export function api (pathToDbJson: string) {

res.json(db.get('todos').value());
})
// update an item
.put('/api/todos/:id', (req: express.Request, res: express.Response) => {

const serialised = Todo.fromJSON(req.body).toJSON();

db.get('todos')
.find({ id: req.params.id })
.assign(serialised)
.write();

res.json(serialised);
})
// remove an item
.delete('/api/todos/:id', (req: express.Request, res: express.Response) => {

db.get('todos')
.remove({ id: req.params.id })
.write();

res.status(200).send();
})
;
}
15 changes: 9 additions & 6 deletions src/cli/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ export = {
- User Interface - http://localhost:${ argv.port }
API:
- Health check - GET http://localhost:${ argv.port }/api/health
- List items - GET http://localhost:${ argv.port }/api/todos
- Add an item - POST http://localhost:${ argv.port }/api/todos { title: string, completed: boolean }
- Update an item - PUT http://localhost:${ argv.port }/api/todos { title: string, completed: boolean }
- Remove an item - DELETE http://localhost:${ argv.port }/api/todos/:id
- Remove all - DELETE http://localhost:${ argv.port }/api/todos
- Health check - GET http://localhost:${ argv.port }/api/health
- List items - GET http://localhost:${ argv.port }/api/todos
- Add an item - POST http://localhost:${ argv.port }/api/todos { title: string, completed: boolean }
- Update all items - PATCH http://localhost:${ argv.port }/api/todos { completed: boolean }
- Remove all items - DELETE http://localhost:${ argv.port }/api/todos
- Update an item - PUT http://localhost:${ argv.port }/api/todos/:id { title: string, completed: boolean }
- Remove an item - DELETE http://localhost:${ argv.port }/api/todos/:id
`);
});
}),
Expand Down

0 comments on commit 0b55c12

Please sign in to comment.