mirror of
https://github.com/Rainyy21/URL_shortener.git
synced 2026-10-10 23:40:14 -04:00
add database and test that it work
This commit is contained in:
+7
-11
@@ -1,18 +1,14 @@
|
||||
from pydantic import Secret
|
||||
import sqlalchemy.orm import Session
|
||||
from 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
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
def get_url(db: Session, short_code: str):
|
||||
return db.query(models.URL).filter(models.URL.short_code == short_code).first()
|
||||
|
||||
@@ -9,7 +9,8 @@ import random
|
||||
from db.database import SessionLocal, engine
|
||||
from db import models, crud, schemas
|
||||
|
||||
models.Base.metadata.create_call(bind=engine)
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@@ -41,4 +42,4 @@ def redirect_url(short_code: str, db: Session = Depends(get_db)):
|
||||
|
||||
if not db_url:
|
||||
raise HTTPException(status_code=404, detail="URL not found")
|
||||
return {"long_url": db_url.long_url}
|
||||
return RedirectResponse(url=db_url.long_url)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
sqlalchemy
|
||||
redis
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
INFO: Started server process [60747]
|
||||
INFO: Waiting for application startup.
|
||||
INFO: Application startup complete.
|
||||
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
Reference in New Issue
Block a user