def removeUserFromContacts(self, user_id, contact_id): dao = UsersDAO() row = dao.checkUserContacts(user_id, contact_id) if not row: return jsonify(Error="Request cannot be completed"), 404 else: dao.removeUserFromContacts(user_id, contact_id) return jsonify(DeleteStatus="OK"), 200
def getUserByUsername(self, username): dao = UsersDAO() row = dao.getUserByUsername(username) if not row: return jsonify(Error="User Not Found"), 404 else: user = self.build_full_user_dict(row) return jsonify(User=user)
def getDetailedUsers(self): dao = UsersDAO() user_list = dao.getDetailedUsers() result_list = [] for row in user_list: result = self.build_user_dict(row) result_list.append(result) return jsonify(User=result_list)
def insertPhone(self, json): user_id = json['user_id'] phone = json['phone'] dao = UsersDAO() if user_id and phone: phone_id = dao.insertPhone(user_id, phone) result = self.build_phone_attributes(user_id, phone) return jsonify(Phone=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400
def getActiveUsers(self): dao = UsersDAO() user = dao.getActiveUsers() if not user: return jsonify(Error="No active users.") else: result_list = [] for row in user: result = self.build_active_user_dict(row) result_list.append(result) return jsonify(User=result_list)
def getUsersThatReact(self, post_id, react_type): dao = UsersDAO() user_list = dao.getUsersThatReact(post_id, react_type) if not user_list: return jsonify(Error="User not found"), 404 else: result_list = [] for row in user_list: result = self.build_user_react_dict(row) result_list.append(result) return jsonify(User=result_list)
def getUserContactList(self, user_id): dao = UsersDAO() row = dao.getUserById(user_id) if not row: return jsonify(Error="User Not Found"), 404 else: contact_list = dao.getUserContactList(user_id) result_list = [] for row in contact_list: result = self.build_user_dict(row) result_list.append(result) return jsonify(Contact=result_list)
def login(self, json): username = json['username'] password = json['password'] user = UsersDAO().getUserByUsername(username) if not user: return jsonify(Error="User not found"), 404 elif username and password: result = UsersDAO().login(username, password) if not result: return jsonify( Error="Bad combination of username and password."), 404 else: return jsonify(Login=result)
def getUserChatList(self, user_id): dao = UsersDAO() user = dao.getUserById(user_id) if not user: print("UNFH") return jsonify(Error="User not found."), 404 else: chat_list = dao.getUserChats(user_id) result_list = [] for row in chat_list: result = self.build_user_chat_dict(row) result_list.append(result) return jsonify(Chat=result_list)
def insertReact(self, json): react_type = json['react_type'] user_that_react = json['user_that_react'] p_reacted = -1 reply_reacted = -1 user = UsersDAO().getUserById(user_that_react) if json.get('p_reacted'): p_reacted = json['p_reacted'] post = PostsDAO().getPostById(p_reacted) if not post: return jsonify(Error="Post not found."), 404 elif json.get('reply_reacted'): reply_reacted = json['reply_reacted'] reply = ReplyDAO().getReplyById(reply_reacted) if not reply: return jsonify(Error="Reply not found."), 404 if not user: return jsonify(Error="User not found."), 404 elif react_type and user_that_react and json.get('p_reacted'): ReactsDAO().insertReactP(react_type, user_that_react, p_reacted) result = self.build_react_attributes_P(react_type, user_that_react, p_reacted) return jsonify(React=result), 201 elif react_type and user_that_react and json.get('reply_reacted'): ReactsDAO().insertReactR(react_type, user_that_react, reply_reacted) result = self.build_react_attributes_R(react_type, user_that_react, reply_reacted) return jsonify(React=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400
def getPhoneById(self, user_id): dao = PhonesDAO() row = UsersDAO().getUserById(user_id) if not row: return jsonify(Error="User Not Found"), 404 else: result = dao.getPhoneById(user_id) phone = self.build_phone_attributes(result) return jsonify(Phone=phone)
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 createPhone(self, json): u_phone = json['u_phone'] phone = json['phone'] user = UsersDAO().getUserById(u_phone) if not user: return jsonify(Error="User not found."), 404 elif u_phone and phone: dao = PhonesDAO() phone_id = dao.createPhone(u_phone, phone) result = self.build_phone_attributes(u_phone, phone) return jsonify(Phone=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400
def createUser(self, json): first_name = json['first_name'] last_name = json['last_name'] u_email_address = json['u_email_address'] u_password = json['u_password'] username = json['username'] if first_name and last_name and u_email_address and u_password and username: UsersDAO().createUser(first_name, last_name, u_email_address, u_password, username) result = self.build_user_attributes(first_name, last_name, u_email_address, u_password, username) return jsonify(User=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400
def insertUserToContacts(self, user_id, contact_id): dao = UsersDAO() user = UsersDAO().getUserById(user_id) contact = UsersDAO().getUserById(contact_id) if not user: return jsonify(Error="User not found."), 404 elif not contact: return jsonify(Error="User not found."), 404 else: dao.insertUserToContacts(user_id, contact_id) return jsonify(InsertStatus="OK"), 200
def insertReply(self, json): # reply_date = json['reply_date'] reply_text = json['reply_text'] p_replied = json['p_replied'] user_that_replied = json['user_that_replied'] post = PostsDAO().getPostById(p_replied) user = UsersDAO().getUserById(user_that_replied) if not post: return jsonify(Error="Post not found."), 404 elif not user: return jsonify(Error="User not found."), 404 elif reply_text and p_replied and user_that_replied: ReplyDAO().insertReply(reply_text, p_replied, user_that_replied) result = self.build_reply_attributes(reply_text, p_replied, user_that_replied) return jsonify(Reply=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400
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