def delete(self, uuid: str) -> tuple: """Delete a category from the database. Delete a category with provided UUID and returns category info. Args: uuid (str): UUID of category to delete. Returns: tuple: (Category Info | Error Message, HTTP Return Code) """ # Confirm UUID is valid if not uuid_is_valid(uuid): return {"message": "{uuid} is not a valid UUID".format(uuid=uuid)}, 400 # Confirm category exists existing_category = db.session.query(CategoryModel).filter_by(uuid=uuid).first() if existing_category is None: return {"message": "No category with uuid {uuid} found.".format(uuid=uuid)}, 404 # Confirm category exists existing_category = db.session.query(CategoryModel).filter_by(uuid=uuid).first() if existing_category is None: return {"message": "Category with uuid {uuid} not found.".format(uuid=uuid)}, 404 # Delete category db.session.delete(existing_category) db.session.commit() # Return Deleted Category w/o ID or Password info category_schema = CategorySchema() return category_schema.dump(existing_category), 200
def delete(self, uuid: str) -> tuple: """Delete a user from the database. Delete a user with provided UUID and returns user info. Args: uuid (str): UUID of user to delete. Returns: tuple: (User Info | Error Message, HTTP Return Code) """ # Confirm UUID is valid if not uuid_is_valid(uuid): return {"message": "{uuid} is not a valid UUID".format(uuid=uuid)}, 400 # Confirm user exists existing_user = db.session.query(UserModel).filter_by(uuid=uuid).first() if existing_user is None: return {"message": "No user with uuid {uuid} found.".format(uuid=uuid)}, 404 # Confirm user exists existing_user = db.session.query(UserModel).filter_by(uuid=uuid).first() if existing_user is None: return {"message": "User with uuid {uuid} not found.".format(uuid=uuid)}, 404 # Delete user db.session.delete(existing_user) db.session.commit() # Return Deleted User w/o ID or Password info user_schema = UserSchema() return user_schema.dump(existing_user), 200
def put(self, uuid: str) -> tuple: """Updates user information. Args: uuid (str): UUID of user to update. Returns: tuple: (User Info | Error Message, HTTP Return Code) """ # Confirm UUID is valid if not uuid_is_valid(uuid): return {"message": "{uuid} is not a valid UUID".format(uuid=uuid)}, 400 # Check for JSON content update_json = request.get_json(silent=True) if update_json is None: return {"message": "Could not parse JSON"}, 500 # Confirm user exists existing_user = db.session.query(UserModel).filter_by(uuid=uuid).first() if existing_user is None: return {"message": "No user with uuid {uuid} found.".format(uuid=uuid)}, 404 # Update first name if it exists if "first_name" in update_json: existing_user.first_name = update_json["first_name"] # Update last name if it exists if "last_name" in update_json: existing_user.last_name = update_json["last_name"] # Update email if it exists if "email" in update_json: email_to_check = update_json["email"] email_exists = db.session.query(UserModel).filter_by(email=email_to_check).first() if email_exists: return {"message": "A user with email address {email} already exists.".format( email=email_to_check)}, 409 else: existing_user.email = email_to_check # Update password if it exists if "password" in update_json: password_hash = ph.hash(update_json["password"]) existing_user.password = password_hash # Commit changes to database db.session.commit() return {"message": "Update successful."}, 200
def get(self, uuid: str) -> tuple: """Returns category info for given UUID. Args: uuid (str): UUID of category to lookup. Returns: tuple: (Category Info | Error Message, HTTP Return Code) """ # Confirm UUID is valid if not uuid_is_valid(uuid): return {"message": "{uuid} is not a valid UUID".format(uuid=uuid)}, 400 # Confirm Category exists existing_cat = db.session.query(CategoryModel).filter_by(uuid=uuid).first() if existing_cat is None: return {"message": "No category with uuid {uuid} found.".format(uuid=uuid)}, 404 # Return Category category = CategorySchema() return category.dump(existing_cat), 200
def get(self, uuid: str) -> tuple: """Returns user info for given UUID. Args: uuid (str): UUID of user to lookup. Returns: tuple: (User Info | Error Message, HTTP Return Code) """ # Confirm UUID is valid if not uuid_is_valid(uuid): return {"message": "{uuid} is not a valid UUID".format(uuid=uuid)}, 400 # Confirm user exists existing_user = db.session.query(UserModel).filter_by(uuid=uuid).first() if existing_user is None: return {"message": "No user with uuid {uuid} found.".format(uuid=uuid)}, 404 # Return User w/o ID or Password info user = UserSchema() return user.dump(existing_user), 200
def put(self, uuid: str) -> tuple: """Updates category name. Args: uuid (str): UUID of category to update. Returns: tuple: (Category Info | Error Message, HTTP Return Code) """ # Confirm UUID is valid if not uuid_is_valid(uuid): return {"message": "{uuid} is not a valid UUID".format(uuid=uuid)}, 400 # Check for JSON content update_json = request.get_json(silent=True) if update_json is None: return {"message": "Could not parse JSON"}, 500 # Confirm category exists existing_cat = db.session.query(CategoryModel).filter_by(uuid=uuid).first() if existing_cat is None: return {"message": "No category with uuid {uuid} found.".format(uuid=uuid)}, 404 # Update name if it exists if "name" in update_json: name_to_check = update_json["name"] name_exists = db.session.query(CategoryModel).filter_by(name=name_to_check).first() if name_exists: return {"message": "A category with name address {name} already exists.".format( name=name_to_check)}, 409 else: existing_cat.name = name_to_check # Commit changes to database db.session.commit() return {"message": "Update successful."}, 200