Skip to content

Commit

Permalink
feat:add login service
Browse files Browse the repository at this point in the history
  • Loading branch information
jingooo5 committed Nov 2, 2024
1 parent 0cc5aa2 commit 35de707
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Data/user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from pydantic import BaseModel

class User(BaseModel):
user_id : int
class UserBase(BaseModel):
name : str
password : str

class UserRead(User):
class UserRead(UserBase):
user_id : str

class UserCreate(UserBase):
pass

class UserCreate(User):
class LoginUser(UserBase):
pass
26 changes: 26 additions & 0 deletions Router/item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import List

from fastapi.responses import JSONResponse

from Data.user import LoginUser, UserCreate
from Database.database import get_db
from fastapi import APIRouter
from starlette.status import *
Expand Down Expand Up @@ -53,3 +55,27 @@ def add_item(request : Request, user_id: str, item_id: str):
def add_items(request : Request, user_id: str):
return JSONResponse(status_code=HTTP_200_OK, content={"message": "Item added successfully"})


@router.post("/signup")
def signup(request: Request, userCreate: UserCreate):
user = User(
name=userCreate.name,
password=userCreate.password
)
with get_db() as db:
db.add(user)
db.commit()
db.refresh(user)

UserItemService.init_userItem(user.user_id)
return JSONResponse(status_code=HTTP_200_OK, content={"message": "User added successfully"})


@router.post("/signin")
def signin(request: Request, user: LoginUser):
with get_db() as db:
user = db.query(User).filter(User.name == user.name).first()
if user is None:
return JSONResponse(status_code=HTTP_204_NO_CONTENT, content={"message": "User not found"})

return JSONResponse(status_code=HTTP_200_OK, content={"message": "User signed in successfully", "user_id": user.user_id})

0 comments on commit 35de707

Please sign in to comment.