diff --git a/barker/barker/routers/guest_book.py b/barker/barker/routers/guest_book.py index 226f539..9ead774 100644 --- a/barker/barker/routers/guest_book.py +++ b/barker/barker/routers/guest_book.py @@ -3,7 +3,7 @@ import uuid from datetime import date, datetime, timedelta from typing import Optional -import barker.schemas.master as schemas +import barker.schemas.guest_book as schemas from fastapi import APIRouter, Depends, HTTPException, Security, status from sqlalchemy.exc import SQLAlchemyError @@ -34,7 +34,7 @@ def save( data: schemas.GuestBookIn, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["guest-book"]), -): +) -> schemas.GuestBook: try: customer: Customer = db.query(Customer).filter(Customer.phone == data.phone).first() if customer is None: @@ -68,7 +68,7 @@ def update( data: schemas.GuestBookIn, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["guest-book"]), -): +) -> schemas.GuestBook: try: item: GuestBook = db.query(GuestBook).filter(GuestBook.id == id_).first() item.customer.name = data.name @@ -88,17 +88,17 @@ def update( raise -@router.delete("/{id_}") +@router.delete("/{id_}", response_model=schemas.GuestBookIn) def delete( id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["guest-book"]), -): +) -> schemas.GuestBookIn: try: item: GuestBook = db.query(GuestBook).filter(GuestBook.id == id_).first() db.delete(item) db.commit() - return guest_book_info(None) + return blank_guest_book_info() except SQLAlchemyError as e: db.rollback() raise HTTPException( @@ -110,20 +110,20 @@ def delete( raise -@router.get("") +@router.get("", response_model=schemas.GuestBookIn) def show_blank( db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["guest-book"]), -): - return guest_book_info(None) +) -> schemas.GuestBook: + return blank_guest_book_info() -@router.get("/list") +@router.get("/list", response_model=schemas.GuestBookList) def show_list( q: Optional[str] = None, db: Session = Depends(get_db), user: UserToken = Depends(get_user), -): +) -> schemas.GuestBookList: if q is None or q == "": q = date.today() else: @@ -149,38 +149,42 @@ def show_list( for i, item in enumerate(list_): guest_book.insert( 0, - { - "id": item.id, - "serial": i + 1, - "name": item.customer.name, - "phone": item.customer.phone, - "pax": item.pax, - "date": item.date.strftime("%d-%b-%Y %H:%M"), - "status": "" if item.status is None else item.status.status, - }, + schemas.GuestBookListItem( + id=item.id, + serial=i + 1, + name=item.customer.name, + phone=item.customer.phone, + pax=item.pax, + date=item.date, + status=None if item.status is None else item.status.status, + tableId=None if item.status is None else item.status.food_table.id, + voucherId=None if item.status is None else item.status.voucher_id, + tableName=None if item.status is None else item.status.food_table.name, + ), ) - return {"date": q.strftime("%d-%b-%Y"), "list": guest_book} + return schemas.GuestBookList(date=q, list=guest_book) -@router.get("/{id_}") +@router.get("/{id_}", response_model=schemas.GuestBook) def show_id( id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["guest-book"]), -): +) -> schemas.GuestBook: item: GuestBook = db.query(GuestBook).filter(GuestBook.id == id_).first() return guest_book_info(item) -def guest_book_info(item: Optional[GuestBook]): - if item is not None: - return { - "id": item.id, - "name": item.customer.name, - "phone": item.customer.phone, - "pax": item.pax, - "address": item.customer.address, - "date": item.date.strftime("%d-%b-%Y %H:%M"), - } - else: - return {"name": "", "phone": "", "pax": 0, "address": ""} +def guest_book_info(item: GuestBook) -> schemas.GuestBook: + return schemas.GuestBook( + id=item.id, + name=item.customer.name, + phone=item.customer.phone, + pax=item.pax, + address=item.customer.address, + date=item.date, + ) + + +def blank_guest_book_info() -> schemas.GuestBookIn: + return schemas.GuestBookIn(name="", phone="", pax=0, address="") diff --git a/barker/barker/schemas/guest_book.py b/barker/barker/schemas/guest_book.py index a92be00..9ea3a3d 100644 --- a/barker/barker/schemas/guest_book.py +++ b/barker/barker/schemas/guest_book.py @@ -1,12 +1,17 @@ import uuid -from pydantic import Field +from datetime import date, datetime +from typing import List, Optional + +from pydantic import BaseModel, Field, validator from . import to_camel -from .customer import CustomerIn -class GuestBookIn(CustomerIn): +class GuestBookIn(BaseModel): + name: str + phone: str + address: str pax: int = Field(ge=0) class Config: @@ -17,9 +22,58 @@ class GuestBookIn(CustomerIn): class GuestBook(GuestBookIn): id_: uuid.UUID - date: str + date_: datetime + + @validator("date_", pre=True) + def parse_date(cls, value): + if isinstance(value, datetime): + return value + return datetime.strptime(value, "%d-%b-%Y %H:%M") class Config: fields = {"id_": "id"} anystr_strip_whitespace = True alias_generator = to_camel + json_encoders = {datetime: lambda v: v.strftime("%d-%b-%Y %H:%M")} + + +class GuestBookListItem(BaseModel): + id_: uuid.UUID + serial: int + name: str + phone: str + pax: int + date_: datetime + status: Optional[str] + table_id: Optional[uuid.UUID] + voucher_id: Optional[uuid.UUID] + table_name: Optional[str] + + @validator("date_", pre=True) + def parse_date(cls, value): + if isinstance(value, datetime): + return value + return datetime.strptime(value, "%d-%b-%Y %H:%M") + + class Config: + fields = {"id_": "id"} + anystr_strip_whitespace = True + alias_generator = to_camel + json_encoders = {datetime: lambda v: v.strftime("%d-%b-%Y %H:%M"), date: lambda v: v.strftime("%d-%b-%Y")} + + +class GuestBookList(BaseModel): + date_: date + list: List[GuestBookListItem] + + @validator("date_", pre=True) + def parse_date(cls, value): + if isinstance(value, date): + return value + return datetime.strptime(value, "%d-%b-%Y").date() + + class Config: + fields = {"id_": "id"} + anystr_strip_whitespace = True + alias_generator = to_camel + json_encoders = {datetime: lambda v: v.strftime("%d-%b-%Y %H:%M"), date: lambda v: v.strftime("%d-%b-%Y")} diff --git a/barker/barker/schemas/master.py b/barker/barker/schemas/master.py index e654dde..316f1ab 100644 --- a/barker/barker/schemas/master.py +++ b/barker/barker/schemas/master.py @@ -9,7 +9,7 @@ from pydantic import BaseModel, Field, validator from . import to_camel from .customer import Customer, CustomerIn # noqa: F401 from .device import Device, DeviceIn, DeviceLink # noqa: F401 -from .guest_book import GuestBook, GuestBookIn # noqa: F401 +from .guest_book import GuestBook, GuestBookIn, GuestBookListItem # noqa: F401 from .menu_category import MenuCategory, MenuCategoryIn, MenuCategoryLink # noqa: F401 from .modifier import Modifier, ModifierIn, ModifierLink # noqa: F401 from .modifier_category import ( # noqa: F401 diff --git a/bookie/src/app/guest-book/guest-book-list/guest-book-list.component.css b/bookie/src/app/guest-book/guest-book-list/guest-book-list.component.css index 5050fb6..c2e5ab1 100644 --- a/bookie/src/app/guest-book/guest-book-list/guest-book-list.component.css +++ b/bookie/src/app/guest-book/guest-book-list/guest-book-list.component.css @@ -1,3 +1,15 @@ .full-width-table { width: 100%; } + +.running { + /* Red 900 */ + background-color: #b71c1c; + color: #ffffff; +} + +.printed { + /* Green 900 */ + background-color: #1b5e20; + color: #ffffff; +} diff --git a/bookie/src/app/guest-book/guest-book-list/guest-book-list.component.html b/bookie/src/app/guest-book/guest-book-list/guest-book-list.component.html index f23ea57..84a21ba 100644 --- a/bookie/src/app/guest-book/guest-book-list/guest-book-list.component.html +++ b/bookie/src/app/guest-book/guest-book-list/guest-book-list.component.html @@ -33,7 +33,7 @@ S. No - {{ row.serial }} + {{ row.serial }} {{ row.status }} @@ -64,9 +64,24 @@ Action - + @@ -77,7 +92,11 @@ - + diff --git a/bookie/src/app/guest-book/guest-book.ts b/bookie/src/app/guest-book/guest-book.ts index 2d8b35d..5c18ebb 100644 --- a/bookie/src/app/guest-book/guest-book.ts +++ b/bookie/src/app/guest-book/guest-book.ts @@ -6,6 +6,10 @@ export class GuestBook { pax: number; address: string; date: string; + status?: string; + tableId?: string; + voucherId?: string; + tableName?: string; public constructor(init?: Partial) { Object.assign(this, init);