Commit ca63c449 authored by Tom Käsler's avatar Tom Käsler

add json-routes to enable HTTP routes on server

export _ids for cards to update cards
parent d50499f6
......@@ -72,3 +72,4 @@ ros:publish-counts
mikowals:batch-insert
mizzao:jquery-ui
dsyko:jquery-ui-touch-punch
simple:json-routes
......@@ -232,6 +232,7 @@ service-configuration@1.0.11
session@1.1.7
shell-server@0.3.1
shim-common@0.1.0
simple:json-routes@2.1.0
simple:katex@1.1.0
socket-stream-client@0.1.0
spacebars@1.0.15
......
import {Mongo} from "meteor/mongo";
import {SimpleSchema} from "meteor/aldeed:simple-schema";
import {JsonRoutes} from "meteor/simple:json-routes";
import {Cards} from "./cards.js";
export const APIAccess = new Mongo.Collection("apiAccess");
/*
* function to automatically generate the mongo modifier for an update.
* checks which attributes have changed.
*/
function mongoReplacementModifier(keep, change) {
var $unset = {};
for (var key in change) {
if (keep[key] === undefined) {
$unset[key] = "";
}
}
var copy = _.clone(keep);
delete copy._id;
return {$set: copy, $unset: $unset};
}
JsonRoutes.add("post", "/cardsets/:id", function (req, res) {
var id = req.params.id;
var token = req.params.token;
var at = APIAccess.findOne({cardset_id: id, token: token});
if (!at) {
JsonRoutes.sendResult(res, {
data: "permission denied"
});
} else {
var cards = req.body;
cards.forEach(function (card) {
card.cardset_id = id;
if (card._id != null && card._id !== "") {
var oldCard = Cards.findOne({_id: card._id});
var modifier = mongoReplacementModifier(card, oldCard);
Cards.update(card._id, modifier);
} else {
Cards.insert(card);
}
});
JsonRoutes.sendResult(res, {
data: "success"
});
}
});
var APIAccessSchema = new SimpleSchema({
cardset_id: {
type: String
},
token: {
type: String
}
});
APIAccess.attachSchema(APIAccessSchema);
......@@ -12,8 +12,8 @@ function exportCards(cardset_id, isCardsExport = true) {
}, {
fields: {
'cardset_id': 0,
'cardGroup': 0,
'_id': 0,
'cardGroup': 0
'_id': 0
'cardType': 0,
'difficulty': 0
}, sort: {
......
......@@ -8,6 +8,7 @@ import "../imports/api/allusers.js";
import "../imports/api/billing.js";
import "../imports/api/cards.js";
import "../imports/api/cardsets.js";
import "../imports/api/cardsetAPI.js";
import "../imports/api/cardsetUserlist.js";
import "../imports/api/colleges_courses.js";
import "../imports/api/serverInventory.js";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment