-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jingooo5
committed
Nov 2, 2024
1 parent
35de707
commit 5d2808f
Showing
2 changed files
with
37 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |