def find_by_id(cls, _id): with DbConnection() as db: query = "SELECT name, price, id FROM items WHERE id = ?" result = db.cursor.execute(query, (_id, )) row = result.fetchone() if row: return cls(*row)
def find_by_name(cls, name): with DbConnection() as db: query = "SELECT name, price, id FROM items WHERE name = ?" result = db.cursor.execute(query, (name, )) row = result.fetchone() if row: logger.info(f"row: {row}") return cls(*row)
def post(self): data = UserRegister.parser.parse_args() if User.find_by_username(data["username"]): return {"message": "User already exists"}, 409 with DbConnection() as db: query = "INSERT INTO users VALUES (NULL, ?, ?, ?)" db.cursor.execute( query, (data["username"], data["password"], data["email"])) return {"message": "User created successfully"}, 201
def get(self): with DbConnection() as db: query = "SELECT * FROM items" result = db.cursor.execute(query) results = result.fetchall() items = [] for i in results: items.append({"id": i[0], "name": i[1], "price": i[2]}) return {"items": items}, 200
def find_by_id(cls, _id): with DbConnection() as db: query = "SELECT * FROM users WHERE id = ?" result = db.cursor.execute(query, (_id, )) row = result.fetchone() if row: user = cls(*row) else: user = None return user
def find_by_email(cls, email): with DbConnection() as db: query = "SELECT * FROM users WHERE email = ?" result = db.cursor.execute(query, (email, )) row = result.fetchone() if row: user = cls(*row) else: logger.error("No user found!") user = None return user
def find_by_id(cls, _id): with DbConnection() as db: query = "SELECT * FROM items WHERE id = ?" result = db.cursor.execute(query, (_id, )) row = result.fetchone() if row: return { "item": { "id": row[0], "name": row[1], "price": row[2] } }
def update(self): with DbConnection() as db: query = "UPDATE items SET price=? WHERE name=?" db.cursor.execute(query, (self.name, self.price))
def insert(self): with DbConnection() as db: query = "INSERT INTO items VALUES (NULL, ?, ?)" db.cursor.execute(query, (self.name, self.price))
def delete(self, name): with DbConnection() as db: query = "DELETE FROM items WHERE name = ?" db.cursor.execute(query, (name, )) return {"message": "Item deleted"}, 200
def update(cls, item): with DbConnection() as db: query = "UPDATE items SET price=? WHERE name=?" db.cursor.execute(query, (item["price"], item["name"]))
def insert(cls, item): with DbConnection() as db: query = "INSERT INTO items VALUES (NULL, ?, ?)" db.cursor.execute(query, (item["name"], item["price"]))