Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Commit c3a110a

Browse files
committed
[Feature] add Candidate route
1 parent 7978a9a commit c3a110a

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Router } from 'express';
22
import auth from './routes/auth';
33
import user from './routes/user';
4+
import candidate from './routes/candidate';
45

56
// guaranteed to get dependencies
67
export default () => {
78
const app = Router();
89
auth(app);
910
user(app);
11+
candidate(app);
1012

1113
return app;
1214
};

src/api/routes/auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Router, Request, Response, NextFunction } from 'express';
22
import { Container } from 'typedi';
3+
import { celebrate, Joi } from 'celebrate';
4+
5+
import middlewares from '../middlewares';
36
import AuthService from '../../services/auth';
47
import { IUserInputDTO } from '../../interfaces/IUser';
5-
import middlewares from '../middlewares';
6-
import { celebrate, Joi } from 'celebrate';
78
import { getLogger } from '../../loaders/dependencyInjector';
89

910
const route = Router();

src/api/routes/candidate.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Router, Request, Response, NextFunction } from 'express';
2+
import { Container } from 'typedi';
3+
import { celebrate, Joi } from 'celebrate';
4+
5+
import CandidateService from '../../services/candidate';
6+
import { getLogger } from '../../loaders/dependencyInjector';
7+
8+
const route = Router();
9+
10+
export default (app: Router) => {
11+
app.use('/candidates', route);
12+
13+
route.get('/', async (req: Request, res: Response) => {
14+
const logger = getLogger();
15+
logger.debug('Calling Candidate-List endpoint');
16+
const candidateServiceInstance = Container.get(CandidateService);
17+
const candidates = await candidateServiceInstance.List();
18+
return res.json(candidates).status(200);
19+
});
20+
21+
route.post(
22+
'/',
23+
celebrate({
24+
body: Joi.object({
25+
name: Joi.string().required(),
26+
}),
27+
}),
28+
async (req: Request, res: Response, next: NextFunction) => {
29+
const logger = getLogger();
30+
logger.debug('Calling Candidate-Create endpoint with body: %o', req.body);
31+
try {
32+
const { name } = req.body;
33+
const candidateServiceInstance = Container.get(CandidateService);
34+
const { candidate } = await candidateServiceInstance.Create(name);
35+
return res.json({ candidate }).status(200);
36+
} catch (e) {
37+
logger.error('🔥 error: %o', e);
38+
return next(e);
39+
}
40+
},
41+
);
42+
43+
route.put('/:id/vote/', async (req: Request, res: Response, next: NextFunction) => {
44+
const logger = getLogger();
45+
logger.debug('Calling Candidate-Vote endpoint');
46+
try {
47+
const { id } = req.params;
48+
const candidateServiceInstance = Container.get(CandidateService);
49+
await candidateServiceInstance.Vote(id);
50+
return res.json({ message: 'vote success' }).status(200);
51+
} catch (e) {
52+
logger.error('🔥 error: %o', e);
53+
return next(e);
54+
}
55+
});
56+
};

0 commit comments

Comments
 (0)