2022-04-14 07:18:45 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from model.base import ServiceEnum
|
|
|
|
from service.base import CreateUserInfoDBDataFromSQLData, UserInfoData, CreatCookieDictFromSQLData, \
|
|
|
|
CreatQuestionFromSQLData, QuestionData, AnswerData, CreatAnswerFromSQLData
|
2022-06-09 12:04:38 +00:00
|
|
|
from utils.mysql import MySQL
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AsyncRepository:
|
2022-06-09 12:04:38 +00:00
|
|
|
def __init__(self, mysql: MySQL):
|
|
|
|
self.mysql = mysql
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def update_cookie(self, user_id: int, cookie: str, service: ServiceEnum):
|
2022-06-19 11:37:31 +00:00
|
|
|
if service == ServiceEnum.HYPERION:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
UPDATE `mihoyo_cookie`
|
|
|
|
SET cookie=%s
|
|
|
|
WHERE user_id=%s;
|
|
|
|
"""
|
|
|
|
elif service == ServiceEnum.HOYOLAB:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
UPDATE `hoyoverse_cookie`
|
|
|
|
SET cookie=%s
|
|
|
|
WHERE user_id=%s;
|
|
|
|
"""
|
|
|
|
else:
|
|
|
|
query = ""
|
|
|
|
query_args = (cookie, user_id)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def set_cookie(self, user_id: int, cookie: str, service: ServiceEnum):
|
2022-06-19 11:37:31 +00:00
|
|
|
if service == ServiceEnum.HYPERION:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
INSERT INTO `mihoyo_cookie`
|
|
|
|
(user_id,cookie)
|
|
|
|
VALUES
|
|
|
|
(%s,%s)
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
cookie=VALUES(cookie);
|
|
|
|
"""
|
|
|
|
elif service == ServiceEnum.HOYOLAB:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
INSERT INTO `hoyoverse_cookie`
|
|
|
|
(user_id,cookie)
|
|
|
|
VALUES
|
|
|
|
(%s,%s)
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
cookie=VALUES(cookie);
|
|
|
|
"""
|
|
|
|
else:
|
|
|
|
raise ValueError()
|
|
|
|
query_args = (user_id, cookie)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def set_user_info(self, user_id: int, mihoyo_game_uid: int, hoyoverse_game_uid: int, service: int):
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
INSERT INTO `user`
|
|
|
|
(user_id,mihoyo_game_uid,hoyoverse_game_uid,service)
|
|
|
|
VALUES
|
|
|
|
(%s,%s,%s,%s)
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
mihoyo_game_uid=VALUES(mihoyo_game_uid),
|
|
|
|
hoyoverse_game_uid=VALUES(hoyoverse_game_uid),
|
|
|
|
service=VALUES(service);
|
|
|
|
"""
|
|
|
|
query_args = (user_id, mihoyo_game_uid, hoyoverse_game_uid, service)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def read_mihoyo_cookie(self, user_id) -> dict:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
SELECT cookie
|
|
|
|
FROM `mihoyo_cookie`
|
|
|
|
WHERE user_id=%s;
|
|
|
|
"""
|
|
|
|
query_args = (user_id,)
|
2022-06-09 12:04:38 +00:00
|
|
|
data = await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
if len(data) == 0:
|
|
|
|
return {}
|
|
|
|
return CreatCookieDictFromSQLData(data[0])
|
|
|
|
|
|
|
|
async def read_hoyoverse_cookie(self, user_id) -> dict:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
SELECT cookie
|
|
|
|
FROM `hoyoverse_cookie`
|
|
|
|
WHERE user_id=%s;
|
|
|
|
"""
|
|
|
|
query_args = (user_id,)
|
2022-06-09 12:04:38 +00:00
|
|
|
data = await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
if len(data) == 0:
|
|
|
|
return {}
|
|
|
|
return CreatCookieDictFromSQLData(data[0])
|
|
|
|
|
|
|
|
async def read_user_info(self, user_id) -> UserInfoData:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
SELECT user_id,mihoyo_game_uid,hoyoverse_game_uid,service
|
|
|
|
FROM `user`
|
|
|
|
WHERE user_id=%s;
|
|
|
|
"""
|
|
|
|
query_args = (user_id,)
|
2022-06-09 12:04:38 +00:00
|
|
|
data = await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
if len(data) == 0:
|
|
|
|
return UserInfoData()
|
|
|
|
return CreateUserInfoDBDataFromSQLData(data[0])
|
|
|
|
|
|
|
|
async def read_question_list(self) -> List[QuestionData]:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
SELECT id,question
|
|
|
|
FROM `question`
|
|
|
|
"""
|
|
|
|
query_args = ()
|
2022-06-09 12:04:38 +00:00
|
|
|
data = await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
return CreatQuestionFromSQLData(data)
|
|
|
|
|
|
|
|
async def read_answer_form_question_id(self, question_id: int) -> List[AnswerData]:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
SELECT id,question_id,is_correct,answer
|
|
|
|
FROM `answer`
|
|
|
|
WHERE question_id=%s;
|
|
|
|
"""
|
|
|
|
query_args = (question_id,)
|
2022-06-09 12:04:38 +00:00
|
|
|
data = await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
return CreatAnswerFromSQLData(data)
|
|
|
|
|
|
|
|
async def save_question(self, question: str):
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
INSERT INTO `question`
|
|
|
|
(question)
|
|
|
|
VALUES
|
|
|
|
(%s)
|
|
|
|
"""
|
|
|
|
query_args = (question,)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def read_question(self, question: str) -> QuestionData:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
SELECT id,question
|
|
|
|
FROM `question`
|
|
|
|
WHERE question=%s;
|
|
|
|
"""
|
|
|
|
query_args = (question,)
|
2022-06-09 12:04:38 +00:00
|
|
|
data = await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
return CreatQuestionFromSQLData(data)[0]
|
|
|
|
|
|
|
|
async def save_answer(self, question_id: int, is_correct: int, answer: str):
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
INSERT INTO `answer`
|
|
|
|
(question_id,is_correct,answer)
|
|
|
|
VALUES
|
|
|
|
(%s,%s,%s)
|
|
|
|
"""
|
|
|
|
query_args = (question_id, is_correct, answer)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def delete_question(self, question_id: int):
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
DELETE FROM `question`
|
|
|
|
WHERE id=%s;
|
|
|
|
"""
|
|
|
|
query_args = (question_id,)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def delete_answer(self, answer_id: int):
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
DELETE FROM `answer`
|
|
|
|
WHERE id=%s;
|
|
|
|
"""
|
|
|
|
query_args = (answer_id,)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def delete_admin(self, user_id: int):
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
DELETE FROM `admin`
|
|
|
|
WHERE user_id=%s;
|
|
|
|
"""
|
|
|
|
query_args = (user_id,)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def add_admin(self, user_id: int):
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
INSERT INTO `admin`
|
|
|
|
(user_id)
|
|
|
|
VALUES
|
|
|
|
(%s)
|
|
|
|
"""
|
|
|
|
query_args = (user_id,)
|
2022-06-09 12:04:38 +00:00
|
|
|
await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def get_admin(self) -> List[int]:
|
2022-05-28 07:27:22 +00:00
|
|
|
query = """
|
2022-04-14 07:18:45 +00:00
|
|
|
SELECT user_id
|
|
|
|
FROM `admin`
|
|
|
|
"""
|
|
|
|
query_args = ()
|
2022-06-09 12:04:38 +00:00
|
|
|
data = await self.mysql.execute_and_fetchall(query, query_args)
|
2022-04-14 07:18:45 +00:00
|
|
|
if len(data) == 0:
|
|
|
|
return []
|
2022-06-19 05:11:13 +00:00
|
|
|
return [i[0] for i in data]
|