diff --git a/fastapi/app/db/crud_room_score.py b/fastapi/app/db/crud_room_score.py index 80b9bac..03041de 100644 --- a/fastapi/app/db/crud_room_score.py +++ b/fastapi/app/db/crud_room_score.py @@ -12,8 +12,19 @@ from db.base import get_db # 방 생성 def create_room(data, db): query = text(f""" - insert into room_main(user_seq, cert_type, user_info, cert_code, cert_expired) - values({user_seq}, '{cert_type}', '{user_info}', '{cert_code}', now() + INTERVAL '5 minutes') + insert into room_main(room_type_seq, user_seq, room_title, room_intro, open_yn, room_pw, room_status, create_dt, update_dt, delete_yn) + values( + (select room_type_seq from room_type where room_type_name = '{str(data['room_type']).upper()}'), + {data['user_seq']}, + '{data['room_title']}', + '{data['room_intro']}', + '{data['open_yn']}', + '{data['room_pw']}', + (select reserve_code from reserve_code where reserve_code_group = 'ROOM_SCORE_STATUS' and reserve_code = '{data['room_status']}'), + now(), + now(), + 'N' + ) """) try: db.execute(query) @@ -24,6 +35,94 @@ def create_room(data, db): return False +# room_type 검증 +async def verify_room_type(room_type, db): + query = text(f""" + select + count(room_type_seq) + from room_type + where room_type_name = '{str(room_type).upper()}' + """) + return db.execute(query).fetchall() + + +# room_status 검증 +async def verify_room_status(room_status, db): + query = text(f""" + select + count(reserve_code) + from reserve_code + where reserve_code_group = 'ROOM_SCORE_STATUS' + and reserve_code = '{room_status}' + """) + return db.execute(query).fetchall() + + +# room_seq 조회 +async def select_room_seq_by_data(data, db): + query = text(f""" + select + room_seq + from room_main + where room_type_seq = (select room_type_seq from room_type where room_type_name = '{str(data['room_type']).upper()}') + and user_seq = {data['user_seq']} + and room_title = '{data['room_title']}' + and room_intro = '{data['room_intro']}' + order by room_seq desc + """) + return db.execute(query).fetchall() + + +# 점수 공개 범위 설정 데이터 입력 +async def insert_score_open_range(data, db): + query = text(f""" + insert into room_score_{data['room_type']}(room_seq, number_of_people, score_open_range) + values( + {data['room_seq']}, + {data['number_of_people']}, + (select reserve_code from reserve_code where reserve_code_group = 'ROOM_SCORE_{str(data['room_type']).upper()}' and reserve_code = '{data['score_open_range']}') + ) + """) + try: + db.execute(query) + db.commit() + return True + except Exception as e: + logger.error(f"sql error: {e}") + return False + + +# 방장 참여자 입력 +async def insert_creater_user(data, db): + query = text(f""" + insert into room_score_{data['room_type']}_participant(room_seq, participant_type, user_seq, nickname, score) + values( + {data['room_seq']}, + (select reserve_code from reserve_code where reserve_code_group = 'PARTICIPANT_TYPE' and reserve_code = 'ADMIN'), + {data['user_seq']}, + (select nickname from manage_user where user_seq = {data['user_seq']}), + 0 + ) + """) + try: + db.execute(query) + db.commit() + return True + except Exception as e: + logger.error(f"sql error: {e}") + return False + + +# 방 참여자 조회 권한 확인 +async def check_auth_user(user_seq, room_type, room_seq, db): + query = text(f""" + select + user_seq + from room_score_{room_type}_participant + where room_seq = {room_seq} + and user_seq = {user_seq} + """) + return db.execute(query).fetchall() diff --git a/fastapi/app/process/room/room_score.py b/fastapi/app/process/room/room_score.py index 6c36643..ef6c692 100644 --- a/fastapi/app/process/room/room_score.py +++ b/fastapi/app/process/room/room_score.py @@ -7,7 +7,57 @@ from process.logger import logger # 방 만들기 async def create_room(data, db): - db_result_ori = crud_room_score.create_room(data=data, db=db) + db_result = crud_room_score.create_room(data=data, db=db) + + if db_result: + return { + "result": "OK" + } + else: + return { + "result": "FAIL", + } + + +# 점수 공개 범위 데이터 입력 +async def insert_score_open_range(data, db): + # room_seq 조회 + select_room_seq_by_data = await crud_room_score.select_room_seq_by_data(data=data, db=db) + data['room_seq'] = select_room_seq_by_data[0][0] + # 점수 공개 범위 설정 데이터 입력 + db_result = await crud_room_score.insert_score_open_range(data=data, db=db) + + if db_result: + return { + "result": "OK" + } + else: + return { + "result": "FAIL", + } + + +# 방장 참여자 입력 +async def insert_creater_user(data, db): + # room_seq 조회 + select_room_seq_by_data = await crud_room_score.select_room_seq_by_data(data=data, db=db) + data['room_seq'] = select_room_seq_by_data[0][0] + # 방장 참여자 입력 + db_result = await crud_room_score.insert_creater_user(data=data, db=db) + + if db_result: + return { + "result": "OK" + } + else: + return { + "result": "FAIL", + } + + +# 방 참여자 조회 권한 확인 +async def check_auth_user(user_seq, room_type, room_seq, db): + db_result_ori = crud_room_score.check_auth_user(user_seq=user_seq, room_type=room_type, room_seq=room_seq, db=db) db_result = [] for db_data in db_result_ori: db_result.append(db_data) @@ -19,8 +69,24 @@ async def create_room(data, db): } else: return { - "result": "OK", - "user_seq": db_result[0] + "result": "OK" + } + + +# 참가자 정보 가져오기 +async def participant_user_list(user_seq_list, room_type, room_seq, db): + db_result_ori = crud_room_score.participant_user_list(user_seq_list=user_seq_list, room_type=room_type, room_seq=room_seq, db=db) + db_result = [] + for db_data in db_result_ori: + db_result.append(db_data) + + if len(db_result) == 0: + return { + "result": "FAIL" + } + else: + return { + "result": "OK" } @@ -28,4 +94,3 @@ async def create_room(data, db): - diff --git a/fastapi/app/process/room/room_score_pattern.py b/fastapi/app/process/room/room_score_pattern.py new file mode 100644 index 0000000..1caf456 --- /dev/null +++ b/fastapi/app/process/room/room_score_pattern.py @@ -0,0 +1,52 @@ +from fastapi import APIRouter, Depends, HTTPException, Header, Body, status, Request +from db import crud_room_score +from sqlalchemy.orm import Session +from db import models, schemas +from process.logger import logger +import re + + +# 회원가입 입력항목 확인 +async def verify_create_room_data(data, db): + # room_type 체크 + verify_room_type_result = await crud_room_score.verify_room_type(room_type=data['room_type'], db=db) + if verify_room_type_result[0][0] != 1: + return 'room_type이 정확하지 않습니다.' + + # room_title 체크 + if not (0 < len(data['room_title']) and len(data['room_title']) < 100): + return '방 제목을 확인해주세요.' + + # room_intro 체크 + if not (0 < len(data['room_intro']) and len(data['room_intro']) < 1000): + return '방 소개를 확인해주세요.' + + # open_yn 체크 + if not (data['open_yn'] == 'Y' or data['open_yn'] == 'N'): + return '방 공개 여부를 확인해주세요.' + + # room_pw 체크 + if not (data['open_yn'] == 'Y' and data['room_pw'] == ''): + return '공개 방입니다. 방 비밀번호를 확인해주세요.' + + if data['open_yn'] == 'N' and not (0 < len(data['room_pw']) and len(data['room_pw']) < 100): + return '방 비밀번호를 확인해주세요.' + + # room_status 체크 + verify_room_status_result = await crud_room_score.verify_room_status(room_status=data['room_status'], db=db) + if verify_room_status_result[0][0] != 1: + return 'room_status가 정확하지 않습니다.' + + # 이상 없으면 NONE리턴 + return None + + + + + + + + + + + diff --git a/fastapi/app/process/user/manage_user_pattern.py b/fastapi/app/process/user/manage_user_pattern.py index c9b8f40..55f9f6c 100644 --- a/fastapi/app/process/user/manage_user_pattern.py +++ b/fastapi/app/process/user/manage_user_pattern.py @@ -113,4 +113,28 @@ async def is_valid_user_id_by_user_id(user_id, db): # profile_img 파일명이 /user/temp_dir/profile_img/IMG 로 시작하는지 검증 def starts_with_user_dir(value: str) -> bool: prefix = '/user/temp_dir/profile_img/IMG' - return value.startswith(prefix) \ No newline at end of file + return value.startswith(prefix) + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/fastapi/app/router/room_score_api.py b/fastapi/app/router/room_score_api.py index 0111151..42eb27b 100644 --- a/fastapi/app/router/room_score_api.py +++ b/fastapi/app/router/room_score_api.py @@ -23,6 +23,7 @@ from process.logger import logger from process.certification import cert_process from process.response import response from process.room import room_score +from process.room import room_score_pattern KST = datetime.timezone(datetime.timedelta(hours=9)) @@ -41,7 +42,7 @@ async def get_body(request: Request): # 방 생성하기 #================================================================================================== @router.post("/create/room") -async def login(request: Request, body: bytes = Depends(get_body), db: Session = Depends(get_db)): +async def create_room(request: Request, body: bytes = Depends(get_body), db: Session = Depends(get_db)): try: # 인증서 갱신 auth_token = cert_process.renew_cert(request=request) @@ -61,19 +62,131 @@ async def login(request: Request, body: bytes = Depends(get_body), db: Session = if user_seq_result["result"] == 'OK': user_seq = user_seq_result['data']['user_seq'] body['user_seq'] = user_seq + # 데이터 검증 + verify_create_room_data_result = await room_score_pattern.verify_create_room_data(data=body, db=db) + if verify_create_room_data_result is not None: + return await response.fail_res(auth_token=auth_token, auth_type='NOMAL', msg_title='방 생성 실패', msg_content=verify_create_room_data_result, data={}) # 방 생성 - create_room_result = await room_score.create_room(data=body) + create_room_result = await room_score.create_room(data=body, db=db) if create_room_result['result'] == 'OK': - return await response.ok_res(auth_token=auth_token, data={}, db=db) - else: - return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='방생성 에러', msg_content='방 생성중 에러가 발생했습니다. 관리자에게 문의해주세요.', data={}) + # 점수 공개 범위 설정 입력 + insert_score_open_range_result = await room_score.insert_score_open_range(data=body, db=db) + if insert_score_open_range_result['result'] == 'OK': + # 방장 참여자 입력 + insert_creater_user_result = await room_score.insert_creater_user(data=body, db=db) + if insert_creater_user_result['result'] == 'OK': + return await response.ok_res(auth_token=auth_token, data={}, db=db) + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='방생성 에러', msg_content='방 생성중 에러가 발생했습니다. 관리자에게 문의해주세요.', data={}) else: - logger.error(f"request error. URL: /user/withdraw/user\nerror message: {e}") return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='토큰 에러', msg_content='토큰 정보가 정확하지 않습니다.', data={}) except Exception as e: - logger.error(f"request error. URL: /room/score/ \nerror message: {e}") - return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='로그인 에러', msg_content='로그인 처리중 에러가 발생했습니다.', data={}) + logger.error(f"request error. URL: /room/score/create/room\nerror message: {e}") + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='방 생성 에러', msg_content='방 생성 처리중 에러가 발생했습니다.', data={}) + + +#================================================================================================== +# 대기중인 방-개인 참가자 정보 +#================================================================================================== +@router.post("/waiting/room/private/info") +async def get_waiting_private_room_info(request: Request, body: bytes = Depends(get_body), db: Session = Depends(get_db)): + try: + # 인증서 갱신 + auth_token = cert_process.renew_cert(request=request) + if auth_token['result'] == 'OK': + auth_token = auth_token['data'] + elif auth_token['result'] == 'TOKEN_EXPIRED': + raise token_expired_return_process(auth_token['msg']) + else: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='토큰 에러', msg_content='토큰 정보가 정확하지 않습니다.', data={}) + # body에서 ID 추출 + try: + body = json.loads(body) + except json.JSONDecodeError as e: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='데이터 에러', msg_content='데이터 처리 장애가 발생했습니다. 요청정보를 정확히 입력했는지 확인해주세요.', data={}) + + user_seq_result = cert_process.get_user_seq_by_token(token=auth_token['token']) + if user_seq_result["result"] == 'OK': + user_seq = user_seq_result['data']['user_seq'] + # 방 참여자 조회 권한 확인 + check_auth_user_result = room_score.check_auth_user(user_seq=user_seq, room_type='private', room_seq=body['room_seq'], db=db) + if check_auth_user_result != 'OK': + return await response.fail_res(auth_token=auth_token, auth_type='NOMAL', msg_title='참가자 조회 실패', msg_content='참가자 조회 권한이 없습니다.', data={}) + # 참가자 정보 가져오기 + participant_user_list_result = room_score.participant_user_list(user_seq_list=body['user_seq_list'], room_type='private', room_seq=body['room_seq'], db=db) + else: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='토큰 에러', msg_content='토큰 정보가 정확하지 않습니다.', data={}) + + except Exception as e: + logger.error(f"request error. URL: /room/score/waiting/room/private/info\nerror message: {e}") + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='(대기중인 방-개인 참가자 정보) 에러', msg_content='(대기중인 방-개인 참가자 정보) 처리중 에러가 발생했습니다.', data={}) + + +#================================================================================================== +# 대기중인 방-개인 게임 시작하기 +#================================================================================================== +@router.post("/start/private") +async def start_private_room(request: Request, body: bytes = Depends(get_body), db: Session = Depends(get_db)): + try: + # 인증서 갱신 + auth_token = cert_process.renew_cert(request=request) + if auth_token['result'] == 'OK': + auth_token = auth_token['data'] + elif auth_token['result'] == 'TOKEN_EXPIRED': + raise token_expired_return_process(auth_token['msg']) + else: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='토큰 에러', msg_content='토큰 정보가 정확하지 않습니다.', data={}) + # body에서 ID 추출 + try: + body = json.loads(body) + except json.JSONDecodeError as e: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='데이터 에러', msg_content='데이터 처리 장애가 발생했습니다. 요청정보를 정확히 입력했는지 확인해주세요.', data={}) + + user_seq_result = cert_process.get_user_seq_by_token(token=auth_token['token']) + if user_seq_result["result"] == 'OK': + user_seq = user_seq_result['data']['user_seq'] + body['user_seq'] = user_seq + # TODO + else: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='토큰 에러', msg_content='토큰 정보가 정확하지 않습니다.', data={}) + + except Exception as e: + logger.error(f"request error. URL: /room/score/start/private\nerror message: {e}") + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='(대기중인 방-개인 게임 시작하기) 에러', msg_content='(대기중인 방-개인 게임 시작하기) 처리중 에러가 발생했습니다.', data={}) + + +#================================================================================================== +# 대기중인 방-팀 참가자 정보 +#================================================================================================== +@router.post("/waiting/room/team/info") +async def get_waiting_team_room_info(request: Request, body: bytes = Depends(get_body), db: Session = Depends(get_db)): + try: + # 인증서 갱신 + auth_token = cert_process.renew_cert(request=request) + if auth_token['result'] == 'OK': + auth_token = auth_token['data'] + elif auth_token['result'] == 'TOKEN_EXPIRED': + raise token_expired_return_process(auth_token['msg']) + else: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='토큰 에러', msg_content='토큰 정보가 정확하지 않습니다.', data={}) + # body에서 ID 추출 + try: + body = json.loads(body) + except json.JSONDecodeError as e: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='데이터 에러', msg_content='데이터 처리 장애가 발생했습니다. 요청정보를 정확히 입력했는지 확인해주세요.', data={}) + + user_seq_result = cert_process.get_user_seq_by_token(token=auth_token['token']) + if user_seq_result["result"] == 'OK': + user_seq = user_seq_result['data']['user_seq'] + body['user_seq'] = user_seq + # TODO + else: + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='토큰 에러', msg_content='토큰 정보가 정확하지 않습니다.', data={}) + + except Exception as e: + logger.error(f"request error. URL: /room/score/waiting/room/team/info\nerror message: {e}") + return await response.error_res(auth_token=auth_token, auth_type='NOMAL', msg_title='(대기중인 방-팀 참가자 정보) 에러', msg_content='(대기중인 방-팀 참가자 정보) 처리중 에러가 발생했습니다.', data={}) +