148 lines
4.5 KiB
Python
148 lines
4.5 KiB
Python
|
from dataclasses import dataclass
|
||
|
import sqlite3
|
||
|
|
||
|
|
||
|
@dataclass
|
||
|
class Bounty:
|
||
|
bounty_id: int
|
||
|
user_id: int
|
||
|
title: str
|
||
|
description: str
|
||
|
languages: str
|
||
|
field: str
|
||
|
|
||
|
def __init__(
|
||
|
self,
|
||
|
bounty_id: int,
|
||
|
user_id: int,
|
||
|
title: str,
|
||
|
description: str,
|
||
|
languages: str,
|
||
|
field: str,
|
||
|
) -> None:
|
||
|
self.bounty_id = bounty_id
|
||
|
self.user_id = user_id
|
||
|
self.title = title
|
||
|
self.description = description
|
||
|
self.languages = languages
|
||
|
self.field = field
|
||
|
|
||
|
|
||
|
class BountyHandler:
|
||
|
connection: sqlite3.Connection = sqlite3.connect("../data/database.db", check_same_thread=False)
|
||
|
cursor: sqlite3.Cursor = connection.cursor()
|
||
|
|
||
|
# Create Bounty
|
||
|
@classmethod
|
||
|
def create_bounty(
|
||
|
cls,
|
||
|
bounty: Bounty
|
||
|
) -> None:
|
||
|
bounty.languages.removeprefix(",")
|
||
|
bounty.languages.removesuffix(",")
|
||
|
print(bounty.languages)
|
||
|
assert set(bounty.languages.split(",")).issubset(
|
||
|
{"c", "cpp", "c#", "css", "go", "html", "java", "js", "python", "ruby", "rust", "other", "any"}
|
||
|
)
|
||
|
assert set(bounty.field.split(",")).issubset(
|
||
|
{"dsa", "ai", "web", "app", "other"}
|
||
|
)
|
||
|
cls.cursor.execute(
|
||
|
f"""
|
||
|
INSERT INTO bounties(user_id, title, description, languages, field)
|
||
|
values(?, ?, ?, ?, ?)
|
||
|
""",
|
||
|
(bounty.bounty_id, bounty.title, bounty.description, bounty.languages, bounty.field)
|
||
|
)
|
||
|
cls.connection.commit()
|
||
|
|
||
|
# Get Bounty
|
||
|
@classmethod
|
||
|
def get_bounty_by_id(cls, bounty_id: int) -> Bounty:
|
||
|
bounties = cls.cursor.execute(
|
||
|
f"""
|
||
|
SELECT user_id, title, description, languages, field
|
||
|
FROM bounties WHERE bounty_id = ?;
|
||
|
""",
|
||
|
(bounty_id, )
|
||
|
).fetchall()
|
||
|
if not bounties:
|
||
|
raise KeyError(f"{bounty_id} does not exist!")
|
||
|
bounty = bounties[0]
|
||
|
return Bounty(
|
||
|
bounty_id=bounty_id,
|
||
|
user_id=bounty[0],
|
||
|
title=bounty[1],
|
||
|
description=bounty[2],
|
||
|
languages=bounty[3],
|
||
|
field=bounty[4],
|
||
|
)
|
||
|
|
||
|
# Get random bounties
|
||
|
@classmethod
|
||
|
def get_randomized_bounty_list(cls):
|
||
|
responses = cls.cursor.execute(
|
||
|
f"""
|
||
|
SELECT bounty_id, user_id, title, description, languages, field FROM bounties ORDER BY RANDOM();
|
||
|
"""
|
||
|
).fetchall()
|
||
|
bounties: list[Bounty] = []
|
||
|
for response in responses:
|
||
|
bounties.append(Bounty(
|
||
|
bounty_id=response[0],
|
||
|
user_id=response[1],
|
||
|
title=response[2],
|
||
|
description=response[3],
|
||
|
languages=response[4],
|
||
|
field=response[5],
|
||
|
))
|
||
|
return bounties
|
||
|
|
||
|
# Rate
|
||
|
@classmethod
|
||
|
def rate(cls, bounty_id: int, user_id: int, rating: int) -> None:
|
||
|
cls.cursor.execute(
|
||
|
f"""
|
||
|
INSERT INTO bounty_ratings(bounty_id, user_id, rating)
|
||
|
values(?, ?, ?)
|
||
|
""",
|
||
|
(bounty_id, user_id, rating)
|
||
|
)
|
||
|
cls.connection.commit()
|
||
|
|
||
|
# Get Rating
|
||
|
@classmethod
|
||
|
def get_sum_of_ratings(cls, bounty_id: int) -> int:
|
||
|
sum_of_ratings: int = cls.cursor.execute(
|
||
|
f"""
|
||
|
SELECT SUM(rating)
|
||
|
FROM bounty_ratings WHERE bounty_id = ?;
|
||
|
""",
|
||
|
(bounty_id,)
|
||
|
).fetchall()[0][0]
|
||
|
return sum_of_ratings if sum_of_ratings else 0
|
||
|
|
||
|
# Get number of Rating
|
||
|
@classmethod
|
||
|
def get_number_of_ratings(cls, bounty_id: int) -> int:
|
||
|
sum_of_ratings: int = cls.cursor.execute(
|
||
|
f"""
|
||
|
SELECT COUNT()
|
||
|
FROM bounty_ratings WHERE bounty_id = ?;
|
||
|
""",
|
||
|
(bounty_id,)
|
||
|
).fetchall()[0][0]
|
||
|
return sum_of_ratings if sum_of_ratings else -1
|
||
|
|
||
|
# Get rater's Rating
|
||
|
@classmethod
|
||
|
def get_rating_of_user(cls, user_id: int, bounty_id: int) -> int:
|
||
|
sum_of_ratings: int = cls.cursor.execute(
|
||
|
f"""
|
||
|
SELECT SUM(rating)
|
||
|
FROM bounty_ratings WHERE bounty_id = ? AND user_id = ?;
|
||
|
""",
|
||
|
(bounty_id, user_id)
|
||
|
).fetchall()[0][0]
|
||
|
return sum_of_ratings if sum_of_ratings else -2
|