Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] update market price #43

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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