diff --git a/angular_app/js/controllers.js b/angular_app/js/controllers.js index e878cfc..26cf9e7 100644 --- a/angular_app/js/controllers.js +++ b/angular_app/js/controllers.js @@ -50,10 +50,10 @@ eventManControllers.controller('PersonDetailsCtrl', ['$scope', 'Person', '$route $scope.person = Person.get($routeParams); } $scope.save = function() { - if ($scope.event.id === undefined) { - Event.save($scope.event); + if ($scope.person.id === undefined) { + Person.save($scope.person); } else { - Event.update($scope.event); + Person.update($scope.person); } }; }] diff --git a/backend.py b/backend.py index 0fce04c..963e757 100644 --- a/backend.py +++ b/backend.py @@ -7,6 +7,7 @@ import pymongo class EventManDB(object): + db = None connection = None def __init__(self, url=None, dbName='eventman'): @@ -15,8 +16,8 @@ class EventManDB(object): self.connect(url) def connect(self, url=None, dbName=None): - if self.connection is not None: - return self.connection + if self.db is not None: + return self.db if url: self._url = url if dbName: @@ -25,6 +26,27 @@ class EventManDB(object): self.db = self.connection[self._dbName] return self.db + def get(self, collection, id_): + results = self.query(collection, {'id': id_}) + print results, id_, type(id_) + return results and results[0] or {} + + def query(self, collection, query=None): + db = self.connect() + results = list(db[collection].find(query or {})) + for result in results: + result['_id'] = str(result['_id']) + return results + + def add(self, collection, data): + db = self.connect() + _id = db[collection].insert(data) + newData = db[collection].find_one({'_id': _id}) + newData['_id'] = str(newData['_id']) + return newData + + #def update(self, collection) + def addUser(self, user): db = self.connect() db.users.insert(user) diff --git a/eventman_server.py b/eventman_server.py index 5999e2c..385a2c8 100755 --- a/eventman_server.py +++ b/eventman_server.py @@ -11,7 +11,7 @@ import tornado.ioloop import tornado.options from tornado.options import define, options import tornado.web -from tornado import gen +from tornado import gen, escape import backend @@ -63,12 +63,23 @@ json._default_encoder = ImprovedEncoder() class PersonsHandler(BaseHandler): + collection = 'persons' + @gen.coroutine def get(self, id_=None): if id_ is not None: - self.write(MOCKUP_PERSONS[int(id_)]) - return - self.write({'persons': MOCKUP_PERSONS.values()}) + self.write(self.db.get(self.collection, int(id_))) + else: + self.write({'persons': self.db.query(self.collection)}) + + @gen.coroutine + def post(self, id_=None, **kwargs): + data = escape.json_decode(self.request.body or {}) + if id_ is None: + newData = self.db.add(self.collection, data) + else: + newData = self.db.update(self.connection, data) + self.write(newData) class EventsHandler(BaseHandler): @@ -81,7 +92,13 @@ class EventsHandler(BaseHandler): @gen.coroutine def post(self, id_=None, **kwargs): - data = self.request.body + event = self.request.body + if id_ is None: + newEvent = self.db.addEvent(event) + print newEvent + else: + dbEvent = self.db.findEvent({'id': event['id']}) + print 'aaaaaa', id_, data @gen.coroutine