add creator to school schema, make server functions more efficient

This commit is contained in:
yamanq 2016-08-10 08:00:09 -04:00
parent 9610867f1c
commit 17581e3f31
2 changed files with 10 additions and 7 deletions

View File

@ -5,6 +5,7 @@ work = new Mongo.Collection("Work");
schools.schema = new SimpleSchema({ schools.schema = new SimpleSchema({
name: {type: String}, name: {type: String},
status: {type: Boolean}, status: {type: Boolean},
creator: {type: String},
aliases: {type: [String]} aliases: {type: [String]}
}); });

View File

@ -11,14 +11,16 @@ Meteor.methods({
return 'xxxxxx'.replace(/[x]/g, _uuid4); return 'xxxxxx'.replace(/[x]/g, _uuid4);
}, },
'createSchool': function(schoolname) { 'createSchool': function(schoolname) {
if (Meteor.user() != null && schools.find({name:input.school}).fetch().length === 0) { if (Meteor.user() != null && schools.findOne({name:input.school}) != null &&
schools.insert({name: schoolname, status: false}); schools.findOne({status: false, creator: Meteor.userId()}) != null) {
schools.insert({name: schoolname, status: false, creator: Meteor.userId()});
} }
}, },
'createClass': function(input) { 'createClass': function(input) {
classes.schema.validate(input); classes.schema.validate(input);
if(Meteor.user() != null && classes.find({status:false, admin:Meteor.userId()}).fetch().length < 5 && if(Meteor.user() != null && classes.find({status:false, admin:Meteor.userId()}).fetch().length < 5 &&
schools.find({name:input.school}).fetch().length > 0 && input.status === false) { schools.findOne({name:input.school}) != null && input.status === false) {
input.subscribers = 0; input.subscribers = 0;
input.admin = Meteor.userId() input.admin = Meteor.userId()
@ -36,7 +38,7 @@ Meteor.methods({
input.banned = [] input.banned = []
input.blockEdit = [] input.blockEdit = []
classes.insert(input); classes.insert(input);
Meteor.call('joinClass',classes.find({input}).fetch()[0]._id, input.code, function(error,result){}); Meteor.call('joinClass',classes.findOne({input})._id, input.code, function(error,result){});
return 1; return 1;
} else { } else {
return 0; return 0;
@ -49,7 +51,7 @@ Meteor.methods({
current.description = change[2]; current.description = change[2];
current.avatar = change[3]; current.avatar = change[3];
current.banner = change[4]; current.banner = change[4];
if (schools.find({name:current.school}).fetch().length > 0 && Number.isInteger(current.grade) && if (schools.findOne({name:current.school}) != null && Number.isInteger(current.grade) &&
current.grade >= 9 && current.grade <= 12 && current.description.length < 100) { current.grade >= 9 && current.grade <= 12 && current.description.length < 100) {
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}}); Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1; return 1;
@ -58,7 +60,7 @@ Meteor.methods({
} }
}, },
'joinClass': function(change, pass) { 'joinClass': function(change, pass) {
found = classes.find({_id: change, status: true}).fetch(); found = classes.findOne({_id: change, status: true});
if (Meteor.user() != null && found.length > 0 && pass === found[0].code && Meteor.user().profile.classes.indexOf(change) === -1) { if (Meteor.user() != null && found.length > 0 && pass === found[0].code && Meteor.user().profile.classes.indexOf(change) === -1) {
current = Meteor.user().profile; current = Meteor.user().profile;
current.classes.append(change); current.classes.append(change);
@ -73,7 +75,7 @@ Meteor.methods({
profile = Meteor.user().profile profile = Meteor.user().profile
index = profile.classes.indexOf(change) index = profile.classes.indexOf(change)
if (index >= 0) { if (index >= 0) {
if (classes.find({_id: change}).fetch()[0].admin != Meteor.userId()) { if (classes.findOne({_id: change}).admin != Meteor.userId()) {
profile.classes.splice(index, 1); profile.classes.splice(index, 1);
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}}); Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1 return 1