def getChatById(self, chat_id): dao = ChatsDAO() row = dao.getChatById(chat_id) if not row: return jsonify(Error="Chat Not Found"), 404 else: chat = self.build_chat_dict(row) return jsonify(Chat=chat)
def getAllChats(self): dao = ChatsDAO() chat_list = dao.getAllChats() result_list = [] for row in chat_list: result = self.build_chat_dict(row) result_list.append(result) return jsonify(Chat=result_list)
def createChat(self, json): chat_name = json['chat_name'] owner_id = json['owner_id'] if chat_name and owner_id: dao = ChatsDAO() chat_id = dao.createChat(chat_name, owner_id) result = self.build_chat_attributes(chat_id, chat_name, owner_id) return jsonify(Chat=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400
def getChatOwner(self, chat_id): chat = ChatsDAO().getChatById(chat_id) if not chat: return jsonify(Error="Chat Not Found"), 404 else: result = ChatsDAO().getChatOwner(chat_id) result = self.build_chat_owner_attributes(result[0][0], result[0][1], result[0][2]) return jsonify(Chat=result)
def insertUserToChat(self, chat_id, user_id): dao = ChatsDAO() chat = dao.getChatById(chat_id) user = UsersDAO().getUserById(user_id) if not chat: return jsonify(Error="Chat not found."), 404 elif not user: return jsonify(Error="User not found."), 404 else: dao.insertUserToChat(chat_id, user_id) return jsonify(InsertStatus="OK"), 200
def getChatUsers(self, chat_id): dao = ChatsDAO() chat = dao.getChatById(chat_id) user_list = dao.getChatUsers(chat_id) if not user_list: return jsonify(Chat=user_list), 404 elif not chat: return jsonify(Error="Chat not found"), 404 else: result_list = [] for row in user_list: user = UserHandler.build_user_dict(UserHandler, row) result_list.append(user) return jsonify(Chat=result_list)
def removeChat(self, chat_id, owner_id): dao = ChatsDAO() o_id = dao.getChatOwner(chat_id) chat = dao.getChatById(chat_id) if owner_id != o_id[0][0]: return jsonify(Error="Operation invalid."), 404 elif not chat: return jsonify(Error="Chat not found."), 404 else: dao.removeChat(chat_id) return jsonify(DeleteStatus="OK"), 200
def insertPost(self, json): post_caption = json['post_caption'] #post_date = json['post_date'] p_created_by = json['p_created_by'] c_post_belongs = json['c_post_belongs'] chat = ChatsDAO().getChatById(c_post_belongs) user = UsersDAO().getUserById(p_created_by) if not chat: return jsonify(Error="Chat not found."), 404 elif not user: return jsonify(Error="User not found."), 404 elif post_caption and p_created_by and c_post_belongs: post_id = PostsDAO().insertPost(post_caption, p_created_by, c_post_belongs) result = self.build_post_attributes(post_caption, p_created_by, c_post_belongs, post_id) return jsonify(Post=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400