From 25ce44d9cf940dd66f74afddde186104ad0c4b5a Mon Sep 17 00:00:00 2001 From: Brian McGonagill Date: Fri, 26 Aug 2022 11:55:42 -0500 Subject: [PATCH] Adding Menu Support with methods. --- imports/api/menu.js | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 imports/api/menu.js diff --git a/imports/api/menu.js b/imports/api/menu.js new file mode 100644 index 0000000..341a1d0 --- /dev/null +++ b/imports/api/menu.js @@ -0,0 +1,77 @@ +import { Meteor } from 'meteor/meteor'; +import { Mongo } from 'meteor/mongo'; +import { check } from 'meteor/check'; + +export const Menus = new Mongo.Collection('menus'); + +Menus.allow({ + insert: function(userId, doc){ + // if use id exists, allow insert + return !!userId; + }, +}); + +Meteor.methods({ + 'add.menu' (menuName) { + check(menuName, String); + + if (!this.userId) { + throw new Meteor.Error('You are not allowed to add menus. Make sure you are logged in with valid user credentials.'); + } + + return Menus.insert({ + menuName: menuName, + menuOwner: this.userId, + menuComplete: false, + }); + }, + 'edit.menu' (menuId, menuName) { + check(menuId, String); + check(menuName, String); + + if (!this.userId) { + throw new Meteor.Error('You are not allowed to edit menus. Make sure you are logged in with valid user credentials.'); + } + + return Menus.update({ _id: menuId }, { + $set: { + menuName: menuName, + } + }); + }, + 'delete.menu' (menuId) { + check(menuId, String); + + if (!this.userId) { + throw new Meteor.Error('You are not allowed to delete menus. Make sure you are logged in with valid user credentials.'); + } + + return Menus.remove({ _id: menuId }); + }, + 'markMenu.complete' (menuId) { + check(menuId, String); + + if (!this.userId) { + throw new Meteor.Error('You are not allowed to mark menus complete. Make sure you are logged in with valid user credentials.'); + } + + return Menus.update({ _id: menuId }, { + $set: { + menuComplete: true, + } + }); + }, + 'markMenu.notComplete' (menuId) { + check(menuId, String); + + if (!this.userId) { + throw new Meteor.Error('You are not allowed to mark menus not complete. Make sure you are logged in with valid user credentials.'); + } + + return Menus.update({ _id: menuId }, { + $set: { + menuComplete: false, + } + }); + } +}); \ No newline at end of file