Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.1 kB
2
Indexable
from datetime import datetime
import os
import random
import mysql.connector as SQL

db = SQL.connect(
        host='localhost',
        user='root',
        password='root',
        port='3306',
        database='emarket'
    )
sql = db.cursor()


sss = '\'https://p0.pikist.com/photos/591/110/sunset-sea-sky-reflection-water-ocean-evening-landscape-mood.jpg\''

class Server():

    @staticmethod
    def LOGIN(_username,_password):
        sql.execute(f"select * from users where username = '{_username}' and password = '{_password}';")
        data = sql.fetchall()
        return data

    @staticmethod
    def getCategory(_type):
        sql.execute(f"select id from category where name = '{_type}';")
        data = sql.fetchall()
        sql.execute(f"select id,price,discount,name,image from products where idcategory = '{data[0][0]}';")
        return sql.fetchall()

    @staticmethod
    def getProduct(_id):
        sql.execute(f"select * from products where id = '{_id}';")
        return sql.fetchall()

    @staticmethod
    def getSearch(_query):
        sql.execute(f"select id from category where name = '{_query}';")
        data = sql.fetchall()
        sql.execute(f"select id,price,discount,name,image from products where idcategory = '{data[0][0]}';")
        return sql.fetchall()

    @staticmethod
    def getRandom():
        sql.execute('select MAX(id) from products;')
        data = sql.fetchall()
        sample = random.sample(range(1,data[0][0]),3)
        sql.execute(f'select id,price,discount,name,image from products where id = {sample[0]} or id = {sample[1]} or id = {sample[2]};')
        return sql.fetchall()

    @staticmethod
    def signup(username,password,first,last,phone,country,email):

        username = '\'' + username + '\''
        password = '\'' + password + '\''
        first = '\'' + first + '\''
        last = '\'' + last + '\''
        email = '\'' + email + '\''
        country = '\'' + country + '\''
        phone = '\'' + phone + '\''

        ee = f'INSERT INTO users (`first`,`last`,`country`,`email`,`username`,`password`,`phone`,`image`)VALUES({first},{last},{country},{email},{username},{password},{phone},{sss});'
        print(ee)
        sql.execute(ee)
        db.commit()
        return True

    @staticmethod
    def getOrders(idbrand):
        sql.execute(f'select * from orders where idbrand = {idbrand};')
        _data = sql.fetchall()
        print(_data)
        orderData = []

        for data in _data:
            sql.execute(f'select first, last from users where id = {data[1]};')
            _d = sql.fetchall()
            sql.execute(f'select name from products where id = {data[2]};')
            _d2 = sql.fetchall()
            orderData.append([data ,_d ,_d2])

        return orderData

    @staticmethod
    def getProductsForBrand(idbrand):
        sql.execute(f'select * from products where idbrand = {idbrand};')
        _data = sql.fetchall()
        productData = []
        for d in _data:
            sql.execute(f'select name from category where id = {d[1]};')
            productData.append([d , sql.fetchall()])

        print("############")
        print(productData)
        print(len(productData))
        return productData


    @staticmethod
    def postOrder(id, productID):
        sql.execute(f'select price from products where id = {productID};')
        price = sql.fetchall()
        status = '\'On Going\''
        country = '\'Palestine\''
        sql.execute(f'INSERT INTO orders(`iduser`,`idproduct`,`idbrand`,`date`,`price`,`status`,`country`)VALUES({id},{productID},1,NOW(),{price[0][0]},{status},{country});')
        db.commit()

    @staticmethod
    def getUserOrders(id):
        sql.execute(f'select * from orders where iduser = {id};')
        return sql.fetchall()

    @staticmethod
    def getBrandByID(id):
        sql.execute(f'select name from brand where idbrand = {id};')
        data = sql.fetchall()
        return data[0][0]

    @staticmethod
    def brandLogin(username, password):
        sql.execute(f'select * from brand where username = \'{username}\' and password =\'{password}\';')
        data = sql.fetchall()
        print(data)
        if len(data) > 0 :
            print('ee1')
            return data[0]
        else:
            print('ee2')
            return False



    @staticmethod
    def addProduct(name,price,category,discount,image,brandid):

        sql.execute(f'select id from category where name = \'{category}\';')
        _d1 = sql.fetchall()
        print(_d1)
        idc = _d1[0][0]
        print(idc)

        eee = f'INSERT INTO products(`idcategory`,`idbrand`,`price`,`discount`,`name`,`image`)VALUES({idc},{brandid},{price},{discount},\'{name}\',\'{image}\');'
        print(eee)
        sql.execute(eee)
        db.commit()

        return True

    @staticmethod
    def deleteProduct(id):
        sql.execute(f'DELETE FROM products WHERE id = {id};')
        db.commit()
        return True