Spaces:
Running
Running
Upload 4 files
Browse files- db/blog_schema.py +20 -0
- db/database.py +25 -0
- db/models.py +27 -0
- db/schemas.py +66 -0
db/blog_schema.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
|
| 4 |
+
class PostResponse(BaseModel):
|
| 5 |
+
id: int
|
| 6 |
+
title: str
|
| 7 |
+
slug: str
|
| 8 |
+
excerpt: str
|
| 9 |
+
content: str
|
| 10 |
+
thumbnail_url: str | None = None
|
| 11 |
+
product_url: str | None = None
|
| 12 |
+
image_urls: str | None = None
|
| 13 |
+
published_at: datetime
|
| 14 |
+
tags: str | None = None # ✅ Added
|
| 15 |
+
rating: float | None = None # ✅ New field
|
| 16 |
+
repeat_purchases: str | None = None # ✅ New field
|
| 17 |
+
|
| 18 |
+
model_config = {
|
| 19 |
+
"from_attributes": True
|
| 20 |
+
}
|
db/database.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# db/database.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from sqlalchemy import create_engine
|
| 5 |
+
from sqlalchemy.ext.declarative import declarative_base
|
| 6 |
+
from sqlalchemy.orm import sessionmaker
|
| 7 |
+
from fastapi.security import OAuth2PasswordBearer
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# ✅ PostgreSQL URL from Neon
|
| 13 |
+
SQLALCHEMY_DATABASE_URL = os.getenv("NEON_DATABASE_URL") # put this in .env
|
| 14 |
+
|
| 15 |
+
# ✅ Create PostgreSQL engine
|
| 16 |
+
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
| 17 |
+
|
| 18 |
+
# ✅ PostgreSQL session
|
| 19 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 20 |
+
|
| 21 |
+
# ✅ Base class for models
|
| 22 |
+
Base = declarative_base()
|
| 23 |
+
|
| 24 |
+
# ✅ Token config
|
| 25 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
db/models.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# db/models.py
|
| 2 |
+
|
| 3 |
+
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
|
| 4 |
+
from sqlalchemy.orm import relationship
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from .database import Base
|
| 7 |
+
|
| 8 |
+
class User(Base):
|
| 9 |
+
__tablename__ = "users"
|
| 10 |
+
|
| 11 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 12 |
+
email = Column(String, unique=True, index=True, nullable=False)
|
| 13 |
+
name = Column(String, nullable=False)
|
| 14 |
+
hashed_password = Column(String, nullable=False)
|
| 15 |
+
created_at = Column(DateTime, default=datetime.utcnow)
|
| 16 |
+
|
| 17 |
+
threads = relationship("Thread", back_populates="owner")
|
| 18 |
+
|
| 19 |
+
class Thread(Base):
|
| 20 |
+
__tablename__ = "threads"
|
| 21 |
+
|
| 22 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 23 |
+
title = Column(String, default="New Chat")
|
| 24 |
+
created_at = Column(DateTime, default=datetime.utcnow)
|
| 25 |
+
user_id = Column(Integer, ForeignKey("users.id"))
|
| 26 |
+
|
| 27 |
+
owner = relationship("User", back_populates="threads")
|
db/schemas.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, EmailStr
|
| 2 |
+
from typing import List, Optional
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
# --------------------------
|
| 6 |
+
# User Schemas
|
| 7 |
+
# --------------------------
|
| 8 |
+
|
| 9 |
+
class UserCreate(BaseModel):
|
| 10 |
+
name: str
|
| 11 |
+
email: EmailStr
|
| 12 |
+
password: str
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class UserLogin(BaseModel):
|
| 16 |
+
email: EmailStr
|
| 17 |
+
password: str
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class Token(BaseModel):
|
| 21 |
+
access_token: str
|
| 22 |
+
token_type: str
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class TokenData(BaseModel):
|
| 26 |
+
email: Optional[str] = None
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# --------------------------
|
| 30 |
+
# Thread Schemas
|
| 31 |
+
# --------------------------
|
| 32 |
+
|
| 33 |
+
class ThreadBase(BaseModel):
|
| 34 |
+
title: Optional[str] = "New Chat"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class ThreadCreate(ThreadBase):
|
| 38 |
+
pass
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class ThreadOut(ThreadBase):
|
| 42 |
+
id: int
|
| 43 |
+
created_at: datetime
|
| 44 |
+
|
| 45 |
+
class Config:
|
| 46 |
+
from_attributes = True # ✅ for Pydantic v2
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# --------------------------
|
| 50 |
+
# User Output with Threads
|
| 51 |
+
# --------------------------
|
| 52 |
+
|
| 53 |
+
class UserResponse(BaseModel):
|
| 54 |
+
id: int
|
| 55 |
+
name: str
|
| 56 |
+
email: EmailStr
|
| 57 |
+
created_at: datetime
|
| 58 |
+
threads: List[ThreadOut] = []
|
| 59 |
+
|
| 60 |
+
class Config:
|
| 61 |
+
from_attributes = True
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# If needed separately (e.g., for /auth/me route):
|
| 65 |
+
class UserWithThreads(UserResponse):
|
| 66 |
+
pass
|