From 4909dda29566710793b8445ab4426102ca4a0324 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 01:42:43 +0300 Subject: feat: create Votes service --- services/index.ts | 2 ++ services/votes/votes.class.ts | 14 ++++++++++++++ services/votes/votes.hooks.ts | 15 +++++++++++++++ services/votes/votes.service.ts | 10 ++++++++++ 4 files changed, 41 insertions(+) create mode 100644 services/votes/votes.class.ts create mode 100644 services/votes/votes.hooks.ts create mode 100644 services/votes/votes.service.ts (limited to 'services') 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 { + 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); +}; + -- cgit v1.2.3