@@ -6,6 +6,7 @@ from db.repository.maintenancejob import ( | |||||
create_car_part, | create_car_part, | ||||
get_all_maintenance_jobs, | get_all_maintenance_jobs, | ||||
get_maintenance_job, | get_maintenance_job, | ||||
change_maintenance_status | |||||
) | ) | ||||
from schemas.maintenancejob import CreateMaintenanceJob | from schemas.maintenancejob import CreateMaintenanceJob | ||||
from schemas.carpart import CreateCarPart | from schemas.carpart import CreateCarPart | ||||
@@ -74,3 +75,21 @@ def get_maintenancejob( | |||||
maintenancejob = get_maintenance_job(maintenance_job_id, db) | maintenancejob = get_maintenance_job(maintenance_job_id, db) | ||||
return maintenancejob | 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 |
@@ -7,7 +7,7 @@ from apis.v1.route_auth import get_current_user | |||||
from db.models.user import User | from db.models.user import User | ||||
from schemas.user import UserCreate, ShowUser | from schemas.user import UserCreate, ShowUser | ||||
from db.session import get_db | 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() | 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) | users = list_users(db=db, role=role) | ||||
return users | 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) | @router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK) | ||||
def get_user_me( | def get_user_me( | ||||
@@ -90,6 +90,8 @@ async def assign_driver( | |||||
@router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK) | @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK) | ||||
async def get_all_vehicles(db: Session = Depends(get_db)): | async def get_all_vehicles(db: Session = Depends(get_db)): | ||||
vehicles = list_vehicles(db=db) | vehicles = list_vehicles(db=db) | ||||
if vehicles == []: | |||||
raise HTTPException(status_code=404, detail="No vehicles found") | |||||
return vehicles | return vehicles | ||||
@@ -151,11 +153,11 @@ def update_vehicle_location( | |||||
): | ): | ||||
print(current_user) | print(current_user) | ||||
print(current_user.Name) | print(current_user.Name) | ||||
if current_user.Role != "Driver": | |||||
if current_user.Role != "Driver" and current_user.Role != "Admin": | |||||
raise HTTPException( | raise HTTPException( | ||||
status_code=403, detail="You are not authorized to perform this action" | 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( | raise HTTPException( | ||||
status_code=403, detail="You are not the correct car driver" | status_code=403, detail="You are not the correct car driver" | ||||
) | ) | ||||
@@ -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) | |||||
@@ -1,10 +1,12 @@ | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, ARRAY | from sqlalchemy import Column, Integer, String, ForeignKey, ARRAY | ||||
from db.base import Base | from db.base import Base | ||||
from sqlalchemy.orm import relationship | |||||
class DriveTask(Base): | class DriveTask(Base): | ||||
Id = Column(Integer, primary_key=True, index=True) | Id = Column(Integer, primary_key=True, index=True) | ||||
DriverId = Column(ForeignKey("user.Id"), nullable=False) | DriverId = Column(ForeignKey("user.Id"), nullable=False) | ||||
CreatedBy = relationship("User", back_populates="driveTasks") | |||||
Description = Column(String, nullable=True) | Description = Column(String, nullable=True) | ||||
Status = Column(String, nullable=False) | Status = Column(String, nullable=False) | ||||
StartLocation = Column(ARRAY(String), nullable=False) | StartLocation = Column(ARRAY(String), nullable=False) | ||||
@@ -7,6 +7,7 @@ class MaintenanceJob(Base): | |||||
Id = Column(Integer, primary_key=True, index=True) | Id = Column(Integer, primary_key=True, index=True) | ||||
# a list of weak entities of class CarPart | # a list of weak entities of class CarPart | ||||
CarParts = relationship("CarPart", back_populates="parent") | CarParts = relationship("CarPart", back_populates="parent") | ||||
CreatedBy = relationship("MaintenancePerson", back_populates="maintenanceJobs") | |||||
Description = Column(String, nullable=False) | Description = Column(String, nullable=False) | ||||
Date = Column(DateTime, nullable=False) | Date = Column(DateTime, nullable=False) | ||||
MaintenanceWorker = Column(ForeignKey("user.Id"), nullable=False) | MaintenanceWorker = Column(ForeignKey("user.Id"), nullable=False) |
@@ -14,7 +14,18 @@ class User(Base): | |||||
ContactNumber = Column(String, nullable=False) | ContactNumber = Column(String, nullable=False) | ||||
Email = Column(String, nullable=False) | Email = Column(String, nullable=False) | ||||
Role = 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) | DrivingLicenseNumber = Column(String, nullable=True) | ||||
AssignedVehicle = Column(Integer, ForeignKey("vehicle.Id"), nullable=True) | AssignedVehicle = Column(Integer, ForeignKey("vehicle.Id"), nullable=True) | ||||
DriveTasks = relationship("DriveTask", back_populates="CreatedBy") | |||||
vehicle = relationship("Vehicle", back_populates="driver") | 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") | |||||
@@ -61,3 +61,14 @@ def get_maintenance_job(maintenancejob_id: int, db: Session): | |||||
result = maintenancejob.__dict__ | result = maintenancejob.__dict__ | ||||
result["CarParts"] = [part.__dict__ for part in maintenancejob.CarParts] | result["CarParts"] = [part.__dict__ for part in maintenancejob.CarParts] | ||||
return result | 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 |
@@ -58,3 +58,20 @@ def get_car_driver(vehicle_id: int, db: Session): | |||||
def list_users(db: Session, role: str = "Any"): | def list_users(db: Session, role: str = "Any"): | ||||
users = db.query(User).filter((User.Role == role) | (role == "Any")).all() | users = db.query(User).filter((User.Role == role) | (role == "Any")).all() | ||||
return users | 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 |
@@ -10,8 +10,7 @@ def create_new_vehicle(vehicle: CreateVehicle, db: Session): | |||||
**vehicle.model_dump(), | **vehicle.model_dump(), | ||||
Fuel=0, | Fuel=0, | ||||
Status="Inactive", | Status="Inactive", | ||||
CurrentLocation=[], | |||||
MaintenanceNotes=[] | |||||
CurrentLocation=[] | |||||
) | ) | ||||
db.add(vehicle_object) | db.add(vehicle_object) | ||||
db.commit() | db.commit() | ||||
@@ -62,6 +61,8 @@ def list_vehicles(db: Session): | |||||
def get_vehicle_by_id(vehicle_id: int, db: Session): | def get_vehicle_by_id(vehicle_id: int, db: Session): | ||||
vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first() | vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first() | ||||
if not vehicle: | |||||
return None | |||||
driver = get_car_driver(vehicle.Id, db) | driver = get_car_driver(vehicle.Id, db) | ||||
if driver: | if driver: | ||||
vehicle.AssignedDriver = driver.Id | vehicle.AssignedDriver = driver.Id | ||||
@@ -27,8 +27,3 @@ app = startup() | |||||
@app.get("/") | @app.get("/") | ||||
def root(): | def root(): | ||||
return {"message": "Hello World!"} | return {"message": "Hello World!"} | ||||
@app.get("/user") | |||||
def get_users(): | |||||
return {{"name": "almaz"}, {"name": "madi"}} |
@@ -31,3 +31,4 @@ class ShowUser(BaseModel): | |||||
class Config: | class Config: | ||||
orm_mode = True | orm_mode = True | ||||
validate_assignment = True | validate_assignment = True | ||||
@@ -8,6 +8,7 @@ class CreateVehicle(BaseModel): | |||||
LicensePlate: str | LicensePlate: str | ||||
Mileage: int | Mileage: int | ||||
Capacity: int | Capacity: int | ||||
Type: str | |||||
class OutputVehicle(BaseModel): | class OutputVehicle(BaseModel): | ||||