浏览代码

things

main
Madiwka 1年前
父节点
当前提交
ad7ce19915
共有 13 个文件被更改,包括 100 次插入11 次删除
  1. +19
    -0
      app/apis/v1/route_maintenancejob.py
  2. +13
    -1
      app/apis/v1/route_user.py
  3. +4
    -2
      app/apis/v1/route_vehicle.py
  4. +16
    -0
      app/db/models/auction.py
  5. +2
    -0
      app/db/models/drivetask.py
  6. +1
    -0
      app/db/models/maintenancejob.py
  7. +12
    -1
      app/db/models/user.py
  8. +11
    -0
      app/db/repository/maintenancejob.py
  9. +17
    -0
      app/db/repository/user.py
  10. +3
    -2
      app/db/repository/vehicle.py
  11. +0
    -5
      app/main.py
  12. +1
    -0
      app/schemas/user.py
  13. +1
    -0
      app/schemas/vehicle.py

+ 19
- 0
app/apis/v1/route_maintenancejob.py 查看文件

@@ -6,6 +6,7 @@ from db.repository.maintenancejob import (
create_car_part,
get_all_maintenance_jobs,
get_maintenance_job,
change_maintenance_status
)
from schemas.maintenancejob import CreateMaintenanceJob
from schemas.carpart import CreateCarPart
@@ -74,3 +75,21 @@ def get_maintenancejob(

maintenancejob = get_maintenance_job(maintenance_job_id, db)
return maintenancejob

@router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
def change_status(
maintenance_job_id: int,
status: str,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
if current_user.Role != "Maintenance" and current_user.Role != "Admin":
raise HTTPException(
status_code=403, detail="You are not authorized to perform this action"
)
result = change_maintenance_status(maintenance_job_id, status, db)
if result is None:
raise HTTPException(
status_code=404, detail="Maintenance job not found"
)
return result

+ 13
- 1
app/apis/v1/route_user.py 查看文件

@@ -7,7 +7,7 @@ from apis.v1.route_auth import get_current_user
from db.models.user import User
from schemas.user import UserCreate, ShowUser
from db.session import get_db
from db.repository.user import create_new_user, list_users, get_user_by_id
from db.repository.user import create_new_user, list_users, get_user_by_id, replace_user_data


router = APIRouter()
@@ -35,6 +35,18 @@ def get_all_users(db: Session = Depends(get_db), role: str = None):
users = list_users(db=db, role=role)
return users

@router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED)
def update_user(
user_id: int,
user: UserCreate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
if current_user.Role != "Admin":
raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
user = replace_user_data(user_id=user_id, user=user, db=db)
return user


@router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK)
def get_user_me(


+ 4
- 2
app/apis/v1/route_vehicle.py 查看文件

@@ -90,6 +90,8 @@ async def assign_driver(
@router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK)
async def get_all_vehicles(db: Session = Depends(get_db)):
vehicles = list_vehicles(db=db)
if vehicles == []:
raise HTTPException(status_code=404, detail="No vehicles found")
return vehicles


@@ -151,11 +153,11 @@ def update_vehicle_location(
):
print(current_user)
print(current_user.Name)
if current_user.Role != "Driver":
if current_user.Role != "Driver" and current_user.Role != "Admin":
raise HTTPException(
status_code=403, detail="You are not authorized to perform this action"
)
if current_user.AssignedVehicle != vehicle_id:
if current_user.AssignedVehicle != vehicle_id and current_user.Role != "Admin":
raise HTTPException(
status_code=403, detail="You are not the correct car driver"
)


+ 16
- 0
app/db/models/auction.py 查看文件

@@ -0,0 +1,16 @@
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from db.base import Base


class Auction(Base):
Id = Column(Integer, primary_key=True, index=True)
initialPrice = Column(Integer, nullable=False)
minimalBet = Column(Integer, nullable=False)
carID = Column(Integer, ForeignKey("car.Id"), nullable=False)
vehicle = relationship("Vehicle", back_populates="auction")
CreatedBy = relationship("User", back_populates="auctions")
dateStart = Column(DateTime, nullable=False)
dateEnd = Column(DateTime, nullable=False)

+ 2
- 0
app/db/models/drivetask.py 查看文件

@@ -1,10 +1,12 @@
from sqlalchemy import Column, Integer, String, ForeignKey, ARRAY
from db.base import Base
from sqlalchemy.orm import relationship


class DriveTask(Base):
Id = Column(Integer, primary_key=True, index=True)
DriverId = Column(ForeignKey("user.Id"), nullable=False)
CreatedBy = relationship("User", back_populates="driveTasks")
Description = Column(String, nullable=True)
Status = Column(String, nullable=False)
StartLocation = Column(ARRAY(String), nullable=False)


+ 1
- 0
app/db/models/maintenancejob.py 查看文件

@@ -7,6 +7,7 @@ class MaintenanceJob(Base):
Id = Column(Integer, primary_key=True, index=True)
# a list of weak entities of class CarPart
CarParts = relationship("CarPart", back_populates="parent")
CreatedBy = relationship("MaintenancePerson", back_populates="maintenanceJobs")
Description = Column(String, nullable=False)
Date = Column(DateTime, nullable=False)
MaintenanceWorker = Column(ForeignKey("user.Id"), nullable=False)

+ 12
- 1
app/db/models/user.py 查看文件

@@ -14,7 +14,18 @@ class User(Base):
ContactNumber = Column(String, nullable=False)
Email = Column(String, nullable=False)
Role = Column(String, nullable=False)
HashedPassword = Column(String, nullable=False)
#Admin-specific relationships
CreatedAuctions = relationship("Auction", back_populates="CreatedBy")
#Driver-specific relationships
DrivingLicenseNumber = Column(String, nullable=True)
AssignedVehicle = Column(Integer, ForeignKey("vehicle.Id"), nullable=True)
DriveTasks = relationship("DriveTask", back_populates="CreatedBy")
vehicle = relationship("Vehicle", back_populates="driver")
HashedPassword = Column(String, nullable=False)
#MaintenancePerson-specific relationships
maintenanceJobs = relationship("MaintenanceJob", back_populates="CreatedBy")
#FuelingPerson-specific relationships
fuelingJobs = relationship("FuelingJob", back_populates="CreatedBy")


+ 11
- 0
app/db/repository/maintenancejob.py 查看文件

@@ -61,3 +61,14 @@ def get_maintenance_job(maintenancejob_id: int, db: Session):
result = maintenancejob.__dict__
result["CarParts"] = [part.__dict__ for part in maintenancejob.CarParts]
return result

def change_maintenance_status(maintenancejob_id: int, status: str, db: Session):
maintenancejob = (
db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
)
if maintenancejob is None:
return None
maintenancejob.Status = status
db.commit()
db.refresh(maintenancejob)
return maintenancejob

+ 17
- 0
app/db/repository/user.py 查看文件

@@ -58,3 +58,20 @@ def get_car_driver(vehicle_id: int, db: Session):
def list_users(db: Session, role: str = "Any"):
users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
return users

def replace_user_data(user_id: int, user_data: UserCreate, db: Session):
user = db.query(User).filter(User.Id == user_id).first()
if not user:
return "userNotFound"
user.Email = user_data.Email
user.Name = user_data.Name
user.MiddleName = user_data.MiddleName
user.LastName = user_data.LastName
user.GovernmentId = user_data.GovernmentId
user.Address = user_data.Address
user.ContactNumber = user_data.ContactNumber
user.Role = user_data.Role
user.HashedPassword = Hasher.get_password_hash(user_data.Password)
db.commit()
db.refresh(user)
return user

+ 3
- 2
app/db/repository/vehicle.py 查看文件

@@ -10,8 +10,7 @@ def create_new_vehicle(vehicle: CreateVehicle, db: Session):
**vehicle.model_dump(),
Fuel=0,
Status="Inactive",
CurrentLocation=[],
MaintenanceNotes=[]
CurrentLocation=[]
)
db.add(vehicle_object)
db.commit()
@@ -62,6 +61,8 @@ def list_vehicles(db: Session):

def get_vehicle_by_id(vehicle_id: int, db: Session):
vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
if not vehicle:
return None
driver = get_car_driver(vehicle.Id, db)
if driver:
vehicle.AssignedDriver = driver.Id


+ 0
- 5
app/main.py 查看文件

@@ -27,8 +27,3 @@ app = startup()
@app.get("/")
def root():
return {"message": "Hello World!"}


@app.get("/user")
def get_users():
return {{"name": "almaz"}, {"name": "madi"}}

+ 1
- 0
app/schemas/user.py 查看文件

@@ -31,3 +31,4 @@ class ShowUser(BaseModel):
class Config:
orm_mode = True
validate_assignment = True


+ 1
- 0
app/schemas/vehicle.py 查看文件

@@ -8,6 +8,7 @@ class CreateVehicle(BaseModel):
LicensePlate: str
Mileage: int
Capacity: int
Type: str


class OutputVehicle(BaseModel):


正在加载...
取消
保存