def delete_user(user_id): user_validator.delete_user_fields_validator(user_id) user = db.session.query(User).filter(User.id == user_id).first() if not user: return error_manager.return_error_code( ValueError("User does not exists", errors_code.USER_DOES_NOT_EXISTS)), 404 customers_created_by = db.session.query(Customer).filter( Customer.created_by == user.id).all() customers_updated_by = db.session.query(Customer).filter( Customer.updated_by == user.id).all() for customer in customers_created_by: customer.created_by = 0 for customer in customers_updated_by: customer.updated_by = 0 db.session.delete(user) db.session.commit() return jsonify({"status": "ok", "message": "User deleted"}), 200
def get_user_by_id(user_id): user_validator.get_user_by_id_fields_validator(user_id) user = db.session.query(User).filter(User.id == user_id).first() if not user: return error_manager.return_error_code( ValueError("User does not exists", errors_code.USER_DOES_NOT_EXISTS)), 404 return jsonify(user.serialize()), 200
def login_user(user_json): user_validator.login_user_fields_validator(user_json) user = db.session.query(User).filter( User.email == user_json.get("email")).first() if not user or not bcrypt.checkpw( user_json.get('password').encode('utf-8'), user.password): return error_manager.return_error_code( ValueError("User or password are invalids", errors_code.VALIDATION_INVALID_PASSWORD_MATCH)), 409 return jsonify( {"token": auth_utils.encode_auth_token(user.id).decode("utf-8")}), 200
def update_user(user_json): user_validator.update_user_fields_validator(user_json) user = db.session.query(User).filter( User.id == user_json.get('id')).first() if not user: return error_manager.return_error_code( ValueError("User does not exists", errors_code.USER_DOES_NOT_EXISTS)), 404 user.update_user_values(user_json) db.session.commit() return jsonify({"status": "ok", "message": "User updated"}), 200
def register_new_user(user_json): user_validator.new_user_fields_validator(user_json) user = db.session.query(User).filter( User.email == user_json.get("email")).first() if user: return error_manager.return_error_code( ValueError("User already exists", errors_code.USER_ALREADY_EXISTS)), 409 new_user = User.from_json_to_model(user_json) db.session.add(new_user) db.session.commit() return jsonify({"status": "ok", "message": "User created"}), 200
def delete_customer(customer_id): customer_validator.delete_customer_fields_validator(customer_id) customer = db.session.query(Customer).filter( Customer.id == customer_id).first() if not customer: return return_error_code( ValueError("Customer does not exists", errors_code.CUSTOMER_DOES_NOT_EXISTS)), 404 remove_customer_photo(customer) db.session.delete(customer) db.session.commit() return jsonify({"status": "ok", "message": "Customer deleted"}), 200
def update_customer(customer_json): customer_validator.update_customer_fields_validator(customer_json) customer = db.session.query(Customer).filter( Customer.id == customer_json.get('id')).first() if not customer: return return_error_code( ValueError("Customer does not exists", errors_code.CUSTOMER_DOES_NOT_EXISTS)), 404 photo = customer_json.get('photo') if photo: customer.photo = upload_customer_photo(customer, photo) customer.update_customer_values(customer_json) db.session.commit() return jsonify({"status": "ok", "message": "Customer updated"}), 200
def get_customer_by_id(id): customer_validator.get_customer_by_id_fields_validator(id) customer = db.session.query(Customer).filter(Customer.id == id).first() if not customer: return return_error_code( ValueError("Customer does not exists", errors_code.CUSTOMER_DOES_NOT_EXISTS)), 404 created_by = db.session.query(User).filter( User.id == customer.created_by).first() updated_by = db.session.query(User).filter( User.id == customer.updated_by).first() if created_by: customer.created_by = created_by if updated_by: customer.updated_by = updated_by return jsonify(customer.serialize()), 200
def update_user(): try: response = user_controller.update_user(request.json) return response except Exception as exception: return jsonify(return_error_code(exception)), 400
def get_user_by_id(id): try: user = user_controller.get_user_by_id(id) return user except Exception as exception: return jsonify(return_error_code(exception)), 400
def get_users(): try: users = user_controller.get_all_users() return users except Exception as exception: return jsonify(return_error_code(exception)), 400
def create_new_customer(): try: response = customer_controller.create_new_customer(request.json) return response except Exception as exception: return jsonify(return_error_code(exception)), 400
def get_customer_by_id(id): try: customer = customer_controller.get_customer_by_id(id) return customer except Exception as exception: return jsonify(return_error_code(exception)), 400
def get_customers(): try: customers = customer_controller.get_all_customer() return customers except Exception as exception: return jsonify(return_error_code(exception)), 400
def register_user(): try: response = user_controller.register_new_user(request.json) return response except Exception as exception: return jsonify(return_error_code(exception)), 400
def delete_user(user_id): try: response = user_controller.delete_user(user_id) return response except Exception as exception: return jsonify(return_error_code(exception)), 400
def change_user_status(): try: response = user_controller.change_user_status(request.json) return response except Exception as exception: return jsonify(return_error_code(exception)), 400