From 1af98079d43b8bf5da7a32870b7bb60e5f59e554 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 01:47:12 +0300 Subject: feat: add vote function to schema --- models/polls/poll.model.ts | 5 +++++ models/polls/poll.schema.ts | 1 + 2 files changed, 6 insertions(+) diff --git a/models/polls/poll.model.ts b/models/polls/poll.model.ts index 7f6be9a..6f3d088 100644 --- a/models/polls/poll.model.ts +++ b/models/polls/poll.model.ts @@ -1,5 +1,10 @@ import { Model, model } from 'mongoose'; import { PollSchema, pollSchema } from './poll.schema'; +pollSchema.methods.vote = function(userId: string, which: 'left' | 'right'): PollSchema { + this.contents[which].votes.push(userId); + return this.save(); +} + export default model>('Poll', pollSchema); diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts index fd6751c..0caa6b2 100644 --- a/models/polls/poll.schema.ts +++ b/models/polls/poll.schema.ts @@ -11,6 +11,7 @@ export interface PollSchema extends Document { right: ImageDataSchema; }; authorId: string; + vote: (userId: string, which: 'left' | 'right') => void; } export const imageDataSchema = { -- cgit v1.2.3