Compare commits
No commits in common. "67856a849dfbc0a052bbadc3de08eac4a5592284" and "9d7df8202b3a68c330007b8cd1fc0c5715dd0eac" have entirely different histories.
67856a849d
...
9d7df8202b
|
@ -1,11 +0,0 @@
|
|||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
from mirrortea.models.user import User
|
||||
|
||||
|
||||
class MatrixRoom(BaseModel):
|
||||
""""""
|
||||
|
||||
matrix_room_id: str
|
||||
connect_to_user: User
|
|
@ -8,8 +8,8 @@ class User(BaseModel):
|
|||
"""
|
||||
|
||||
first_name: str
|
||||
last_name: Optional[str]
|
||||
last_named: Optional[str]
|
||||
|
||||
user_id: str
|
||||
id: str
|
||||
username: Optional[str]
|
||||
avatar_hash: Optional[str]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from .sqlite_repository import (
|
||||
from repository.sqlite_repository import (
|
||||
SQLiteDatabaseRepository,
|
||||
)
|
||||
|
||||
repository = SQLiteDatabaseRepository
|
||||
repository = SQLiteDatabaseRepository()
|
||||
|
|
|
@ -1,34 +1,7 @@
|
|||
from abc import ABC, abstractmethod
|
||||
|
||||
from ..models.matrix_room import MatrixRoom
|
||||
from ..models.user import User
|
||||
|
||||
|
||||
class AbstractDatabaseRepository(ABC):
|
||||
@abstractmethod
|
||||
def create_tables(self) -> None:
|
||||
"""Init tables in database"""
|
||||
|
||||
@abstractmethod
|
||||
def create_user(self, user: User) -> User:
|
||||
""""""
|
||||
|
||||
@abstractmethod
|
||||
def get_user_by_id(self) -> User:
|
||||
""""""
|
||||
|
||||
@abstractmethod
|
||||
def delete_user(self) -> None:
|
||||
""""""
|
||||
|
||||
@abstractmethod
|
||||
def create_matrix_room(self) -> MatrixRoom:
|
||||
""""""
|
||||
|
||||
@abstractmethod
|
||||
def get_matrix_room(self) -> MatrixRoom:
|
||||
""""""
|
||||
|
||||
@abstractmethod
|
||||
def delete_matrix_room(self) -> None:
|
||||
""""""
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
class UserNotFoundError(Exception):
|
||||
"""User not found!"""
|
||||
|
||||
|
||||
class UserAlreadyExistError(Exception):
|
||||
"""Token already exits!"""
|
||||
|
||||
|
||||
class MatrixRoomNotFoundError(Exception):
|
||||
"""Matrix room not found!"""
|
||||
|
||||
|
||||
class MatrixRoomAlreadyExistError(Exception):
|
||||
"""matrix room already exits!"""
|
|
@ -1,27 +1,27 @@
|
|||
# PLATFORM_USER_MATRIX_CHATS_SQL = """
|
||||
# CREATE TABLE IF NOT EXISTS platforms_user_matrix_chats (
|
||||
# id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
# plstform_user_id INTEGER NOT NULL,
|
||||
# matrix_chat_id INTEGER NOT NULL,
|
||||
# FOREIGN KEY(platform_user_id) REFERENCES platforms_users(id),
|
||||
# FOREIGN KEY(matrix_chat_id) REFERENCES matrix_chats(id)
|
||||
# );
|
||||
# """
|
||||
TELEGRAM_USER_MATRIX_CHATS_SQL = """
|
||||
CREATE TABLE IF NOT EXISTS telegram_user_matrix_chats (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
telegram_user_id INTEGER NOT NULL,
|
||||
matrix_chat_id INTEGER NOT NULL,
|
||||
FOREIGN KEY(telegram_user_id) REFERENCES telegram_users(id),
|
||||
FOREIGN KEY(matrix_chat_id) REFERENCES matrix_chats(id)
|
||||
);
|
||||
"""
|
||||
|
||||
PLATFORMS_USERS_SQL = """
|
||||
CREATE TABLE IF NOT EXISTS platforms_users
|
||||
TELEGRAM_USERS_SQL = """
|
||||
CREATE TABLE IF NOT EXISTS telegram_users
|
||||
(
|
||||
user_id INTEGER(100) PRIMARY KEY NOT NULL,
|
||||
first_name TEXT(100) NOT NULL,
|
||||
last_name TEXT(100),
|
||||
username TEXT(100),
|
||||
avatar_hash BLOB
|
||||
id INTEGER PRIMARY KEY NOT NULL (15),
|
||||
first_name TEXT NOT NULL (50),
|
||||
last_name TEXT (50),
|
||||
username TEXT (50),
|
||||
avatar_hash BLOB,
|
||||
);
|
||||
"""
|
||||
|
||||
MATRIX_ROOMS_SQL = """
|
||||
CREATE TABLE IF NOT EXISTS matrix_rooms
|
||||
CREATE TABLE IF NOT EXISTS telegram_users
|
||||
(
|
||||
matrix_room_id TEXT(100) PRIMARY KEY NOT NULL
|
||||
id TEXT PRIMARY KEY NOT NULL
|
||||
);
|
||||
"""
|
||||
|
|
|
@ -1,103 +1,31 @@
|
|||
import sqlite3
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from collections import namedtuple
|
||||
|
||||
from mirrortea.repository.abstract_db_repository import AbstractDatabaseRepository
|
||||
from repository.abstract_db_repository import AbstractDatabaseRepository
|
||||
|
||||
from mirrortea.models.matrix_room import MatrixRoom
|
||||
from mirrortea.models.user import User
|
||||
|
||||
from mirrortea.repository.exceptions import (
|
||||
UserNotFoundError,
|
||||
MatrixRoomAlreadyExistError,
|
||||
MatrixRoomNotFoundError,
|
||||
UserAlreadyExistError,
|
||||
)
|
||||
|
||||
from mirrortea.repository.sqlite_init_tables import (
|
||||
# PLATFORM_USER_MATRIX_CHATS_SQL,
|
||||
PLATFORMS_USERS_SQL,
|
||||
from repository.sqlite_init_tables import (
|
||||
TELEGRAM_USER_MATRIX_CHATS_SQL,
|
||||
TELEGRAM_USERS_SQL,
|
||||
MATRIX_ROOMS_SQL,
|
||||
)
|
||||
|
||||
TABLES_LIST = [
|
||||
# PLATFORM_USER_MATRIX_CHATS_SQL,
|
||||
PLATFORMS_USERS_SQL,
|
||||
TELEGRAM_USER_MATRIX_CHATS_SQL,
|
||||
TELEGRAM_USERS_SQL,
|
||||
MATRIX_ROOMS_SQL,
|
||||
]
|
||||
|
||||
PATH_TO_DB = "database.db"
|
||||
|
||||
|
||||
class SQLiteDatabaseRepository(AbstractDatabaseRepository):
|
||||
def __init__(self, path=PATH_TO_DB):
|
||||
def __init__(self, app, path: Path):
|
||||
self.path = path
|
||||
self.conn = sqlite3.connect(self.path)
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
def _namedtuple_factory(cursor, row):
|
||||
fields = [column[0] for column in cursor.description]
|
||||
cls = namedtuple("Row", fields)
|
||||
return cls._make(row)
|
||||
self.conn = sqlite3.connect(path)
|
||||
|
||||
@staticmethod
|
||||
def create_tables(self) -> None:
|
||||
"""Init tables in database"""
|
||||
|
||||
for table in TABLES_LIST:
|
||||
self.conn.execute(table)
|
||||
self.conn.commit()
|
||||
|
||||
def get_user_by_id(self, user_id: str) -> User:
|
||||
"""Get user by user id"""
|
||||
|
||||
self.conn.row_factory = self._namedtuple_factory
|
||||
self.cur_userdata = self.conn.execute(
|
||||
"SELECT user_id AS user_id, first_name AS first_name, last_name AS last_name, username AS username, avatar_hash AS avatar_hash FROM platforms_users WHERE user_id = ?",
|
||||
(user_id,),
|
||||
)
|
||||
|
||||
self.userdata = self.cur_userdata.fetchone()
|
||||
|
||||
if self.userdata == ():
|
||||
return UserNotFoundError("User not found!")
|
||||
|
||||
return User(
|
||||
id=user_id,
|
||||
first_name=self.userdata.user_id,
|
||||
last_name=self.userdata.first_name,
|
||||
username=self.userdata.username,
|
||||
avatar_hash=self.userdata.avatar_hash,
|
||||
)
|
||||
|
||||
def create_user(self, user: User) -> User:
|
||||
"""Create user"""
|
||||
try:
|
||||
self.cursor.execute(
|
||||
"INSERT INTO platforms_users VALUES (?, ?, ?, ?, ?)",
|
||||
(
|
||||
user.user_id,
|
||||
user.first_name,
|
||||
user.last_name,
|
||||
user.username,
|
||||
user.avatar_hash,
|
||||
),
|
||||
).fetchall()
|
||||
|
||||
except sqlite3.IntegrityError:
|
||||
return UserAlreadyExistError("User already exist!")
|
||||
|
||||
self.conn.commit()
|
||||
|
||||
return self.get_user_by_id(user_id=user.user_id)
|
||||
|
||||
def delete_user(self) -> None:
|
||||
""""""
|
||||
|
||||
def create_matrix_room(self) -> MatrixRoom:
|
||||
""""""
|
||||
|
||||
def get_matrix_room(self) -> MatrixRoom:
|
||||
""""""
|
||||
|
||||
def delete_matrix_room(self) -> None:
|
||||
""""""
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
import pytest
|
||||
import os
|
||||
|
||||
from mirrortea.models.user import User
|
||||
from mirrortea.repository import repository
|
||||
|
||||
|
||||
PATH = "tests/test_database.db"
|
||||
|
||||
|
||||
def create_repository_object():
|
||||
return repository(PATH)
|
||||
|
||||
|
||||
def test_create_tables():
|
||||
os.system("rm -rf " + PATH)
|
||||
repo = create_repository_object()
|
||||
assert repo.create_tables() is None
|
||||
|
||||
|
||||
def test_get_user_by_id():
|
||||
repo = create_repository_object()
|
||||
assert repo.get_user_by_id(user_id="123") is None
|
||||
|
||||
|
||||
def test_create_user():
|
||||
repo = create_repository_object()
|
||||
assert (
|
||||
repo.create_user(
|
||||
User(
|
||||
user_id="123",
|
||||
first_name="dettlaff",
|
||||
# last_name=None,
|
||||
# username=None,
|
||||
# avatar_hash=None,
|
||||
)
|
||||
)
|
||||
is None
|
||||
)
|
Reference in a new issue