From 5d2808f46952e9c277e6cddd060c396166a6a6f8 Mon Sep 17 00:00:00 2001 From: jingooo5 Date: Sat, 2 Nov 2024 22:57:24 +0900 Subject: [PATCH] feat:separate login view --- Router/item.py | 25 ------------------------- Router/login.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 Router/login.py diff --git a/Router/item.py b/Router/item.py index 5c22665..b766083 100644 --- a/Router/item.py +++ b/Router/item.py @@ -54,28 +54,3 @@ def add_item(request : Request, user_id: str, item_id: str): @router.post("/{user_id}/receipt") 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}) \ No newline at end of file diff --git a/Router/login.py b/Router/login.py new file mode 100644 index 0000000..98c68b6 --- /dev/null +++ b/Router/login.py @@ -0,0 +1,37 @@ +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 * +from fastapi import Query, Request +from Database.models import User +from Service.useritem_service import UserItemService + + +router = APIRouter(tags=["login"], prefix="/login") + + +@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}) \ No newline at end of file