def edit(self, u): try: sql = "SELECT * FROM users WHERE id = %s" cur.execute(sql, u["id"]) dbUser = cur.fetchone() if dbUser == None: return "User ID not found" u["pw"] = Bcrypt.generate_password_hash(None, u["pw"], 12) sql = "UPDATE users SET isAdmin=%s, name=%s, pw=%s, username=%s, settings=%s where id=%s" cur.execute(sql, (u["isAdmin"], u["name"], u["pw"], u["username"], u["settings"], u["id"])) con.commit() out = {} out["id"] = u["id"] out["username"] = u["username"] out["isAdmin"] = u["isAdmin"] out["name"] = u["name"] return out except Exception as e: print(e) return False finally: print("\n")
def editPictures(): try: data = request.get_json() editedPic = {} editedPic["id"] = data["id"] editedPic["caption"] = data["caption"] editedPic["pet_id"] = data["pet_id"] with con.cursor() as cur: sql = "UPDATE pictures SET caption=%s, pet_id=%s WHERE id = %s" cur.execute(sql, (editedPic["caption"], editedPic["pet_id"], editedPic["pet_id"])) con.commit() return { "msg": f"Updated picture with ID: {editedPic['id']}", "pet_id": f"{editedPic['pet_id']}", "caption": f"{editedPic['caption']}" } except Exception as e: return { "msg": f"Unable to update picture with id of {editedPic['id']}" } finally: print("\n")
def addPictures(): now = datetime.now().strftime('%Y-%m-%d %H:%M:%S') try: data = request.get_json() newPic = {} newPic["comments"] = [] newPic["author_id"] = data["author_id"] newPic["caption"] = data["caption"] newPic["imgUrl"] = data["imgUrl"] newPic["pet_id"] = data["pet_id"] print(newPic) with con.cursor() as cur: sql = "INSERT INTO pictures (comments, author_id, dt, caption, imgUrl, likes, pet_id) VALUES (%s, %s, %s, %s, %s, 0, %s)" cur.execute( sql, (newPic["comments"], newPic["author_id"], now, newPic["caption"], newPic["imgUrl"], newPic["pet_id"])) con.commit() except Exception as e: return {"msg": "Unable to add picture: {}".format(e)}, 400 finally: print("\n") return {"msg": "New picture added!"}, 200
def delete(self, u): try: sql = "DELETE FROM users WHERE id=%s" cur.execute(sql, u["id"]) con.commit() return f"User with ID: {u['id']}, was deleted" except Exception as e: print(e) return False finally: print("\n")
def createPet(self, nP): try: sql = "INSERT INTO pets (owner_id, catOrDog, name, birthday, gender, pictureUrl) values (%s, %s, %s, %s, %s, %s)" cur.execute(sql, (nP["owner_id"], nP["catOrDog"], nP["name"], nP["birthday"], nP["gender"], nP["pictureUrl"])) con.commit() return {"msg": "Successfully Created new pet", "data": f"{nP}"}, 200 except Exception as e: print(e) return {"msg": "Unable to add {}: {}".format(newPet["name"], e)}, 400 finally: print("\n")
def register(self, nU): try: _ = {"test": "cookies"} sql = "INSERT INTO users (username, pw, isAdmin, name, settings) values (%s, %s, %s, %s, %s)" cur.execute(sql, (nU["username"], nU["password"], int( nU["isAdmin"]), nU["name"], json.dumps(_))) con.commit() return nU except Exception as e: print(e) return False finally: print("\n")
def deletePicture(): try: data = request.get_json() with con.cursor() as cur: sql = "DELETE FROM pictures WHERE id = %s" cur.execute(sql, (data["id"])) con.commit() return {"msg": f"Deleted picture with id {data['id']}"} except Exception as e: return {"msg": f"Unable to delete picture with id {data['id']}"} finally: print("\n")
def deletePet(self, nP): try: sql = "SELECT * FROM pets Where id = %s" cur.execute(sql, nP["id"]) dbPet = cur.fetchone() if dbPet == None: return {"msg": "{} was not found :(".format(nP["name"])} sql = "DELETE FROM pets WHERE id=%s" cur.execute(sql, nP["id"]) con.commit() return {"msg": "{} was deleted :(".format(nP["name"])} except Exception as e: print(e) return {"msg": "unable to delete pet {}".format(e)}, 400 finally: print("\n")
def editPet(self, nP): try: sql = "SELECT * FROM pets Where id = %s" cur.execute(sql, data["id"]) dbPet = cur.fetchone() if dbPet == None: return {"msg": "{} was not found :(".format(nP["name"])} sql = "UPDATE pets SET owner_id=%s, catOrDog=%s, name=%s, birthday=%s, gender=%s, pictureUrl=%s where id=%s" cur.execute(sql, (nP["owner_id"], nP["catOrDog"], nP["name"], nP["birthday"], nP["gender"], nP["pictureUrl"], nP["id"])) con.commit() return {"msg": "Updated {}".format(nP["name"]), "newPetData": newPetData}, 200 except Exception as e: print(e) return {"msg": "unable to update {}: {}".format(nP["name"], e)}, 400 finally: print("\n")