<reponame>star-junk/references

from user import DB_NAME
import sqlite3
from flask_jwt import jwt_required
from flask_restful import Resource, reqparse

class Item(Resource):

    parser = reqparse.RequestParser()
    parser.add_argument('price', 
            type=float,
            required=True,
            help="This field cannot be blank!"
    )

    @jwt_required()
    def get(self, name):
        try:
            item = self.find_by_name(name)
            if not item:
                return {"message": "item not found"}, 404
            return {"item": item}
        except:
            return {"message": "internal server error"}, 500

    @classmethod
    def find_by_name(cls, name):
        connection = sqlite3.connect(DB_NAME)
        cursor = connection.cursor()
        query = 'SELECT * from items WHERE name=?'
        result = cursor.execute(query, (name,))
        row = result.fetchone()
        connection.close()
        if row:
            return {"name": row[0], "price":row[1]}
    
    @classmethod
    def insert(cls, item):
        connection = sqlite3.connect(DB_NAME)
        cursor = connection.cursor()
        query = 'INSERT INTO items VALUES (?, ?)'
        cursor.execute(query, (item['name'], item['price']))
        connection.commit()
        connection.close()

    @classmethod
    def update(cls, item):
        connection = sqlite3.connect(DB_NAME)
        cursor = connection.cursor()
        query = 'UPDATE items SET price=? WHERE name=?'
        cursor.execute(query, (item['price'], item['name']))
        connection.commit()
        connection.close()

    
    def post(self, name):
        try:
            if self.find_by_name(name):
                return {"message": "Item with the name '{}' already exist".format(name)}, 400    
            data = Item.parser.parse_args()
            item = {"name": name, "price": data["price"]}
            self.insert(item)
            return {"item": item, "message": "item created"}, 201 
        except:
            return {"message": "internal server error"}, 500

    def delete(self, name):
        try:
            item = self.find_by_name(name)
            if not item:
                return {"message": "Item not found"}, 400
            connection = sqlite3.connect(DB_NAME)
            cursor = connection.cursor()
            cursor.execute('DELETE FROM items WHERE name=?', (name,))
            connection.commit()
            connection.close()
            return {"message": "Item deleted."}
        except:
            return {"message": "internal server error"}, 500

    def put(self, name):
        data = Item.parser.parse_args()
        try:
            item = self.find_by_name(name)
            updated_item = {"name": name, "price": data['price']}
            if not item:
                self.insert(updated_item)
            else:
                self.update(updated_item)
            return {"item": updated_item}
        except:
            return {"message": "internal server error"}, 500



class ItemList(Resource):
    def get(self):
        connection = sqlite3.connect(DB_NAME)
        cursor = connection.cursor()
        query = 'SELECT * from items'
        result = cursor.execute(query)
        items = []
        for row in result:
            items.append({"name": row[0], "price": row[1]})
        connection.close()
        return {"items": items}


from user import DB_NAME
import sqlite3
from flask_jwt import jwt_required
from flask_restful import Resource, reqparse

class Item(Resource):

    parser = reqparse.RequestParser()
    parser.add_argument('price', 
            type=float,
            required=True,
            help="This field cannot be blank!"
    )

    @jwt_required()
    def get(self, name):
        try:
            item = self.find_by_name(name)
            if not item:
                return {"message": "item not found"}, 404
            return {"item": item}
        except:
            return {"message": "internal server error"}, 500

    @classmethod
    def find_by_name(cls, name):
        connection = sqlite3.connect(DB_NAME)
        cursor = connection.cursor()
        query = 'SELECT * from items WHERE name=?'
        result = cursor.execute(query, (name,))
        row = result.fetchone()
        connection.close()
        if row:
            return {"name": row[0], "price":row[1]}
    
    @classmethod
    def insert(cls, item):
        connection = sqlite3.connect(DB_NAME)
        cursor = connection.cursor()
        query = 'INSERT INTO items VALUES (?, ?)'
        cursor.execute(query, (item['name'], item['price']))
        connection.commit()
        connection.close()

    @classmethod
    def update(cls, item):
        connection = sqlite3.connect(DB_NAME)
        cursor = connection.cursor()
        query = 'UPDATE items SET price=? WHERE name=?'
        cursor.execute(query, (item['price'], item['name']))
        connection.commit()
        connection.close()

    
    def post(self, name):
        try:
            if self.find_by_name(name):
                return {"message": "Item with the name '{}' already exist".format(name)}, 400    
            data = Item.parser.parse_args()
            item = {"name": name, "price": data["price"]}
            self.insert(item)
            return {"item": item, "message": "item created"}, 201 
        except:
            return {"message": "internal server error"}, 500

    def delete(self, name):
        try:
            item = self.find_by_name(name)
            if not item:
                return {"message": "Item not found"}, 400
            connection = sqlite3.connect(DB_NAME)
            cursor = connection.cursor()
            cursor.execute('DELETE FROM items WHERE name=?', (name,))
            connection.commit()
            connection.close()
            return {"message": "Item deleted."}
        except:
            return {"message": "internal server error"}, 500

    def put(self, name):
        data = Item.parser.parse_args()
        try:
            item = self.find_by_name(name)
            updated_item = {"name": name, "price": data['price']}
            if not item:
                self.insert(updated_item)
            else:
                self.update(updated_item)
            return {"item": updated_item}
        except:
            return {"message": "internal server error"}, 500



class ItemList(Resource):
    def get(self):
        connection = sqlite3.connect(DB_NAME)
        cursor = connection.cursor()
        query = 'SELECT * from items'
        result = cursor.execute(query)
        items = []
        for row in result:
            items.append({"name": row[0], "price": row[1]})
        connection.close()
        return {"items": items}