Skip to content

Commit

Permalink
[Feat] update market price (#43)
Browse files Browse the repository at this point in the history
## Description
- update market price
 only when a higher price is offered than the current on
- make bid price Pydantic model
<!-- Add a more detailed description of the changes if needed. -->

## Related Issue
close #36 
<!-- If your PR refers to a related issue, link it here. -->
  • Loading branch information
nahyun0121 authored Dec 3, 2023
2 parents 55bb971 + 6541761 commit cdc29bc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/api/routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from app.api.routes import messages
from app.api.routes import likes
from app.api.routes import reports
from app.api.routes import bid_prices

router = APIRouter()
router.include_router(users.router, tags=["users"], prefix="/user")
Expand All @@ -18,3 +19,4 @@
router.include_router(messages.router, tags=["messages"], prefix="/message")
router.include_router(likes.router, tags=["likes"], prefix="/like")
router.include_router(reports.router, tags=["reports"], prefix="/report")
router.include_router(bid_prices.router, tags=["bid_prices"], prefix="/bid")
32 changes: 32 additions & 0 deletions app/api/routes/bid_prices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.api.dependencies import database
from app.crud.crud_bid_prices import bid
from app.models.domain.bid_prices import BidPrice
from app.services.user_manager import current_active_user
from app.crud.crud_users import get_user_by_email
from app.db.fastapi_user import User

router = APIRouter()


@router.put("/{post_id}", description="경매 게시글의 경매 가격을 제시합니다.")
def bid_for_item(
*,
db: Session = Depends(database.get_db),
current_user: User = Depends(current_active_user),
post_id: int,
bid_price: BidPrice,
) -> Any:
current_user_name = get_user_by_email(db=db, email=current_user.email).username

# market price update
market = bid(db=db, post_id=post_id, bid_price=bid_price.bid_price)
if market:
return {"message": f"{current_user_name} successfully placed a bid"}
else:
raise HTTPException(
status_code=400,
detail="Bid price must be higher than current price",
)
15 changes: 15 additions & 0 deletions app/crud/crud_bid_prices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from sqlalchemy.orm import Session
from app.models.schemas.markets import Market


def bid(db: Session, *, post_id: int, bid_price: int) -> Market:
market = db.query(Market).filter(Market.post_id == post_id).one()

if bid_price > market.price:
market.price = bid_price
db.add(market)
db.commit()
db.refresh(market)
return market

return None
5 changes: 5 additions & 0 deletions app/models/domain/bid_prices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pydantic import BaseModel


class BidPrice(BaseModel):
bid_price: int

0 comments on commit cdc29bc

Please sign in to comment.