diff --git a/backend/services/api.js b/backend/services/api.js index e0cefd38..8d27dad7 100644 --- a/backend/services/api.js +++ b/backend/services/api.js @@ -2,6 +2,7 @@ const mongoose = require("mongoose"); const Schema = mongoose.Schema; const express = require('express'); +const { getIo } = require('../io/socket'); /** * Class for managing the backend api @@ -192,6 +193,10 @@ class BackendModel { return this.#_mongoSchema.findOne(params); } + findOneAndUpdate(findParams, updateParams){ + return this.#_mongoSchema.findOneAndUpdate(findParams, updateParams); + } + /** * Finds an element by id and returns a promise with the result of the query * @param {String} id @@ -243,6 +248,10 @@ class BackendSocket { on(msg, callback){ this.#_internalSocket[`${this.#_prefix}/${msg}`] = callback; } + + emit(campaign, msg, data = {}){ + getIo().to(campaign).emit(msg, data); + } } function ParseSchema(schema){ diff --git a/plugins/dnd-5e/backend/main.js b/plugins/dnd-5e/backend/main.js index 18401a9f..04e38535 100644 --- a/plugins/dnd-5e/backend/main.js +++ b/plugins/dnd-5e/backend/main.js @@ -38,6 +38,41 @@ function Main(api){ itemModel.find({campaign}).select('-data').lean().then(data => { res.json({status: "ok", data}); }); + }); + + dndModule.router.post('/item/create', (req, res) => { + const campaign = req.campaign; + let data = req.body.data; + + if(!(data.type && data.name)) { + res.json({status: "error", msg: "args"}); + return; + } + + itemModel.create({campaign, type: data.type, name: data.name, info: {}, data: {}}).then(item => { + dndModule.socket.emit(campaign, 'update-concepts'); + res.json({status: "ok", item}); + }); + }); + + dndModule.router.get('/item/get', (req, res) => { + const campaign = req.campaign; + let id = req.query.id; + + itemModel.findOne({_id: id, campaign}).lean().then(concept => { + res.json({status: "ok", concept}); + }); + }) + + dndModule.router.put('/item/update', (req, res) => { + const campaign = req.campaign; + let id = req.query.id; + + itemModel.findOneAndUpdate({_id: id, campaign}, req.body.concept).then(result => { + if(req.query.fireUpdate) dndModule.socket.emit(campaign, 'update-concepts'); + dndModule.socket.emit(campaign).emit('update-concept', id); + res.json({status: "ok"}); + }); }) Api.socket.on("test", () => console.log("test")); diff --git a/plugins/dnd-5e/client/data.js b/plugins/dnd-5e/client/data.js index 1b4b7c76..9ffcd2de 100644 --- a/plugins/dnd-5e/client/data.js +++ b/plugins/dnd-5e/client/data.js @@ -7,11 +7,11 @@ import { GetCampaign } from "@/services/Dragonroll"; let data = reactive({}); let Api = Global('dnd-5e').Api; -let dndModule = Global('dnd-5e')['dndModule']; +let dndModule = Global('dnd-5e').DndModule; function InitData(){ Api = Global('dnd-5e').Api; - dndModule = Global('dnd-5e')['dndModule']; + dndModule = Global('dnd-5e').DndModule; data.value = { concepts: [] diff --git a/plugins/dnd-5e/client/main.js b/plugins/dnd-5e/client/main.js index 72de6e30..ce3be6da 100644 --- a/plugins/dnd-5e/client/main.js +++ b/plugins/dnd-5e/client/main.js @@ -70,7 +70,7 @@ function Main(Api){ Api.registerModule(dndModule); - Global('dnd-5e')['dndModule'] = dndModule; + Global('dnd-5e').DndModule = dndModule; } export { diff --git a/plugins/dnd-5e/client/views/ItemSheet.vue b/plugins/dnd-5e/client/views/ItemSheet.vue index ccfe421d..9c3a03c4 100644 --- a/plugins/dnd-5e/client/views/ItemSheet.vue +++ b/plugins/dnd-5e/client/views/ItemSheet.vue @@ -1,20 +1,23 @@