feat: add entity

This commit is contained in:
arkohut 2024-06-01 13:03:53 +08:00
parent 929b81ff41
commit 121c8c51b1
4 changed files with 20 additions and 14 deletions

View File

@ -1,6 +1,6 @@
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from .schemas import Library, NewLibraryParam, Folder from .schemas import Library, NewLibraryParam, Folder, NewEntityParam, Entity
from .models import LibraryModel, FolderModel from .models import LibraryModel, FolderModel, EntityModel, EntityModel
def get_library_by_id(library_id: int, db: Session) -> Library | None: def get_library_by_id(library_id: int, db: Session) -> Library | None:
@ -24,3 +24,14 @@ def create_library(library: NewLibraryParam, db: Session) -> Library:
folders=[Folder(id=db_folder.id, name=db_folder.path) for db_folder in db_library.folders], folders=[Folder(id=db_folder.id, name=db_folder.path) for db_folder in db_library.folders],
plugins=[] plugins=[]
) )
def create_entity(library_id: int, entity: NewEntityParam, db: Session) -> Entity:
db_entity = EntityModel(
**entity.model_dump(),
library_id=library_id
)
db.add(db_entity)
db.commit()
db.refresh(db_entity)
return db_entity

View File

@ -95,5 +95,5 @@ class LibraryPluginModel(Base):
# Create the database engine with the path from config # Create the database engine with the path from config
engine = create_engine(f"sqlite:///:memory:", echo=True) engine = create_engine(f"sqlite:///{get_database_path()}", echo=True)
Base.metadata.create_all(engine) Base.metadata.create_all(engine)

View File

@ -26,9 +26,8 @@ class NewFolderParam(BaseModel):
class NewEntityParam(BaseModel): class NewEntityParam(BaseModel):
filename: str filename: str
path: str filepath: str
size: int size: int
created_at: datetime
file_created_at: datetime file_created_at: datetime
file_last_modified_at: datetime file_last_modified_at: datetime
file_type: str file_type: str
@ -92,7 +91,7 @@ class Library(BaseModel):
class Entity(BaseModel): class Entity(BaseModel):
id: int id: int
path: str filepath: str
filename: str filename: str
size: int size: int
file_created_at: datetime file_created_at: datetime

View File

@ -6,7 +6,7 @@ from sqlalchemy.orm import sessionmaker
from typing import List from typing import List
from .config import get_database_path from .config import get_database_path
from .crud import get_library_by_id, create_library from .crud import get_library_by_id, create_library, create_entity
from .schemas import ( from .schemas import (
Library, Library,
Folder, Folder,
@ -57,14 +57,10 @@ def new_folder(
@app.post("/libraries/{library_id}/entities", response_model=Entity) @app.post("/libraries/{library_id}/entities", response_model=Entity)
def new_entity( def new_entity(
entity: NewEntityParam, library_id: int, db: Session = Depends(get_db) new_entity: NewEntityParam, library_id: int, db: Session = Depends(get_db)
): ):
entity = create_entity(library_id, new_entity, db)
db_entity = Entity(**entity.model_dump(), library_id=library_id) return entity
db.add(db_entity)
db.commit()
db.refresh(db_entity)
return db_entity
@app.post("/plugins", response_model=Plugin) @app.post("/plugins", response_model=Plugin)