forked from github/codespaces-blank
-
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.
Merge pull request github#1 from sun007021/register
feat: user register
- Loading branch information
Showing
7 changed files
with
83 additions
and
6 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,15 @@ | ||
from passlib.context import CryptContext | ||
from sqlalchemy.orm import Session | ||
from domain.user.user_schema import UserCreate | ||
from models import User | ||
|
||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | ||
|
||
def create_user(db: Session, user_create: UserCreate): | ||
db_user = User(username=user_create.username, | ||
password=pwd_context.hash(user_create.password1), | ||
email=user_create.email, | ||
university_id=user_create.university_id | ||
) | ||
db.add(db_user) | ||
db.commit() |
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,16 @@ | ||
from fastapi import APIRouter | ||
from fastapi import Depends | ||
from sqlalchemy.orm import Session | ||
from starlette import status | ||
|
||
from database import get_db | ||
from domain.user import user_crud, user_schema | ||
|
||
router = APIRouter( | ||
prefix="/user", | ||
) | ||
|
||
|
||
@router.post("/", status_code=status.HTTP_204_NO_CONTENT) | ||
def user_create(_user_create: user_schema.UserCreate, db: Session = Depends(get_db)): | ||
user_crud.create_user(db=db, user_create=_user_create) |
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,21 @@ | ||
from pydantic import BaseModel, field_validator, EmailStr | ||
from pydantic_core.core_schema import FieldValidationInfo | ||
|
||
class UserCreate(BaseModel): | ||
username: str | ||
password1: str | ||
password2: str | ||
email: EmailStr | ||
university_id: int | None = None | ||
|
||
@field_validator('username', 'password1', 'password2', 'email') | ||
def not_empty(cls, v): | ||
if not v or not v.strip(): | ||
raise ValueError('빈 값은 허용되지 않습니다.') | ||
return v | ||
|
||
@field_validator('password2') | ||
def passwords_match(cls, v, info: FieldValidationInfo): | ||
if 'password1' in info.data and v != info.data['password1']: | ||
raise ValueError('비밀번호가 일치하지 않습니다') | ||
return v |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from fastapi import FastAPI | ||
|
||
from domain.user import user_router | ||
|
||
app = FastAPI() | ||
app = FastAPI() | ||
|
||
app.include_router(user_router.router) |
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 |
---|---|---|
@@ -1,4 +1,26 @@ | ||
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey | ||
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, ForeignKey | ||
from sqlalchemy.orm import relationship | ||
|
||
from database import Base | ||
from database import Base | ||
|
||
|
||
class User(Base): | ||
__tablename__ = "users" | ||
|
||
id = Column(Integer, primary_key=True, index=True) | ||
username = Column(String, unique=True, nullable=False, index=True) | ||
email = Column(String, unique=True, index=True) | ||
password = Column(String, nullable=False) | ||
image = Column(String, nullable=True) | ||
university_id = Column(Integer, ForeignKey("university.id"), nullable=True) | ||
is_active = Column(Boolean, default=True) | ||
is_superuser = Column(Boolean, default=False) | ||
|
||
university = relationship("University", backref="users") | ||
|
||
class University(Base): | ||
__tablename__ = "university" | ||
|
||
id = Column(Integer, primary_key=True, index=True) | ||
name = Column(String, unique=True, nullable=False, index=True) | ||
address = Column(String, nullable=True) |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
fastapi | ||
uvicorn | ||
sqlalchemy | ||
alembic | ||
alembic | ||
pydantic[email] | ||
passlib[bcrypt] |