From 010995b38bf27308525b45a4738c48dfc5dd3235 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Sun, 1 Sep 2013 20:50:36 -0400 Subject: [PATCH] api: createGroup --- assets/js/controllers/groupsCtrl.js | 36 ++++++++++++++++++++++++-- src/controllers/groups.js | 8 ++++++ src/routes/api.js | 2 +- views/options/groups/create-group.jade | 13 +++++----- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/assets/js/controllers/groupsCtrl.js b/assets/js/controllers/groupsCtrl.js index 77e1b340c7..043a070676 100644 --- a/assets/js/controllers/groupsCtrl.js +++ b/assets/js/controllers/groupsCtrl.js @@ -1,7 +1,7 @@ "use strict"; -habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'API_URL', '$q', - function($scope, $rootScope, Groups, $http, API_URL, $q) { +habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'API_URL', '$q', 'User', + function($scope, $rootScope, Groups, $http, API_URL, $q, User) { $scope.isMember = function(user, group){ return ~(group.members.indexOf(user._id)); @@ -53,6 +53,36 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A alert("User invited to group"); }); } + + $scope.create = function(group){ + group.leader = User.user._id; + group.members = [User.user._id]; + + if (group.type === 'party') { + group.$save(function(){ + location.reload(); + }); + } + + if (User.user.balance >= 1) { + $rootScope.modals.moreGems = true; +// $('#more-gems-modal').modal('show'); + } + + if (confirm("Create Guild for 4 Gems?")) { + if (group.type === 'guild') { + group.privacy = group.privacy || 'public'; + } + group.balance = 1; + group.$save(function(){ + User.user.balance--; + User.log({op:'set', data:{'balance':user.balance}}); + window.setTimeout(function(){window.location.href='/';}, 500) + }) + } + + + } } ]) @@ -74,6 +104,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A function($scope, Groups) { $scope.type = 'guild'; $scope.text = 'Guild'; + $scope.newGroup = new Groups({type:'guild', privacy:'private'}); $scope.join = function(group){ group.$join(function(saved){ @@ -95,6 +126,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A function($scope, Groups, User) { $scope.type = 'party'; $scope.text = 'Party'; + $scope.newGroup = new Groups({type:'party'}); $scope.group = $scope.groups.party; $scope.join = function(party){ // workaround since group isn't currently a resource, this won't get saved to the server diff --git a/src/controllers/groups.js b/src/controllers/groups.js index a7a07307d3..0beb412258 100644 --- a/src/controllers/groups.js +++ b/src/controllers/groups.js @@ -83,6 +83,14 @@ api.getGroups = function(req, res, next) { }) }; +api.createGroup = function(req, res, next) { + var group = new Group(req.body); + group.save(function(err, saved){ + if (err) return res.json(500,{err:err}); + res.json(saved); + }) +} + api.attachGroup = function(req, res, next) { Group.findById(req.params.gid, function(err, group){ if(err) return res.json(500, {err:err}); diff --git a/src/routes/api.js b/src/routes/api.js index ada2abea63..673ac9846b 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -55,9 +55,9 @@ router.post('/user/buy-gems', auth, user.buyGems); /* Groups*/ router.get('/groups', auth, groups.getGroups); +router.post('/groups', auth, groups.createGroup); //TODO: //GET /groups/:gid (get group) -//POST /groups/:gid (create group) //PUT /groups/:gid (edit group) //DELETE /groups/:gid diff --git a/views/options/groups/create-group.jade b/views/options/groups/create-group.jade index e1f08e0b39..9d03f4e92d 100644 --- a/views/options/groups/create-group.jade +++ b/views/options/groups/create-group.jade @@ -1,23 +1,22 @@ -form.form-horizontal(x-bind='submit:groupCreate', data-type='{{type}}') - .alert.alert-danger(ng-show='_groupError') {{_groupError}} +form.form-horizontal(ng-submit='create(newGroup)') .control-group.whatever-options .control-group label.control-label(for='new-group-name') {{text}} Name .controls - input#new-group-name.input-medium.option-content(required, type='text', placeholder='{{text}} Name', value='{{_new.group.name}}') + input#new-group-name.input-medium.option-content(required, type='text', placeholder='{{text}} Name', ng-model='newGroup.name') .control-group label.control-label(for='new-group-description') Description .controls - textarea#new-group-description.option-content(cols='3', placeholder='Description') {{_new.group.description}} + textarea#new-group-description.option-content(cols='3', placeholder='Description', ng-model='newGroup.description') .control-group(ng-show='type=="guild"') .controls label.radio - input(type='radio', name='new-group-privacy', checked='{{"public"==_new.group.privacy}}') + input(type='radio', name='new-group-privacy', value='public', ng-model='newGroup.privacy') | Public label.radio - input(type='radio', name='new-group-privacy', checked='{{"private"==_new.group.privacy}}') + input(type='radio', name='new-group-privacy', value='private', ng-model='newGroup.privacy') | Invite Only - input.btn(type='submit', ng-disabled='!_new.group.privacy && !_new.group.name', value='Create') + input.btn(type='submit', ng-disabled='!newGroup.privacy && !newGroup.name', value='Create') span.gem-cost 4 Gems p small