コード例 #1
0
    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)
コード例 #2
0
    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)
コード例 #3
0
    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
コード例 #4
0
    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
コード例 #5
0
    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
コード例 #6
0
    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
コード例 #7
0
    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]
                    }
                }
コード例 #8
0
 def update(self):
     with DbConnection() as db:
         query = "UPDATE items SET price=? WHERE name=?"
         db.cursor.execute(query, (self.name, self.price))
コード例 #9
0
 def insert(self):
     with DbConnection() as db:
         query = "INSERT INTO items VALUES (NULL, ?, ?)"
         db.cursor.execute(query, (self.name, self.price))
コード例 #10
0
    def delete(self, name):
        with DbConnection() as db:
            query = "DELETE FROM items WHERE name = ?"
            db.cursor.execute(query, (name, ))

        return {"message": "Item deleted"}, 200
コード例 #11
0
 def update(cls, item):
     with DbConnection() as db:
         query = "UPDATE items SET price=? WHERE name=?"
         db.cursor.execute(query, (item["price"], item["name"]))
コード例 #12
0
 def insert(cls, item):
     with DbConnection() as db:
         query = "INSERT INTO items VALUES (NULL, ?, ?)"
         db.cursor.execute(query, (item["name"], item["price"]))