Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
WSL0809 committed Mar 28, 2024
1 parent 554ebc5 commit fcb3637
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 5 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Build and Push Docker Image

on:
push:
branches:
- master

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build the Docker image
run: docker build . --file Dockerfile --tag wangsl0809/yuezi_server:${{ github.sha }}
- name: Login to DockerHub
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
- name: Push the Docker image to DockerHub
run: docker push wangsl0809/yuezi_server:${{ github.sha }}
Empty file added api/IMS/__init__.py
Empty file.
69 changes: 69 additions & 0 deletions api/IMS/add_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from datetime import datetime
from typing import Union

from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy.orm import Session

import model
from database import get_db

router = APIRouter()


class ProductCreate(BaseModel):
name: str
quantity: int
price: float
note: Union[str, None] = None


class ProductResponse(BaseModel):
id: int
name: str
quantity: int
price: float
note: Union[str, None] = None
created_at: datetime
updated_at: datetime

class Config:
orm_mode = True


class LogResponse(BaseModel):
id: int
product_id: int
user_id: int
operation: str
quantity_changed: int
timestamp: datetime

class Config:
orm_mode = True


def update_product_quantity(db, product_id, quantity):
db_product = db.query(model.Product).filter(model.Product.id == product_id).first()
db_product.quantity = quantity
db.commit()
db.refresh(db_product)
return db_product


def get_all_products(db):
return db.query(model.Product).all()


@router.post("/IMS/create_product", response_model=ProductResponse)
async def create_product(product: ProductCreate, db: Session = Depends(get_db)):
db_product = model.Product(name=product.name, quantity=product.quantity, price=product.price, note=product.note)
db.add(db_product)
db.commit()
db.refresh(db_product)
return db_product


@router.get("/IMS/get_all_products")
async def get_all_products(db: Session = Depends(get_db)):
return get_all_products(db)
10 changes: 6 additions & 4 deletions api/reserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from pydantic import BaseModel, Json
from pydantic import BaseModel
from starlette import status

import model
from api.utils import exception_handler
from auth import get_current_active_user
from auth_schema import User
Expand Down Expand Up @@ -76,15 +75,17 @@ def update_client_and_room(db, reserve_recv: ReserveRecv):
"booked": booked,
"client_id": client_id
}
)
)
db.commit() # 提交事务
except SQLAlchemyError as e:
db.rollback() # 发生错误时回滚事务
print("发生错误,事务回滚:", e)
raise e


@router.post("/reserve")
async def reserve_room(reserve_recv: ReserveRecv, current_user: User = Depends(get_current_active_user), db: Session = Depends(get_db)):
async def reserve_room(reserve_recv: ReserveRecv, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
print(dict(reserve_recv))
if current_user.role == "admin":
try:
Expand All @@ -99,6 +100,7 @@ async def reserve_room(reserve_recv: ReserveRecv, current_user: User = Depends(g
headers={"WWW-Authenticate": "Bearer"},
)


'''
the logic of reserve:
1. about room table: update room.status = booked, update room.client_id
Expand Down
30 changes: 30 additions & 0 deletions model/IMS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from sqlalchemy import DECIMAL, Column, ForeignKey, Integer, String, DateTime, func
from sqlalchemy.orm import relationship

from database import Base


class Product(Base):
__tablename__ = "product"

id = Column(Integer, primary_key=True)
name = Column(String(255), unique=True, index=True)
quantity = Column(Integer)
price = Column(DECIMAL(precision=10, scale=2))
note = Column(String(255))
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())


class Log(Base):
__tablename__ = "log"

id = Column(Integer, primary_key=True)
product_id = Column(Integer, ForeignKey("product.id"))
user_id = Column(Integer, ForeignKey("users.id"))
operation = Column(String(255))
quantity_changed = Column(Integer)
timestamp = Column(DateTime, default=func.now())

product = relationship("Product", backref="logs")
user = relationship("User", backref="logs")
1 change: 1 addition & 0 deletions model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .client import *
from .base import *
from .room import *
from .IMS import *
5 changes: 4 additions & 1 deletion model/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy import DECIMAL, Column, ForeignKey, Integer, String, Text, Date, Boolean
from sqlalchemy import Column, Integer, String, Boolean
from sqlalchemy.orm import relationship

from database import Base

Expand All @@ -13,3 +14,5 @@ class User(Base):
hashed_password = Column(String)
is_active = Column(Boolean, default=True)
double_check_password = Column(String(255), default="88888888")

log = relationship("Log", backref="users")

0 comments on commit fcb3637

Please sign in to comment.