Created basic backend structure, auth and CRUD endpoints.
This commit is contained in:
62
data/crud.py
Normal file
62
data/crud.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# crud.py
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
from data.models import Connection, User
|
||||
from data.schemas import ConnectionCreate, ConnectionUpdate, UserCreate
|
||||
from core.exceptions import ObjectNotFoundInDB
|
||||
|
||||
async def read_user(db:AsyncSession, user_id:int):
|
||||
result = await db.execute(select(User).filter(User.id == user_id))
|
||||
return result.scalars().first()
|
||||
|
||||
async def read_all_users(db:AsyncSession):
|
||||
result = await db.execute(select(User))
|
||||
return result.scalars().all()
|
||||
|
||||
async def create_user(db: AsyncSession, user: UserCreate):
|
||||
from core.scripts import create_secret
|
||||
db_user = User(**user.model_dump(), api_key=create_secret())
|
||||
db.add(db_user)
|
||||
await db.commit()
|
||||
await db.refresh(db_user)
|
||||
return db_user
|
||||
|
||||
async def delete_user(db: AsyncSession, user_id: int):
|
||||
db_user = await read_user(db, user_id)
|
||||
if db_user:
|
||||
await db.delete(db_user)
|
||||
await db.commit()
|
||||
return db_user
|
||||
else:
|
||||
raise ObjectNotFoundInDB
|
||||
|
||||
async def read_connection(db: AsyncSession, connection_id: int):
|
||||
result = await db.execute(select(Connection).filter(Connection.id == connection_id))
|
||||
return result.scalars().first()
|
||||
|
||||
async def read_all_connections(db: AsyncSession):
|
||||
result = await db.execute(select(Connection))
|
||||
return result.scalars().all()
|
||||
|
||||
async def create_connection(db: AsyncSession, connection: ConnectionCreate, user_id: int):
|
||||
db_connection = Connection(**connection.model_dump(), owner_id=user_id)
|
||||
db.add(db_connection)
|
||||
await db.commit()
|
||||
await db.refresh(db_connection)
|
||||
return db_connection
|
||||
|
||||
async def update_connection(db: AsyncSession, connection_id: int, connection: ConnectionUpdate):
|
||||
db_connection = await read_connection(db, connection_id)
|
||||
if db_connection:
|
||||
for key, value in connection.model_dump().items():
|
||||
setattr(db_connection, key, value)
|
||||
await db.commit()
|
||||
await db.refresh(db_connection)
|
||||
return db_connection
|
||||
|
||||
async def delete_connection(db: AsyncSession, connection_id: int):
|
||||
db_connection = await read_connection(db, connection_id)
|
||||
if db_connection:
|
||||
await db.delete(db_connection)
|
||||
await db.commit()
|
||||
return db_connection
|
||||
22
data/db.py
Normal file
22
data/db.py
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
|
||||
DATABASE_URL = "sqlite+aiosqlite:///files/db.sqlite"
|
||||
|
||||
engine = create_async_engine(DATABASE_URL, echo=False)
|
||||
SessionLocal = sessionmaker(
|
||||
bind=engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
import logging
|
||||
|
||||
logging.basicConfig()
|
||||
logging.getLogger('sqlalchemy').setLevel(logging.WARNING)
|
||||
|
||||
logging.getLogger('sqlalchemy.engine').disabled = True
|
||||
29
data/models.py
Normal file
29
data/models.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# models.py
|
||||
from sqlalchemy import Column, Integer, String, Enum, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from data.db import Base
|
||||
from core.enums import ConnectionTypes, UserRole
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
username = Column(String, unique=True, index=True, nullable=False)
|
||||
role = Column(Enum(UserRole), default=UserRole.user, nullable=False)
|
||||
api_key = Column(String, unique=True, nullable=False)
|
||||
|
||||
# repos = relationship("Connection", back_populates="owner")
|
||||
|
||||
class Connection(Base):
|
||||
__tablename__ = "connections"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
db_name = Column(String, nullable=False)
|
||||
type = Column(Enum(ConnectionTypes), nullable=False)
|
||||
host = Column(String)
|
||||
port = Column(Integer)
|
||||
username = Column(String)
|
||||
password = Column(String)
|
||||
owner_id = Column(Integer, ForeignKey("users.id"))
|
||||
|
||||
# owner = relationship("User", back_populates="connections")
|
||||
59
data/schemas.py
Normal file
59
data/schemas.py
Normal file
@@ -0,0 +1,59 @@
|
||||
# schemas.py
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from core.enums import ConnectionTypes, UserRole
|
||||
|
||||
|
||||
class ConnectionBase(BaseModel):
|
||||
db_name: str
|
||||
type: ConnectionTypes
|
||||
host: str
|
||||
port: int
|
||||
username: str
|
||||
password: str
|
||||
|
||||
|
||||
class ConnectionCreate(ConnectionBase):
|
||||
pass
|
||||
|
||||
|
||||
class ConnectionUpdate(ConnectionBase):
|
||||
pass
|
||||
|
||||
|
||||
class ConnectionInDBBase(ConnectionBase):
|
||||
id: int
|
||||
owner_id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class Connection(ConnectionInDBBase):
|
||||
pass
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
username: str
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
role: UserRole
|
||||
|
||||
|
||||
class UserOut(UserBase):
|
||||
id: int
|
||||
role: UserRole
|
||||
|
||||
|
||||
class UserInDBBase(UserBase):
|
||||
id: int
|
||||
role: UserRole
|
||||
api_key: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class User(UserInDBBase):
|
||||
pass
|
||||
Reference in New Issue
Block a user