import sqlite3


class UserModel:
    def __init__(self,_id,username,password):
        self.id = _id
        self.username = username
        self.password = password

    @classmethod
    def find_by_username(cls,username):
        connection = sqlite3.connect('data.db')
        cursor = connection.cursor() #responsible for executing the queries and getting results
        query ="SELECT * FROM users WHERE username=?"
        result = cursor.execute(query,(username,)) #the second argument needs to be a tuple always so follow this syntax for single values
        row = result.fetchone() #returns the first row / result
        if row:
            user = cls(*row)# this means cls(row[0],row[1],row[2]) cls means current classs
        else:
            user = None
        connection.close()

        return user

    @classmethod
    def find_by_id(cls,_id):
        connection = sqlite3.connect('data.db')
        cursor = connection.cursor() #responsible for executing the queries and getting results
        query ="SELECT * FROM users WHERE id=?"
        result = cursor.execute(query,(_id,)) #the second argument needs to be a tuple always so follow this syntax for single values
        row = result.fetchone() #returns the first row / result
        if row:
            user = cls(*row)# this means cls(row[0],row[1],row[2]) cls means current classs
        else:
            user = None
        connection.close()

        return user
