Skip to content

Commit

Permalink
Merge pull request github#1 from sun007021/register
Browse files Browse the repository at this point in the history
feat: user register
  • Loading branch information
sun007021 authored Mar 2, 2024
2 parents 088a406 + b1afac2 commit 7e6775e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
2 changes: 0 additions & 2 deletions chek/database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contextlib

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
Expand All @@ -14,7 +13,6 @@
Base = declarative_base()


@contextlib.contextmanager
def get_db():
db = SessionLocal()
try:
Expand Down
15 changes: 15 additions & 0 deletions chek/domain/user/user_crud.py
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()
16 changes: 16 additions & 0 deletions chek/domain/user/user_router.py
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)
21 changes: 21 additions & 0 deletions chek/domain/user/user_schema.py
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
5 changes: 4 additions & 1 deletion chek/main.py
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)
26 changes: 24 additions & 2 deletions chek/models.py
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)
4 changes: 3 additions & 1 deletion requests.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
fastapi
uvicorn
sqlalchemy
alembic
alembic
pydantic[email]
passlib[bcrypt]

0 comments on commit 7e6775e

Please sign in to comment.