mirror of
https://gitflic.ru/project/alt-gnome/karkas.git
synced 2025-09-26 18:29:06 +03:00
Написал интерфейс взаимодействия с БД и покрыл тестами
This commit is contained in:
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
0
src/core/__init__.py
Normal file
0
src/core/__init__.py
Normal file
3
src/core/database/__init__.py
Normal file
3
src/core/database/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .api import *
|
||||
from .models import *
|
||||
from .models.base import database
|
92
src/core/database/api.py
Normal file
92
src/core/database/api.py
Normal file
@@ -0,0 +1,92 @@
|
||||
from src.core.database.models.chats import Chats
|
||||
from src.core.database.models.messages import Messages
|
||||
from src.core.database.models.users import Users
|
||||
|
||||
import peewee as pw
|
||||
|
||||
|
||||
def create_tables(db: pw.SqliteDatabase):
|
||||
"""Создание таблиц"""
|
||||
for table in Chats, Messages, Users:
|
||||
if not table.table_exists():
|
||||
db.create_tables([table])
|
||||
|
||||
|
||||
def add_chat(chat_id, chat_role, chat_stats=0, chat_federation=0):
|
||||
chat, created = Chats.get_or_create(id=chat_id, defaults={
|
||||
'chat_role': chat_role,
|
||||
'chat_stats': chat_stats,
|
||||
'chat_federation': chat_federation
|
||||
})
|
||||
if not created:
|
||||
# Обновить существующий чат, если он уже существует
|
||||
chat.chat_role = chat_role
|
||||
chat.chat_stats = chat_stats
|
||||
chat.chat_federation = chat_federation
|
||||
chat.save()
|
||||
|
||||
|
||||
def add_user(user_id, user_name, user_tag=None, user_role=0, user_stats=0, user_rep=0):
|
||||
user, created = Users.get_or_create(id=user_id, defaults={
|
||||
'user_name': user_name,
|
||||
'user_tag': user_tag,
|
||||
'user_role': user_role,
|
||||
'user_stats': user_stats,
|
||||
'user_rep': user_rep
|
||||
})
|
||||
if not created:
|
||||
# Обновить существующего пользователя, если он уже существует
|
||||
user.user_name = user_name
|
||||
user.user_tag = user_tag
|
||||
user.user_role = user_role
|
||||
user.user_stats = user_stats
|
||||
user.user_rep = user_rep
|
||||
user.save()
|
||||
|
||||
|
||||
def add_message(message_id, message_text, message_sender, answer_id):
|
||||
Messages.create(
|
||||
id=message_id,
|
||||
message_text=message_text,
|
||||
message_id_sender=message_sender,
|
||||
answer_to_id=answer_id
|
||||
)
|
||||
|
||||
|
||||
def get_chat(chat_id):
|
||||
return Chats.get_or_none(Chats.id == chat_id)
|
||||
|
||||
|
||||
def get_user(user_id):
|
||||
return Users.get_or_none(Users.id == user_id)
|
||||
|
||||
|
||||
def get_user_role(user_id):
|
||||
user = Users.get_or_none(Users.id == user_id)
|
||||
return user.user_role if user else None
|
||||
|
||||
|
||||
def get_message(message_id):
|
||||
return Messages.get_or_none(Messages.id == message_id)
|
||||
|
||||
|
||||
def change_user_name(user_id, new_user_name):
|
||||
query = Users.update(user_name=new_user_name).where(Users.id == user_id)
|
||||
query.execute()
|
||||
|
||||
|
||||
# Аналогично для других функций обновления...
|
||||
|
||||
def update_user_stats():
|
||||
query = Users.update(user_stats=Users.user_stats + 1)
|
||||
query.execute()
|
||||
|
||||
|
||||
def update_user_rep():
|
||||
query = Users.update(user_rep=Users.user_rep + 1)
|
||||
query.execute()
|
||||
|
||||
|
||||
def update_chat_stats():
|
||||
query = Chats.update(chat_stats=Chats.chat_stats + 1)
|
||||
query.execute()
|
1
src/core/database/models/__init__.py
Normal file
1
src/core/database/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .base import BaseModel
|
8
src/core/database/models/base.py
Normal file
8
src/core/database/models/base.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import peewee as pw
|
||||
|
||||
database = pw.SqliteDatabase("database/OCAB.db")
|
||||
|
||||
|
||||
class BaseModel(pw.Model):
|
||||
class Meta:
|
||||
database = database
|
9
src/core/database/models/chats.py
Normal file
9
src/core/database/models/chats.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from .base import BaseModel
|
||||
|
||||
import peewee as pw
|
||||
|
||||
|
||||
class Chats(BaseModel):
|
||||
chat_role = pw.IntegerField(null=False)
|
||||
chat_stats = pw.IntegerField(null=False)
|
||||
chat_federation = pw.IntegerField(null=False)
|
9
src/core/database/models/messages.py
Normal file
9
src/core/database/models/messages.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from .base import BaseModel
|
||||
|
||||
import peewee as pw
|
||||
|
||||
|
||||
class Messages(BaseModel):
|
||||
message_text = pw.TextField(null=False)
|
||||
message_id_sender = pw.IntegerField(null=False)
|
||||
answer_to_id = pw.IntegerField(null=True)
|
11
src/core/database/models/users.py
Normal file
11
src/core/database/models/users.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from .base import BaseModel
|
||||
|
||||
import peewee as pw
|
||||
|
||||
|
||||
class Users(BaseModel):
|
||||
user_name = pw.CharField(null=False) # до 255 символов
|
||||
user_tag = pw.CharField(null=True)
|
||||
user_role = pw.IntegerField(null=True)
|
||||
user_stats = pw.IntegerField(null=True)
|
||||
user_rep = pw.IntegerField(null=True)
|
1
src/modules/__init__.py
Normal file
1
src/modules/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
Reference in New Issue
Block a user