def updateUser(self, uid, form): dao = UserDao() if not dao.getUserById(uid): # hardcoded, delete later return jsonify(Error="User not found."), 404 else: if len(form) != 7: return jsonify(Error="Malformed update request."), 400 else: try: aid = form['address_id'] rid = form['role_id'] fname = form['first_name'] lname = form['last_name'] email = form['email'] password = form['password'] phone_number = form['phone_number'] if fname and lname and email and password and phone_number and aid and rid: dao.update(fname, lname, email, password, rid, aid, phone_number, uid) result = self.__build_user_attributes( uid, aid, rid, fname, lname, email, password, phone_number) return jsonify(User=result), 200 else: return jsonify( Error="Attributes must not be null"), 400 except Exception as e: return jsonify( Error=f'Unexpected attributes in put request; {e}' ), 400
def insertUser(self, form): if not form: return jsonify(Error='Empty Form'), 400 print("form: ", form) if len(form) != 7: return jsonify(Error="Malformed post request"), 400 else: try: rid = form['role_id'] fname = form['first_name'] lname = form['last_name'] email = form['email'] password = form['password'] phone_number = form['phone_number'] address_id = form['address_id'] if fname and lname and email and password and phone_number and rid and address_id: dao = UserDao() uid = dao.insert(fname, lname, email, password, rid, address_id, phone_number) result = self.__build_user_attributes( uid, address_id, rid, fname, lname, email, password, phone_number) return jsonify(User=result), 201 else: return jsonify(Error="Attributes must not be null"), 400 except Exception as e: return jsonify( Error=f'Unexpected attributes in post request; {e}'), 400
def getUserByID(self, uid): dao = UserDao() row = dao.getUserById(uid) if not row: return jsonify(Error="User Not Found"), 404 else: user = self.__build_user_dict(row) return jsonify(User=user)
def getAllUsers(self, args): limit = args.get('limit', 25) offset = args.get('offset', 0) orderBy = args.get('orderBy', 'uid') limit = int(limit) offset = int(offset) dao = UserDao() users_list = dao.getAllUsers(limit, offset, orderBy) result_list = [] for row in users_list: result = self.__build_user_dict(row) result_list.append(result) return jsonify(Users=result_list)
def loginUser(self, form): if len(form) != 2: return jsonify(Error="Malformed login request."), 400 else: try: email = form['email'] password = form['password'] if email and password: dao = UserDao() row = dao.loginUser(email, password) if not row: return jsonify( Error="Incorrect email or password"), 400 else: user = self.__build_user_dict(row) return jsonify(User=user) else: return jsonify(Error="Attributes must not be null"), 400 except: return jsonify( Error='Unexpected attributes in get request'), 400
class UserService: __user_dao = UserDao() # 验证用户登录 def login(self, username, password): result = self.__user_dao.login(username, password) return result # 查询用户角色 def search_user_role(self, username): role = self.__user_dao.search_user_role(username) return role # 添加用户 def insert(self, username, password, email, role_id): self.__user_dao.insert(username, password, email, role_id) # 查询用户名 def research_username(self, username): result = self.__user_dao.research_username(username) return result # 查询用户分页记录 def search_user_list(self, page): result = self.__user_dao.search_user_list(page) return result # 查询用户总页数 def search_count_page(self): count_page = self.__user_dao.search_count_page() return count_page # 修改用户信息 def update(self, id, username, password, email, role_id): self.__user_dao.update(id, username, password, email, role_id) # 删除用户信息 def delete_by_id(self, id): self.__user_dao.delete_by_id(id) # 查询用户密码 def search_uesr_passwd(self, username): old_password = self.__user_dao.search_uesr_passwd(username) return old_password # 查询用户id def search_userid(self, username): userid = self.__user_dao.search_userid(username) return userid
def searchUsers( self, args): # can be redesigned based on implementation decisions fname = args.get("first_name") lname = args.get("last_name") email = args.get("email") phone_number = args.get("phone_number") role_name = args.get("role_name") rid = args.get("role_id") extra = 0 if args.get('limit'): extra = extra + 1 if args.get('offset'): extra = extra + 1 if args.get('orderBy'): extra = extra + 1 limit = args.get('limit', 25) offset = args.get('offset', 0) orderBy = args.get('orderBy', 'uid') limit = int(limit) offset = int(offset) dao = UserDao() users_list = [] if (len(args) == 2 + extra) and fname and lname: users_list = dao.getUsersByFullName(fname=fname, lname=lname, limit=limit, offset=offset, orderBy=orderBy) elif (len(args) == 1 + extra) and email: users_list = dao.getUsersByEmail(email, limit, offset, orderBy) elif (len(args) == 1 + extra) and phone_number: users_list = dao.getUsersByPhoneNumber(phone_number, limit, offset, orderBy) elif (len(args) == 1 + extra) and (role_name or rid): if role_name: users_list = dao.getUsersByRoleName(roleName=role_name, limit=limit, offset=offset, orderBy=orderBy) else: users_list = dao.getUsersByRoleID(roleID=rid, limit=limit, offset=offset, orderBy=orderBy) else: return jsonify(Error="Malformed query string"), 400 result_list = [] for row in users_list: result = self.__build_user_dict(row) result_list.append(result) return jsonify(Users=result_list)
def signUpUser(self, address, user, payment): if not address and not user: return jsonify(Error='Empty Address and User'), 400 elif len(user) != 6: return jsonify(Error="Malformed user post request"), 400 elif len(address) != 7: return jsonify(Error="Malformed address post request"), 400 elif payment and len(payment) != 2: return jsonify(Error="Malformed payment_method post request"), 400 else: try: rid = user['role_id'] fname = user['first_name'] lname = user['last_name'] email = user['email'] password = user['password'] phone_number = user['phone_number'] street_address = address['street_address'] city = address['city'] country = address['country'] zip_code = address['zip_code'] senate_region = address['senate_region'] latitud = address['latitud'] longitud = address['longitud'] if fname and lname and email and password and phone_number and rid and street_address and city and country and zip_code and senate_region and latitud and longitud: if payment: type = payment['type'] wallet = payment['wallet'] if type and wallet: dao = UserDao() aid, uid, pid = dao.signupWiPayment( fname, lname, email, password, rid, phone_number, street_address, city, country, zip_code, senate_region, latitud, longitud, type, wallet) userres = self.__build_user_attributes( uid, aid, rid, fname, lname, email, password, phone_number) addressres = AddressHandler( ).build_address_attributes(aid, street_address, city, country, zip_code, senate_region, latitud, longitud) paymentres = PaymentsMethodHandler( ).build_payment_attributes(pid, uid, type, wallet) return jsonify(User=userres, Address=addressres, Payment_Method=paymentres), 201 else: return jsonify( Error="Attributes must not be null"), 400 else: dao = UserDao() aid, uid = dao.signupNoPayment( fname, lname, email, password, rid, phone_number, street_address, city, country, zip_code, senate_region, latitud, longitud) userres = self.__build_user_attributes( uid, aid, rid, fname, lname, email, password, phone_number) addressres = AddressHandler().build_address_attributes( aid, street_address, city, country, zip_code, senate_region, latitud, longitud) return jsonify(User=userres, Address=addressres), 201 else: return jsonify(Error="Attributes must not be null"), 400 except Exception as e: return jsonify( Error=f'Unexpected attributes in post request; {e}'), 400