def getMessageById(self, mID): dao = MessageDAO() result = dao.getMessageById(int(mID)) if not result: dao.closeDB() return jsonify(Error="Message NOT FOUND"), 404 else: mapped = mapMessageToDict(result) dao.closeDB() return jsonify(Messages=mapped)
def getAllMessages(self): dao = MessageDAO() result = dao.getAllMessages() if not result: dao.closeDB() return jsonify(Error="Message NOT FOUND"), 404 mapped_result = [] for r in result: mapped_result.append(mapMessageToDict(r)) dao.closeDB() return jsonify(Messages=mapped_result)
def getMessageByHashtag(self, args): dao = HashTagDAO() text = args.get('hText') if not text: dao.closeDB() return jsonify(Error="Bad Request Arguments"), 400 else: results = dao.getMessageByHashTag(text) mapped_results = [] for result in results: mapped_results.append(mapMessageToDict(result)) dao.closeDB() return jsonify(Messages=mapped_results)
def getOriginalMessageByReplyID(self, rID): dao = MessageDAO() if not dao.getMessageById(rID): dao.closeDB() return jsonify(Error="Reply NOT FOUND"), 404 result = dao.getOriginalMessageByReplyID(rID) if not result: dao.closeDB() return jsonify(Error="Original Message NOT FOUND"), 404 else: result = mapMessageToDict(result) dao.closeDB() return jsonify(Messages=result)
def getRepliesByMessageID(self, mID): dao = MessageDAO() if not dao.getMessageById(mID): dao.closeDB() return jsonify(Error="Message NOT FOUND"), 404 reply_list = dao.getRepliesByMessageID(mID) if not reply_list: dao.closeDB() return jsonify(Error="Reply NOT FOUND"), 404 results = [] for row in reply_list: result = mapMessageToDict(row) results.append(result) dao.closeDB() return jsonify(Messages=results)
def getMessagesByPersonID(self, pID): dao = PersonDAO() if not dao.getPersonById(pID): dao.closeDB() return jsonify(Error="Person NOT FOUND"), 404 message_list = dao.getMessagesByPersonID(pID) if not message_list: dao.closeDB() return jsonify(Error="Message NOT FOUND"), 404 results = [] for row in message_list: result = mapMessageToDict(row) results.append(result) dao.closeDB() return jsonify(Message=results)
def getMessagesPostedByPersoninGroupID(self, mID, gID): dao = MessageDAO() dao1 = GroupDAO() results = [] if not dao.getMessageById(mID): dao.closeDB() dao1.closeDB() return jsonify(Error="Message NOT FOUND"), 404 if not dao1.getGroupById(gID): dao.closeDB() dao1.closeDB() return jsonify(Error="Group NOT FOUND"), 404 message_list = dao.getMessagesPostedByPersoninGroupID(mID, gID) for row in message_list: result = mapMessageToDict(row) results.append(result) dao.closeDB() dao1.closeDB() return jsonify(Messages=results)
def addMessage(self, json): dao = MessageDAO() if len(json) != 3: dao.closeDB() return jsonify(Error="Malformed post request"), 400 else: mtext = json['mtext'] pid = json['pid'] gid = json['gid'] if mtext and pid and gid: (mid, timedate) = dao.addMessage(mtext, pid, gid) self.parseHashTag(mid, mtext) result = mapMessageToDict([mid, mtext, timedate, pid, gid]) dao.closeDB() return jsonify(Message=result), 201 else: dao.closeDB() return jsonify(Error="Unexpected attributes in post request"), 400
def addMessageAsReply(self, originalMessage, json): dao = MessageDAO() if len(json) != 4: dao.closeDB() return jsonify(Error="Malformed post request"), 400 else: mtext = json['mtext'] rtext = json['rtext'] pid = json['pid'] gid = json['gid'] if mtext and pid and gid and rtext: (mid, timedate) = dao.addMessage(mtext, pid, gid) self.parseHashTag(mid, rtext) oid, rid = dao.addMessageAsReply(originalMessage, mid) result = mapMessageToDict([mid, mtext, timedate, pid, gid]) result['originalMessageID'] = oid dao.closeDB() return jsonify(Message=result), 201 else: dao.closeDB() return jsonify(Error="Unexpected attributes in post request"), 400