2021-01-25 23:30:52 +01:00
|
|
|
from attr import dataclass
|
|
|
|
|
2020-10-13 21:02:40 +02:00
|
|
|
from functions.database import utils
|
|
|
|
|
|
|
|
|
|
|
|
def insert(id, name, fields):
|
2021-01-25 23:21:40 +01:00
|
|
|
if getMeme(name) is not None:
|
|
|
|
return "Deze meme staat al in de database."
|
|
|
|
|
2020-10-13 21:02:40 +02:00
|
|
|
connection = utils.connect()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute("INSERT INTO memes(id, name, fields) VALUES (%s, %s, %s)", [int(id), name.lower(), int(fields)])
|
|
|
|
connection.commit()
|
2021-01-25 23:21:40 +01:00
|
|
|
|
|
|
|
return "{} is toegevoegd aan de database.".format(name[0].upper() + name[1:].lower())
|
2020-10-13 21:02:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def getMeme(name):
|
|
|
|
connection = utils.connect()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute("SELECT * FROM memes WHERE name like %s", ["%" + name.lower() + "%"])
|
|
|
|
result = cursor.fetchall()
|
2021-01-25 23:30:52 +01:00
|
|
|
|
2020-10-13 21:02:40 +02:00
|
|
|
if len(result) == 0:
|
2021-01-25 23:21:40 +01:00
|
|
|
return None
|
2021-01-25 23:30:52 +01:00
|
|
|
|
|
|
|
meme = Meme(result[0][0], result[0][1], result[0][2])
|
|
|
|
|
|
|
|
return meme
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Meme:
|
|
|
|
"""
|
|
|
|
Dataclass to represent a meme in order to avoid having to use [] on tuples all the time
|
|
|
|
"""
|
|
|
|
meme_id: int
|
|
|
|
name: str
|
|
|
|
fields: int
|
2020-10-13 21:02:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def getAllMemes():
|
|
|
|
connection = utils.connect()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute("SELECT * FROM memes")
|
|
|
|
result = cursor.fetchall()
|
|
|
|
return result
|