-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
169 lines (129 loc) · 5.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from fastapi import FastAPI, Depends , HTTPException , status , Form
from sqlalchemy.orm import Session
from typing import List
import uvicorn
import models, schema # Ensure these imports are correct
from database import SessionLocal, engine, Base
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
# import logging
# logging.basicConfig(level=logging.ERROR)
app = FastAPI()
from fastapi.middleware.cors import CORSMiddleware # for separate frontend and backend server
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
#for static html files
app.mount("/static", StaticFiles(directory="static"), name="static")
models.Base.metadata.create_all(bind=engine) # type: ignore
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# HOME PAGE
@app.get("/", response_class=HTMLResponse)
async def show_home_page():
with open("static/index.html", "r") as file:
return HTMLResponse(content=file.read(), status_code=200)
# Feedback Form Route
@app.get("/feedback", response_class=HTMLResponse)
async def show_feedback_form():
with open("static/FBForm.html", "r") as file:
return HTMLResponse(content=file.read(), status_code=200)
# feedback form submission route
@app.post("/feedform", response_model=schema.FeedBase)
async def feedback(name: str = Form(...), phoneno: str = Form(...), location: str = Form(...), description: str = Form(...), db: Session = Depends(get_db)):
new_form = models.Feedback(name=name, phoneno=phoneno, location=location, description=description) # type: ignore
db.add(new_form)
db.commit()
db.refresh(new_form)
return new_form
# return HTMLResponse(f"<h3>Feedback Submitted!</h3><p>Name: {name}</p><p>Location: {location}</p><p>Description: {description}</p>")
# @app.post("/feedform", tags=['feedback'])
# async def feedback(
# name: str = Form(...),
# email: str = Form(...),
# location: str = Form(...),
# phoneno: str = Form(...),
# description: str = Form(...),
# db: Session = Depends(get_db)
# ):
# new_form = models.Feedback(name=name, email=email, location=location, phoneno=phoneno, description=description) # type: ignore
# db.add(new_form)
# db.commit()
# db.refresh(new_form)
# return new_form
@app.get("/feedbacks", response_class=HTMLResponse)
async def show_all_feedbacks(db: Session = Depends(get_db)):
feedbacks = db.query(models.Feedback).all()
html_content = "<h2>Feedback List</h2><ul>"
for feedback in feedbacks:
html_content += f"<li>{feedback.name}: {feedback.description} (Location: {feedback.location})</li>"
html_content += "</ul>"
return HTMLResponse(content=html_content)
# @app.get("/feedform/{id}", response_class=HTMLResponse, tags=['feedback'])
# async def all(id: int, db: Session = Depends(get_db)):
# try:
# feedback = db.query(models.Feedback).filter(models.Feedback.id == id).first() # type: ignore
# if not feedback:
# raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
# # Return an HTML page with the feedback details
# html_content = f"""
# <html>
# <head>
# <title>Feedback #{id}</title>
# </head>
# <body>
# <h1>Feedback Details</h1>
# <p><strong>Name:</strong> {feedback.name}</p>
# <p><strong>Email:</strong> {feedback.email}</p>
# <p><strong>Location:</strong> {feedback.location}</p>
# <p><strong>Phone Number:</strong> {feedback.phoneno}</p>
# <p><strong>Description:</strong> {feedback.description}</p>
# </body>
# </html>
# """
# return HTMLResponse(content=html_content)
# except Exception as e:
# logging.error(f"Error: {e}")
# raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,detail=str(e))
@app.get("/feedform/{id}", response_model=List[schema.ShowFeed])
async def all(id : int , db: Session = Depends(get_db)):
allfeed = db.query(models.Feedback).filter(models.Feedback.id == id).all() # type: ignore
if not allfeed:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
return allfeed
@app.post("/user",response_model=schema.user)
async def create(request: schema.user , db: Session = Depends(get_db)):
user = models.User(name = request.name , email = request.email , password = request.password) #type: ignore
db.add(user)
db.commit()
db.refresh(user)
return user
# Checks user presence
@app.get("/user/login")
async def login(email: str, password: str, db: Session = Depends(get_db)):
user = db.query(models.User).filter(models.User.email == email).first()
if not user:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
# Validate password (without hash password hai)
if user.password != password:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid password")
return {"message": "Login successful", "user_id": user.id, "name": user.name}
'''
@app.get("/user/{id}" , response_model=schema.ShowUser)
async def show(id:int , db: Session = Depends(get_db)):
user = db.query(models.User).filter(models.User.id == id).first() # type: ignore
if not user:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"User with the id {id} is not available")
return user
'''
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)