diff --git a/main.py b/main.py index f850a29..6ca7815 100644 --- a/main.py +++ b/main.py @@ -1 +1 @@ -from api.index import app \ No newline at end of file +from api.index import app diff --git a/requirements.txt b/requirements.txt index 1dec285..b265ffd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,14 @@ pytest==7.1.3 pytest-asyncio==0.20.3 uvicorn==0.20.0 pydantic~=1.10.4 + +jinja2 + +motor>=2.5.0 +configparser==6.0.0 +json5==0.9.14 +azure-identity==1.15.0 +msgraph-sdk==1.0.0 +msgraph-core==1.0.0 + + diff --git a/src/config.cfg b/src/config.cfg new file mode 100644 index 0000000..a933f0b --- /dev/null +++ b/src/config.cfg @@ -0,0 +1,4 @@ +[azure] +clientId = 0e8e54e4-c386-4a78-a465-5d9f7aaf376d +tenantId = a2f14507-fab7-4185-9f8d-bbb265ce04e5 +graphUserScopes = User.Read diff --git a/src/index.py b/src/index.py index 246f9fb..a484e47 100644 --- a/src/index.py +++ b/src/index.py @@ -1,20 +1,129 @@ -from fastapi import FastAPI - -from src.dtos.ISayHelloDto import ISayHelloDto +from fastapi import FastAPI, HTTPException, Depends, Request +from fastapi.responses import HTMLResponse +from fastapi.staticfiles import StaticFiles +from fastapi.templating import Jinja2Templates +from motor.motor_asyncio import AsyncIOMotorClient +from pydantic import BaseModel,HttpUrl +from typing import List, Dict, Any, Optional +from fastapi.middleware.cors import CORSMiddleware app = FastAPI() +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], # Or better: [ "https://*.github.dev" ] + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +# Serve static files (if needed) +app.mount("/static", StaticFiles(directory="static"), name="static") + +# Configure templates +templates = Jinja2Templates(directory="templates") + +# MongoDB Configuration +MONGODB_URI = "mongodb+srv://naveendevarapalli99:Naveen123@cluster0.jmg62pd.mongodb.net/" +DB_NAME = "ott" +COLLECTION_NAME = "catalogue" + +async def get_database(): + if not MONGODB_URI: + raise HTTPException(status_code=500, detail="MongoDB URI not configured") + client = AsyncIOMotorClient(MONGODB_URI) + try: + db = client[DB_NAME] + yield db + finally: + client.close() + +# Catalogue Item Model +class CatalogueItem(BaseModel): + id: str + title: str + genre: List[str] + year: str + thumbnail: str + description: str + url: Optional[str] = "" + +# Serve the HTML templates +@app.get("/", response_class=HTMLResponse) +async def home(request: Request): + return templates.TemplateResponse("index.html", {"request": request}) + +@app.get("/add", response_class=HTMLResponse) +async def add_page(request: Request): + return templates.TemplateResponse("add.html", {"request": request}) + +@app.get("/home", response_class=HTMLResponse) +async def home(request: Request,db=Depends(get_database)): + collection = db[COLLECTION_NAME] + cursor = collection.find({}) + documents = await cursor.to_list(length=None) + for doc in documents: + if "_id" in doc: + doc["_id"] = str(doc["_id"]) + return templates.TemplateResponse("home.html", {"request": request, "movies": documents[::-1]}) + +@app.get("/watch/{movie_id}", response_class=HTMLResponse) +async def watch_movie(movie_id: str,request: Request,db=Depends(get_database)): + collection = db[COLLECTION_NAME] + cursor = collection.find({}) + movies = await cursor.to_list(length=None) + for doc in movies: + if "_id" in doc: + doc["_id"] = str(doc["_id"]) + movie = next((m for m in movies if m["id"] == movie_id), None) + if not movie: + raise HTTPException(status_code=404, detail="Movie not found") + return templates.TemplateResponse("watch.html", {"request": request, "movie": movie}) + + +# API Endpoints +@app.get("/getall", response_model=List[Dict[str, Any]]) +async def get_all_items(db=Depends(get_database)): + collection = db[COLLECTION_NAME] + cursor = collection.find({}) + documents = await cursor.to_list(length=None) + for doc in documents: + if "_id" in doc: + doc["_id"] = str(doc["_id"]) + return documents[::-1] # Reverse the array before returning + -@app.get("/") -async def root(): - return {"message": "Hello World"} +@app.post("/add_item", response_model=Dict[str, str]) +async def add_item(item: CatalogueItem, db=Depends(get_database)): + try: + collection = db[COLLECTION_NAME] + result = await collection.insert_one(item.dict()) # Change model_dump() to dict() + return {"message": "Item added successfully", "id": str(result.inserted_id)} + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) -@app.get("/hello/{name}") -async def say_hello(name: str): - return {"message": f"Hello {name}"} +@app.delete("/delete/{item_id}", response_model=Dict[str, str]) +async def delete_item(item_id: str, db=Depends(get_database)): + collection = db[COLLECTION_NAME] + result = await collection.delete_one({"id": item_id}) + if result.deleted_count == 0: + raise HTTPException(status_code=404, detail="Item not found") + return {"message": "Item deleted successfully"} +@app.get("/get/{item_id}", response_model=Dict[str, Any]) +async def get_item(item_id: str, db=Depends(get_database)): + collection = db[COLLECTION_NAME] + item = await collection.find_one({"id": item_id}) + if not item: + raise HTTPException(status_code=404, detail="Item not found") + item["_id"] = str(item["_id"]) + return item -@app.post("/hello") -async def hello_message(dto: ISayHelloDto): - return {"message": f"Hello {dto.message}"} +@app.put("/update/{item_id}", response_model=Dict[str, str]) +async def update_item(item_id: str, updated_item: CatalogueItem, db=Depends(get_database)): + collection = db[COLLECTION_NAME] + result = await collection.update_one({"id": item_id}, {"$set": updated_item.dict()}) + if result.matched_count == 0: + raise HTTPException(status_code=404, detail="Item not found") + return {"message": "Item updated successfully"} diff --git a/src/static/app.css b/src/static/app.css new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/static/app.css @@ -0,0 +1 @@ + diff --git a/src/templates/add.html b/src/templates/add.html new file mode 100644 index 0000000..8b02eec --- /dev/null +++ b/src/templates/add.html @@ -0,0 +1,33 @@ + + + +
+ID | +Title | +Genre | +Year | +Thumbnail | +Description | +Actions | +
---|
{{ movie.title }}
+ +{{ movie.description[:80] }}{% if movie.description|length > 80 %}...{% endif %}
+ID | +Title | +Genre | +Year | +Thumbnail | +Description | +URL | +Actions | +
---|