def getPersonById(self, pID): dao = PersonDAO() result = dao.getPersonById(pID) if result is None: dao.closeDB() return jsonify(Error="Person NOT FOUND"), 404 else: mapped = mapPersonToDict(result) dao.closeDB() return jsonify(Person=mapped)
def getAllPersons(self): dao = PersonDAO() result = dao.getAllPersons() if not result: dao.closeDB() return jsonify(Error="Person NOT FOUND"), 404 mapped_result = [] for r in result: mapped_result.append(mapPersonToDict(r)) dao.closeDB() return jsonify(Person=mapped_result)
def getPeopleByGroupID(self, gID): dao = GroupDAO() if not dao.getGroupById(gID): dao.closeDB() return jsonify(Error="Group NOT FOUND"), 404 person_list = dao.getPeopleByGroupID(gID) results = [] for row in person_list: result = mapPersonToDict(row) results.append(result) dao.closeDB() return jsonify(Person=results)
def getPersonByUsername(self, args): dao = PersonDAO() username = args.get('username') if not username or len(args) != 1: dao.closeDB() return jsonify(Error="Bad Request Arguments"), 400 result = dao.getPersonByUsername(username) if result is None: dao.closeDB() return jsonify(Error="Person NOT FOUND"), 404 else: mapped = mapPersonToDict(result) dao.closeDB() return jsonify(Person=mapped)
def addPerson(self, json): dao = PersonDAO() if len(json) != 6: dao.closeDB() return jsonify(Error="Malformed post request"), 400 else: username = json['username'] password = json['password'] firstname = json['pfirstname'] lastname = json['plastname'] phone = json['pphone'] email = json['pemail'] if username and password and firstname and lastname and phone and email: pid = dao.addPerson(username, password, firstname, lastname, phone, email) result = mapPersonToDict([ pid, username, password, firstname, lastname, phone, email ]) dao.closeDB() return jsonify(Person=result), 201 else: dao.closeDB() return jsonify( Error="Unexpected attributes in post request"), 400