Compare commits
No commits in common. "main" and "new_branch" have entirely different histories.
main
...
new_branch
4 changed files with 13 additions and 195 deletions
|
|
@ -1,43 +1,22 @@
|
||||||
from fastapi import HTTPException, Depends
|
from fastapi import HTTPException
|
||||||
from sqlalchemy import Integer, String, Boolean
|
from sqlalchemy import Column, Integer, String, Float, Boolean, ForeignKey
|
||||||
from pydantic import BaseModel
|
from sqlalchemy.dialects.postgresql import ARRAY
|
||||||
from sqlalchemy.orm import Session, relationship, mapped_column, Mapped
|
from sqlalchemy.orm import Session, relationship, mapped_column, Mapped
|
||||||
from ..config import Base, get_session_db, user_collection, collection_item
|
from ..config import Base, get_session_db, user_collection, collection_item
|
||||||
from ..auth.models import DBUser
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ..items.models import Items, Item
|
from ..auth.models import DBUser
|
||||||
|
from ..items.models import Items
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
###### SCHEMAS #########
|
|
||||||
|
|
||||||
class CollectionBase(BaseModel):
|
|
||||||
collection_name : str | None = None
|
|
||||||
collection_description : str | None = None
|
|
||||||
visibility : bool | None = None
|
|
||||||
|
|
||||||
class CollectionCreate(CollectionBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class CollectionPublic(CollectionBase):
|
|
||||||
collection_id : int | None = None
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
from_attributes = True #sqlalchemy ile pydantic arasında geçiş yapabilmek için kullanılır
|
|
||||||
|
|
||||||
class CollectionUpdate(CollectionBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
##### veri tabanı modelleri #####
|
##### veri tabanı modelleri #####
|
||||||
class CollectionsDB(Base):
|
class CollectionsDB(Base):
|
||||||
__tablename__ = "collections_table"
|
__tablename__ = "collections_table"
|
||||||
|
|
||||||
collection_id : Mapped[int] = mapped_column(Integer, primary_key=True, index=True, autoincrement=True)
|
collection_id : Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||||
#user_id : Mapped[int] = mapped_column(Integer, ForeignKey("users_table.user_id"), nullable=False) # user_id ile ilişki
|
#user_id : Mapped[int] = mapped_column(Integer, ForeignKey("users_table.user_id"), nullable=False) # user_id ile ilişki
|
||||||
#item_id : Mapped[list[int]] = mapped_column(Integer, ForeignKey("items_table.item_id"), nullable=False) # item_id ile ilişki
|
#item_id : Mapped[list[int]] = mapped_column(Integer, ForeignKey("items_table.item_id"), nullable=False) # item_id ile ilişki
|
||||||
visibility : Mapped[bool] = mapped_column(Boolean, default=True)
|
visibility : Mapped[bool] = mapped_column(Boolean, default=True)
|
||||||
|
|
@ -45,7 +24,7 @@ class CollectionsDB(Base):
|
||||||
collection_description : Mapped[str] = mapped_column(String, default="No description")
|
collection_description : Mapped[str] = mapped_column(String, default="No description")
|
||||||
|
|
||||||
# ilişkiler
|
# ilişkiler
|
||||||
users : Mapped[list['DBUser']] = relationship(
|
users : Mapped['DBUser'] = relationship(
|
||||||
"DBUser",
|
"DBUser",
|
||||||
secondary=user_collection,
|
secondary=user_collection,
|
||||||
back_populates="collections",
|
back_populates="collections",
|
||||||
|
|
@ -60,114 +39,4 @@ class CollectionsDB(Base):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
#### collection bir item listesi birde kullanıcı listesi tutacak
|
#### collection bir item listesi birde kullanıcı listesi tutacak
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def create_colletion(
|
|
||||||
collection: CollectionCreate | None = None,
|
|
||||||
user_id : int | None = None
|
|
||||||
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection oluşturma fonksiyonu
|
|
||||||
"""
|
|
||||||
if collection is None:
|
|
||||||
raise HTTPException(status_code=400, detail="Collection is None returned")
|
|
||||||
|
|
||||||
session = next(get_session_db()) # -> get_session_db() fonksiyonu daima generator döndürür next ile çağırmalısın
|
|
||||||
user = session.query(DBUser).filter(DBUser.user_id == user_id).first()
|
|
||||||
if user is None:
|
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
new_collection = CollectionsDB(
|
|
||||||
collection_name=collection.collection_name,
|
|
||||||
collection_description=collection.collection_description,
|
|
||||||
visibility=collection.visibility
|
|
||||||
)
|
|
||||||
|
|
||||||
new_collection.users.append(user)
|
|
||||||
session.add(new_collection)
|
|
||||||
session.commit()
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Error creating collection: {e}")
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def get_collections(
|
|
||||||
user_id : int | None = None
|
|
||||||
) -> list[CollectionPublic] | None:
|
|
||||||
"""
|
|
||||||
Kullanıcının collectionlarını döndürür
|
|
||||||
"""
|
|
||||||
if user_id is None:
|
|
||||||
raise HTTPException(status_code=400, detail="User id is None")
|
|
||||||
|
|
||||||
session = next(get_session_db()) # -> get_session_db() fonksiyonu daima generator döndürür next ile çağırmalısın
|
|
||||||
collections = session.query(CollectionsDB).filter(CollectionsDB.users.any(user_id=user_id)).all()
|
|
||||||
|
|
||||||
if collections is None:
|
|
||||||
raise HTTPException(status_code=404, detail="No collections found")
|
|
||||||
|
|
||||||
return collections
|
|
||||||
|
|
||||||
def update_collection(
|
|
||||||
collection: CollectionUpdate | None = None,
|
|
||||||
user_id : int | None = None,
|
|
||||||
collection_id : int | None = None
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection güncelleme fonksiyonu
|
|
||||||
"""
|
|
||||||
if collection is None:
|
|
||||||
raise HTTPException(status_code=400, detail="Collection is None returned")
|
|
||||||
|
|
||||||
session = next(get_session_db()) # -> get_session_db() fonksiyonu daima generator döndürür next ile çağırmalısın
|
|
||||||
user = session.query(DBUser).filter(DBUser.user_id == user_id).first()
|
|
||||||
if user is None:
|
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
|
||||||
|
|
||||||
collection_to_update = session.query(CollectionsDB).filter(CollectionsDB.collection_id == collection_id).first()
|
|
||||||
if collection_to_update is None:
|
|
||||||
raise HTTPException(status_code=404, detail="Collection not found")
|
|
||||||
|
|
||||||
try:
|
|
||||||
collection_to_update.collection_name = collection.collection_name
|
|
||||||
collection_to_update.collection_description = collection.collection_description
|
|
||||||
collection_to_update.visibility = collection.visibility
|
|
||||||
|
|
||||||
session.commit()
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Error updating collection: {e}")
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def delete_collection(
|
|
||||||
user_id : int | None = None,
|
|
||||||
collection_id : int | None = None
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection silme fonksiyonu
|
|
||||||
"""
|
|
||||||
if user_id is None or collection_id is None:
|
|
||||||
raise HTTPException(status_code=400, detail="User id or collection id is None")
|
|
||||||
|
|
||||||
session = next(get_session_db()) # -> get_session_db() fonksiyonu daima generator döndürür next ile çağırmalısın
|
|
||||||
user = session.query(DBUser).filter(DBUser.user_id == user_id).first()
|
|
||||||
if user is None:
|
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
|
||||||
|
|
||||||
collection_to_delete = session.query(CollectionsDB).filter(CollectionsDB.collection_id == collection_id).first()
|
|
||||||
if collection_to_delete is None:
|
|
||||||
raise HTTPException(status_code=404, detail="Collection not found")
|
|
||||||
|
|
||||||
try:
|
|
||||||
session.delete(collection_to_delete)
|
|
||||||
session.commit()
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Error deleting collection: {e}")
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
@ -1,60 +1,8 @@
|
||||||
from fastapi import FastAPI, APIRouter
|
from fastapi import FastAPI, APIRouter
|
||||||
from .models import CollectionPublic, CollectionCreate, CollectionUpdate
|
|
||||||
from .models import get_collections, create_colletion, update_collection, delete_collection
|
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix="/collections",
|
prefix="/collections",
|
||||||
tags=["collections"],
|
tags=["collections"],
|
||||||
responses={404: {"description": "Not found"}},
|
responses={404: {"description": "Not found"}},
|
||||||
dependencies=[],
|
dependencies=[],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{user_id}")
|
|
||||||
async def get_collections_api(user_id: int) -> list[CollectionPublic]:
|
|
||||||
"""
|
|
||||||
Kullanıcının collectionlarını döndürür
|
|
||||||
"""
|
|
||||||
|
|
||||||
_collections : list[CollectionPublic] = get_collections(user_id=user_id)
|
|
||||||
|
|
||||||
return _collections
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{user_id}")
|
|
||||||
async def create_collection(
|
|
||||||
user_id: int,
|
|
||||||
collection: CollectionCreate
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection oluşturma fonksiyonu
|
|
||||||
"""
|
|
||||||
_result = create_colletion(user_id=user_id, collection=collection)
|
|
||||||
return _result
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{user_id}/{collection_id}")
|
|
||||||
async def update_collection_api(
|
|
||||||
user_id: int,
|
|
||||||
collection_id : int,
|
|
||||||
collection: CollectionUpdate
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection güncelleme fonksiyonu
|
|
||||||
"""
|
|
||||||
_result = update_collection(user_id=user_id, collection_id=collection_id, collection=collection)
|
|
||||||
return _result
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{user_id}/{collection_id}")
|
|
||||||
async def delete_collection_api(
|
|
||||||
user_id: int,
|
|
||||||
collection_id : int
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection silme fonksiyonu
|
|
||||||
"""
|
|
||||||
_result = delete_collection(user_id=user_id, collection_id=collection_id)
|
|
||||||
return _result
|
|
||||||
|
|
@ -30,11 +30,11 @@ class Base(DeclarativeBase):
|
||||||
#models te içe aktarmayı unutma
|
#models te içe aktarmayı unutma
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
#Base.metadata.drop_all(engine) # Veritabanını her başlangıcta siler burayada dikkat !!!!!!!!
|
Base.metadata.drop_all(engine) # Veritabanını her başlangıcta siler burayada dikkat !!!!!!!!
|
||||||
Base.metadata.create_all(bind=engine) # Veritabanını oluşturur
|
Base.metadata.create_all(bind=engine) # Veritabanını oluşturur
|
||||||
|
|
||||||
# Session dependency (FastAPI için)
|
# Session dependency (FastAPI için)
|
||||||
def get_session_db() -> 'Generator[Session, None]':
|
def get_session_db():
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
yield db
|
yield db
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,10 @@ class Items(Base):
|
||||||
item_score: Mapped[float] = mapped_column(Float, default=0.0)
|
item_score: Mapped[float] = mapped_column(Float, default=0.0)
|
||||||
|
|
||||||
# ilişkiler
|
# ilişkiler
|
||||||
collections : Mapped[list['CollectionsDB']]= relationship(
|
collections : Mapped['CollectionsDB']= relationship(
|
||||||
"CollectionsDB",
|
"CollectionsDB",
|
||||||
secondary=collection_item,
|
secondary=collection_item,
|
||||||
back_populates="items",
|
back_populates="items",
|
||||||
lazy='select'
|
lazy='select'
|
||||||
) #back_populates karşı tarafın ismi
|
) #back_populates karşı tarafın ismi
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue