-
Notifications
You must be signed in to change notification settings - Fork 2
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 #109 from kucc/exp
Exp: Merge exp to main
- Loading branch information
Showing
32 changed files
with
928 additions
and
299 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 was deleted.
Oops, something went wrong.
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,78 @@ | ||
from enum import Enum | ||
|
||
|
||
class ExtendEnum(Enum): | ||
@classmethod | ||
def is_valid_enum_value(cls, status) -> bool: | ||
return status in cls._value2member_map_ | ||
|
||
def __call__(self): | ||
return self.value | ||
|
||
def __repr__(self): | ||
return f"<{self.__class__.__name__}.{self.name}({self.value})>" | ||
|
||
class AdminStatus(ExtendEnum): | ||
''' | ||
# 사용 예시 | ||
# print(AdminStatus.ACTIVE) # 현재 활성화된 관리자입니다. | ||
# print(AdminStatus.ACTIVE()) # True | ||
# print(AdminStatus.INACTIVE()) # False | ||
''' | ||
ACTIVE = True | ||
INACTIVE = False | ||
|
||
def __str__(self): | ||
desc = "활성화된" if self.value else "비활성화된" | ||
return f"현재 {desc} 관리자입니다." | ||
|
||
|
||
class BookStatus(ExtendEnum): | ||
AVAILABLE = True | ||
INAVILABLE = False | ||
|
||
def __str__(self): | ||
desc = "이용 가능" if self.value else "이용 불가능한" | ||
return f"현재 {desc} 한 도서입니다." | ||
|
||
class BookRequestStatus(ExtendEnum): | ||
''' | ||
# 사용 예시 | ||
# print(BookRequestStatus.WAITING) # 해당 도서 구매 요청은 대기 중입니다. | ||
# print(BookRequestStatus.WAITING()) # 0 | ||
# print(BookRequestStatus.WAITING.__repr__()) # <BookRequestStatus.WAITING(0)> | ||
# print(BookRequestStatus.is_valid_enum_value(3)) # True | ||
''' | ||
WAITING = 0 | ||
PURCHASED = 1 | ||
CANCELED = 2 | ||
REJECTED = 3 | ||
|
||
def __str__(self): | ||
status_descriptions = { | ||
0: "해당 도서 구매 요청은 대기 중입니다.", | ||
1: "해당 도서 구매 요청은 구매 완료되었습니다.", | ||
2: "해당 도서 구매 요청은 신청자 취소되었습니다.", | ||
3: "해당 도서 구매 요청은 관리자 반려되었습니다." | ||
} | ||
return status_descriptions.get(self.value, "잘못된 도서 구매 요청 상태입니다.") | ||
|
||
|
||
class ReturnStatus(ExtendEnum): | ||
RETURNED = True | ||
NOT_RETURNED = False | ||
|
||
def __str__(self): | ||
desc = "반납 완료된" if self.value else "대출 중인" | ||
return f"현재 {desc} 도서입니다." | ||
|
||
|
||
class ExtendStatus(ExtendEnum): | ||
EXTENDED = True | ||
NOT_EXTENDED = False | ||
|
||
def __str__(self): | ||
desc = "연장된" if self.value else "연장되지 않은" | ||
return f"대출이 {desc} 상태입니다." | ||
|
||
|
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
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
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,133 @@ | ||
|
||
from datetime import datetime | ||
from math import ceil | ||
|
||
from fastapi import HTTPException, status | ||
from sqlalchemy import and_, func, select | ||
from sqlalchemy.exc import IntegrityError | ||
from sqlalchemy.orm import Session | ||
|
||
from domain.enums.status import BookRequestStatus | ||
from domain.schemas.bookrequest_schemas import ( | ||
DomainReqAdminPutBookRequest, | ||
DomainResAdminGetBookRequest, | ||
DomainResBookRequest, | ||
) | ||
from repositories.models import RequestedBook | ||
|
||
|
||
async def service_admin_read_bookrequest(db: Session, page: int, limit: int): | ||
total = db.execute(select(func.count()).select_from(RequestedBook) | ||
.where(RequestedBook.is_deleted==False)).scalar() | ||
if ceil(total/limit) <page: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="Page is out of range" | ||
) | ||
offset = (page-1)*limit | ||
stmt = (select(RequestedBook).where(RequestedBook.is_deleted==False).order_by(RequestedBook.updated_at.desc()) | ||
.limit(limit).offset(offset)) | ||
bookrequest = db.execute(stmt).scalars().all() | ||
if not bookrequest: | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
detail="Not found" | ||
) | ||
result = [DomainResBookRequest( | ||
user_id=book.user_id, | ||
request_id=book.id, | ||
book_title=book.book_title, | ||
publication_year=book.publication_year, | ||
request_link=book.request_link, | ||
reason=book.reason, | ||
processing_status=book.processing_status, | ||
request_date=book.request_date, | ||
reject_reason=book.reject_reason | ||
) for book in bookrequest] | ||
|
||
response = DomainResAdminGetBookRequest( | ||
data=result, | ||
total = total | ||
) | ||
return response | ||
|
||
|
||
async def service_admin_update_bookrequest(db:Session, request: DomainReqAdminPutBookRequest): | ||
stmt = select(RequestedBook).where(and_(RequestedBook.id==request.request_id, RequestedBook.is_deleted==False)) | ||
request_book = db.execute(stmt).scalar_one_or_none() | ||
if not request_book: | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
detail="BookRequest Not found" | ||
) | ||
|
||
try: | ||
request_book.processing_status = request.processing_status | ||
if request_book.processing_status == BookRequestStatus.REJECTED(): | ||
request_book.reject_reason = request.reject_reason | ||
else: | ||
request_book.reject_reason = None | ||
request_book.updated_at = datetime.now() | ||
|
||
db.flush() | ||
|
||
except IntegrityError as e: | ||
db.rollback() | ||
raise HTTPException( | ||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
detail=f"Integrity Error occurred during update the Review item.: {str(e)}", | ||
) from e | ||
|
||
except Exception as e: | ||
db.rollback() | ||
raise HTTPException( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
detail=f"Unexpected error occurred during update: {str(e)}", | ||
) from e | ||
|
||
else: | ||
db.commit() | ||
db.refresh(request_book) | ||
result = DomainResBookRequest( | ||
user_id=request_book.user_id, | ||
request_id=request_book.id, | ||
book_title=request_book.book_title, | ||
publication_year=request_book.publication_year, | ||
request_link=request_book.request_link, | ||
reason=request_book.reason, | ||
processing_status=request_book.processing_status, | ||
request_date=request_book.request_date, | ||
reject_reason=request_book.reject_reason | ||
) | ||
return result | ||
|
||
async def service_admin_delete_bookrequest(request_id: int, db: Session): | ||
stmt = select(RequestedBook).where(RequestedBook.id==request_id, RequestedBook.is_deleted==False) | ||
request_book = db.execute(stmt).scalar_one_or_none() | ||
if not request_book: | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
detail="BookRequest Not found" | ||
) | ||
try: | ||
request_book.is_deleted = True | ||
db.flush() | ||
|
||
except IntegrityError as e: | ||
db.rollback() | ||
raise HTTPException( | ||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
detail=f"Integrity Error occurred during update the Review item.: {str(e)}", | ||
) from e | ||
|
||
except Exception as e: | ||
db.rollback() | ||
raise HTTPException( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
detail=f"Unexpected error occurred during update: {str(e)}", | ||
) from e | ||
|
||
else: | ||
db.commit() | ||
db.refresh(request_book) | ||
return |
Oops, something went wrong.