From fcb0651579a003f88a812e8c07d99b5b8eeed811 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 24 Nov 2023 12:30:28 +0530 Subject: [PATCH] feat: add stats support in slap --- src/models/stats.js | 47 +++++++++++++++++++++++++++++++++++++++++++ src/models/streams.js | 4 ++++ src/ws/channels.js | 3 ++- src/ws/crud.js | 29 ++++++++++++++++++++++++-- 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/models/stats.js diff --git a/src/models/stats.js b/src/models/stats.js new file mode 100644 index 0000000..89ca982 --- /dev/null +++ b/src/models/stats.js @@ -0,0 +1,47 @@ +const { Schema, model } = require('mongoose') + +const stats = new Schema({ + name: { + type: String, + required: true, + }, + name_arabic: { + type: String, + }, + name_jpn: { + type: String, + }, + image: { + type: String, + required: true, + }, + image_full: { + type: Boolean, + default: false, + required: true, + }, + win: { + type: String, + required: true, + }, + goals: { + type: String, + required: true, + }, + shots: { + type: String, + required: true, + }, + saves: { + type: String, + required: true, + }, + assists: { + type: String, + required: true, + }, +}) + +const Stat = model('Stats', stats) + +module.exports = Stat diff --git a/src/models/streams.js b/src/models/streams.js index e3910bc..4b916ef 100644 --- a/src/models/streams.js +++ b/src/models/streams.js @@ -27,6 +27,10 @@ const streams = new Schema({ type: Schema.Types.ObjectId, ref: 'Casters', }], + stats: [{ + type: Schema.Types.ObjectId, + ref: 'Stats' + }], }) const Stream = model('Streams', streams) diff --git a/src/ws/channels.js b/src/ws/channels.js index 0dc0415..5a21774 100644 --- a/src/ws/channels.js +++ b/src/ws/channels.js @@ -2,6 +2,7 @@ const channels = [ 'rosters', 'matches', 'streams', + 'stats', 'events', 'casters', 'hosts', @@ -29,4 +30,4 @@ module.exports = { channelEvents, channels, events, -}; \ No newline at end of file +}; diff --git a/src/ws/crud.js b/src/ws/crud.js index aa7381d..a9a8832 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -1,6 +1,7 @@ const events = require('../models/events') const rosters = require('../models/rosters') const matches = require('../models/matches') +const stats = require('../models/stats') const streams = require('../models/streams') const casters = require('../models/casters') const hosts = require('../models/hosts') @@ -75,6 +76,29 @@ const matchFns = { } } +const statsFns = { + getAll: async () => { + return await stats.find().exec() + }, + getById: async (id) => { + return await stats.findById(id).exec() + }, + update: async(id, data) => { + return await stats.findByIdAndUpdate(id, data).exec() + }, + delete: async (id) => { + return await stats.findByIdAndDelete(id).exec() + }, + create: async (data) => { + try { + const ev = new stats(data) + await ev.save(); + } catch (e) { + console.warn(e) + } + } +} + const streamFns = { getAll: async () => { return await streams.find().exec() @@ -83,13 +107,13 @@ const streamFns = { return await streams.find().populate('event').populate({ path: 'matches', populate: [{ path: 'orange' }, { path: 'blue' }], - }).populate('casters').populate('hosts').lean().exec() + }).populate('casters').populate('hosts').populate('stats').lean().exec() }, getById: async (id) => { return await streams.findById(id).populate('event').populate({ path: 'matches', populate: [{ path: 'orange' }, { path: 'blue' }], - }).populate('casters').populate('hosts').lean().exec() + }).populate('casters').populate('hosts').populate('stats').lean().exec() }, update: async(id, data) => { return await streams.findByIdAndUpdate(id, data).exec() @@ -189,6 +213,7 @@ module.exports = { 'events': eventFns, 'rosters': rosterFns, 'matches': matchFns, + 'stats': statsFns, 'streams': streamFns, 'casters': casterFns, 'hosts': hostFns,