Lab-Semtle / Semtle-Web-Server

아치셈틀 웹 페이지 구축 프로젝트(서버)
1 stars 0 forks source link

[BE-TASK] 유저 중복 확인, 유효성 검사 #17

Open P-hongsi opened 4 months ago

P-hongsi commented 4 months ago

[ 24.05.21 주간 중간 보고 ]

1. 수행한 작업

[1] 특정 유저 정보 가져오는 api 추가

[2] 회원가입시 유저 중복 확인 기능 추가

코드


async def create_user(user_info: Optional[CreateUserInfo], db: AsyncSession = Depends(get_db)):
logger.info("----------신규 유저 생성----------")
if user_info and await user_dao.is_user(user_info.user_id, user_info.user_name, user_info.user_email, user_info.user_phone, db):
    logger.warning("이미 존재하는 유저입니다.")
    return ER.DUPLICATE_RECORD

await user_service.create_user(user_info, db)
return SU.CREATED
```javascript
async def is_user(user_id: str, user_name: str, user_email: str, user_phone: str, db: AsyncSession) -> bool:
    stmt = select(User).where(
        (User.user_id == user_id) |
        (User.user_name == user_name) |
        (User.user_email == user_email) |
        (User.user_phone == user_phone)
    )
    result = await db.execute(stmt)
    user_exists = result.scalars().first() is not None
    return user_exists

[3] 회원가입시 이름, 이메일, 전화번호, 비밀번호 유효성 검사 코드 dto에 추가

코드

@validator('user_email', 'user_name', 'user_phone', 'user_password')
def check_empty(cls, v):
if not v or v. isspace():
raise HTTPException(status_code=422, detail="필수 항목을 입력해주세요.")
return v
@validator ('user_phone')
def check_phone(cls, v):
phone = v
if '-' not in v or len (phone) != 13:
raise HTTPException(status_code=422, detail="올바른 형식의 번호를 입력해주세요. ")
return phone
@validator ('user_password')
def validate_password(cls, v):
if len(v) < 8:
raise HTTPException(status_code=422, detail="비밀번호는 8자리 이상 영문과 숫자를 포함하여 작성해 주세요. ")
if not any(char.isdigit() for char in v):
raise HTTPException(status_code=422, detail="비밀번호는 8자리 이상 영문과 숫자를 포함하여 작성해 주세요. ")
if not any(char.isalpha() for char in v): 
raise HTTPException (status_code=422, detail="비밀번호는 8자리 이상 영문과 숫자를 포함하여 작성해 주세요. ")
return v