diff options
Diffstat (limited to 'services')
-rw-r--r-- | services/index.ts | 2 | ||||
-rw-r--r-- | services/votes/votes.class.ts | 14 | ||||
-rw-r--r-- | services/votes/votes.hooks.ts | 15 | ||||
-rw-r--r-- | services/votes/votes.service.ts | 10 |
4 files changed, 41 insertions, 0 deletions
diff --git a/services/index.ts b/services/index.ts index f000837..638fb7a 100644 --- a/services/index.ts +++ b/services/index.ts @@ -2,6 +2,7 @@ import { Application } from '@feathersjs/express'; import Users from './users/users.service'; import Polls from './polls/polls.service'; import Profiles from './profiles/profiles.service'; +import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; export default (app: Application): void => { @@ -9,5 +10,6 @@ export default (app: Application): void => { app.configure(Users); app.configure(Polls); app.configure(Profiles); + app.configure(Votes); }; diff --git a/services/votes/votes.class.ts b/services/votes/votes.class.ts new file mode 100644 index 0000000..d95c148 --- /dev/null +++ b/services/votes/votes.class.ts @@ -0,0 +1,14 @@ +import PollModel from '../../models/polls/poll.model'; +import { PollSchema } from '../../models/polls/poll.schema'; + +export default class Votes { + async create(data: any, params: any): Promise<PollSchema | null> { + return PollModel.findById(params.route.id) + .then(poll => poll?.vote(params.user._id, data.which)) + .catch(e => { + console.error(e); + return null; + }); + } +} + diff --git a/services/votes/votes.hooks.ts b/services/votes/votes.hooks.ts new file mode 100644 index 0000000..2e29008 --- /dev/null +++ b/services/votes/votes.hooks.ts @@ -0,0 +1,15 @@ +import { + convertPollHook +} from '../../hooks/convertPoll'; + +import { authenticate } from '@feathersjs/authentication'; + +export default { + before: { + create: [authenticate('jwt')] + }, + after: { + all: [convertPollHook] + } +}; + diff --git a/services/votes/votes.service.ts b/services/votes/votes.service.ts new file mode 100644 index 0000000..3947d9b --- /dev/null +++ b/services/votes/votes.service.ts @@ -0,0 +1,10 @@ +import { Application } from '@feathersjs/express'; +import Votes from './votes.class'; + +import hooks from './votes.hooks'; + +export default (app: Application): void => { + app.use('/polls/:id/votes/', new Votes()); + app.service('/polls/:id/votes/').hooks(hooks); +}; + |