-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
41 lines (33 loc) · 1.08 KB
/
app.py
File metadata and controls
41 lines (33 loc) · 1.08 KB
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
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from fastapi import FastAPI
import pymongo
app = FastAPI()
myclient = pymongo.MongoClient("mongodb+srv://ruby:Sneha%4008@test.1p5iv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
mydb = myclient["mydatabase"]
mycol = mydb["user"]
class User(BaseModel):
name: str
userid: str
password: str
@app.post('/register/{name}')
def registers(request: User, name: str):
res = {"response": "error"}
if not mycol.find_one({'name': name}):
request = jsonable_encoder(request)
mycol.insert_one(request)
res = {"response": "success"}
else:
res = {"response": "user already exists"}
return res
@app.post('/login/{name}/{password}')
def logins(name: str, password: str):
res = {"response": "error"}
if mycol.find_one({'name': name}):
if mycol.find_one({'password': password}):
res = {"response": "success"}
else:
res = {"response": "Incorrect userid"}
else:
res = {"response": "Incorrect name"}
return res