def view_orders( db: Session = Depends(db.db_connection), current_user: schemas.UserRequestType = Depends(oauth2.get_current_user), ): orders: List[m.Order] = db.query(m.Order).all() return orders
def view_customers( id: UUID, db: Session = Depends(db.db_connection), current_user: schemas.UserRequestType = Depends(oauth2.get_current_user), ): customer = db.query(m.Customer).filter(m.Customer.id == id).first() if not customer: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") return customer
def create_order( request: schemas.OrderRequestType, db: Session = Depends(db.db_connection), current_user: schemas.UserRequestType = Depends(oauth2.get_current_user), ): customer = db.query(m.Customer).first() if db.query(m.Order).filter_by(code=request.code).first(): raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Order already existed") else: new_order: m.Order = m.Order( code=request.code, customer_id=customer.id, ) db.add(new_order) db.commit() db.refresh(new_order) return new_order
def delete_order( id: UUID, db: Session = Depends(db.db_connection), current_user: schemas.UserRequestType = Depends(oauth2.get_current_user), ): order = db.query(m.Order).filter(m.Order.id == id) if not order.first(): raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Order not found") else: order.delete(synchronize_session=False) db.commit() return "Deleted"
def update_order( id: UUID, request: schemas.OrderRequestType, db: Session = Depends(db.db_connection), current_user: schemas.UserRequestType = Depends(oauth2.get_current_user), ): order = db.query(m.Order).filter(m.Order.id == id) if not order.first(): raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Order not found") else: order.update(dict(code=request.code)) db.commit() return "Updated"
def login( request: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(db.db_connection), ): user = db.query(m.User).filter(m.User.email == request.username).first() if not user: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Invalid credentials") if not Hash.verify(hashed_password=user.password, unhassed_password=request.password): raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Password doesn't match the account", ) access_token_expires = timedelta( minutes=jwt_token.ACCESS_TOKEN_EXPIRE_MINUTES) access_token = jwt_token.create_access_token( data={"sub": user.email}, expires_delta=access_token_expires) return {"access_token": access_token, "token_type": "bearer"}