mirror of
https://github.com/Rainyy21/URL_shortener.git
synced 2026-10-10 23:40:14 -04:00
add database but need to test it
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
from pydantic import Secret
|
||||
import sqlalchemy.orm import Session
|
||||
from . import models
|
||||
|
||||
def create_url(db:Session, short_code:str , long_url:str):
|
||||
db_url = models.URL(
|
||||
short_code=short_code,
|
||||
long_url=long_url
|
||||
)
|
||||
db.add(db_url)
|
||||
db.commit()
|
||||
db.refresh(db_url)
|
||||
return db_url
|
||||
|
||||
def get_url(db:Session , short_code:str):
|
||||
return db.query(models.URL).filter(
|
||||
models.URL.short_code == short_code
|
||||
).first()
|
||||
@@ -0,0 +1,9 @@
|
||||
from sqlalchemy import create_engine, engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
|
||||
DATABASE_URL = "sqlite:///./urls.db"
|
||||
|
||||
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
@@ -0,0 +1,14 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy.sql.traversals import ColIdentityComparatorStrategy
|
||||
from .database import Base
|
||||
|
||||
|
||||
class URL(Base):
|
||||
__tablename__ = "urls"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
short_code = Column(String, unique=True, index=True)
|
||||
long_url = Column(String)
|
||||
create_at = Column(DateTime, default=datetime.utcnow)
|
||||
@@ -0,0 +1,9 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class URLCreate(BaseModel):
|
||||
long_url: str
|
||||
|
||||
|
||||
class URLResponse(BaseModel):
|
||||
short_url: str
|
||||
Reference in New Issue
Block a user