@@ -1,4 +1,4 @@ | |||||
from fastapi import Depends, APIRouter, HTTPException, status | |||||
from fastapi import Depends, APIRouter, File, Form, HTTPException, UploadFile, status | |||||
from sqlalchemy.orm import Session | from sqlalchemy.orm import Session | ||||
from db.session import get_db | from db.session import get_db | ||||
from db.repository.maintenancejob import ( | from db.repository.maintenancejob import ( | ||||
@@ -10,7 +10,7 @@ from db.repository.maintenancejob import ( | |||||
) | ) | ||||
from typing import List | from typing import List | ||||
from schemas.maintenancejob import CreateMaintenanceJob, OutputMaintenanceJob | from schemas.maintenancejob import CreateMaintenanceJob, OutputMaintenanceJob | ||||
from schemas.carpart import CreateCarPart | |||||
from schemas.carpart import CreateCarPart, ShowCarPart | |||||
from db.models.user import User | from db.models.user import User | ||||
from apis.v1.route_auth import get_current_user | from apis.v1.route_auth import get_current_user | ||||
@@ -34,9 +34,9 @@ def create_maintenancejob( | |||||
return maintenancejob_res | return maintenancejob_res | ||||
@router.post("/carpart", status_code=status.HTTP_201_CREATED) | |||||
@router.post("/carpart", response_model = ShowCarPart ,status_code=status.HTTP_201_CREATED) | |||||
def create_carpart( | def create_carpart( | ||||
car_part: CreateCarPart, | |||||
car_part: CreateCarPart = Depends(), | |||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
): | ): | ||||
@@ -44,8 +44,9 @@ def create_carpart( | |||||
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" | ||||
) | ) | ||||
print("So it begins...") | |||||
car_part_res = create_car_part(car_part=car_part, db=db) | car_part_res = create_car_part(car_part=car_part, db=db) | ||||
print("So it ends...") | |||||
return car_part_res | return car_part_res | ||||
@@ -1,4 +1,4 @@ | |||||
from sqlalchemy import Column, Integer, String, ForeignKey | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, LargeBinary | |||||
from sqlalchemy.orm import relationship | from sqlalchemy.orm import relationship | ||||
from db.base import Base | from db.base import Base | ||||
@@ -11,5 +11,5 @@ class CarPart(Base): | |||||
Name = Column(String, nullable=False) | Name = Column(String, nullable=False) | ||||
Number = Column(String, nullable=False) | Number = Column(String, nullable=False) | ||||
Condition = Column(String, nullable=False) | Condition = Column(String, nullable=False) | ||||
ImageURL = Column(String, nullable=False) | |||||
ImageURL = Column(LargeBinary, nullable=False) | |||||
Cost = Column(Integer, nullable=False) | Cost = Column(Integer, nullable=False) |
@@ -1,4 +1,4 @@ | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, LargeBinary | |||||
from sqlalchemy.orm import relationship | from sqlalchemy.orm import relationship | ||||
from db.base import Base | from db.base import Base | ||||
@@ -13,8 +13,8 @@ class FuelingTask(Base): | |||||
Cost = Column(Integer, nullable=False) | Cost = Column(Integer, nullable=False) | ||||
FuelRefilled = Column(Integer, nullable=False) | FuelRefilled = Column(Integer, nullable=False) | ||||
GasStationName = Column(String, nullable=False) | GasStationName = Column(String, nullable=False) | ||||
ImageBefore = Column(String, nullable=False) | |||||
ImageAfter = Column(String, nullable=False) | |||||
ImageBefore = Column(LargeBinary, nullable=False) | |||||
ImageAfter = Column(LargeBinary, nullable=False) | |||||
CreatedBy = relationship( | CreatedBy = relationship( | ||||
"User", back_populates="fuelingTasks", foreign_keys="FuelingTask.CreatedById" | "User", back_populates="fuelingTasks", foreign_keys="FuelingTask.CreatedById" | ||||
) | ) |
@@ -8,6 +8,8 @@ class MaintenanceJob(Base): | |||||
# 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("User", back_populates="maintenanceJobs") | CreatedBy = relationship("User", back_populates="maintenanceJobs") | ||||
VehicleID = Column(ForeignKey("vehicle.Id"), nullable=False) | |||||
Vehicle = relationship("Vehicle", 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) |
@@ -23,4 +23,5 @@ class Vehicle(Base): | |||||
DriverHistory = Column(ARRAY(Integer), nullable=True) | DriverHistory = Column(ARRAY(Integer), nullable=True) | ||||
driver = relationship("User", back_populates="vehicle") | driver = relationship("User", back_populates="vehicle") | ||||
auction = relationship("Auction", back_populates="vehicle") | auction = relationship("Auction", back_populates="vehicle") | ||||
maintenanceJobs = relationship("MaintenanceJob", back_populates="Vehicle") | |||||
@@ -11,6 +11,7 @@ def create_new_maintenancejob( | |||||
maintenancejob_object = MaintenanceJob( | maintenancejob_object = MaintenanceJob( | ||||
MaintenanceWorker=maintenanceworker, | MaintenanceWorker=maintenanceworker, | ||||
Description=maintenancejob.Description, | Description=maintenancejob.Description, | ||||
VehicleID=maintenancejob.VehicleID, | |||||
Date=maintenancejob.Date, | Date=maintenancejob.Date, | ||||
) | ) | ||||
print("OBJECT CREATED") | print("OBJECT CREATED") | ||||
@@ -26,13 +27,15 @@ def create_car_part(car_part: CreateCarPart, db: Session): | |||||
Name=car_part.Name, | Name=car_part.Name, | ||||
Number=car_part.Number, | Number=car_part.Number, | ||||
Condition=car_part.Condition, | Condition=car_part.Condition, | ||||
ImageURL=car_part.ImageURL, | |||||
ImageURL=car_part.image.file.read(), | |||||
Cost=car_part.Cost, | Cost=car_part.Cost, | ||||
) | ) | ||||
print("OBJECT CREATED") | print("OBJECT CREATED") | ||||
db.add(car_part_object) | db.add(car_part_object) | ||||
db.commit() | db.commit() | ||||
print("OBJECT SAVED") | |||||
db.refresh(car_part_object) | db.refresh(car_part_object) | ||||
print("OBJECT REFRESHED") | |||||
return car_part_object | return car_part_object | ||||
@@ -51,6 +54,7 @@ def get_all_maintenance_jobs(db: Session): | |||||
job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts] | job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts] | ||||
job_dict["TotalCost"] = calculate_total_cost(job.CarParts) | job_dict["TotalCost"] = calculate_total_cost(job.CarParts) | ||||
job_dict["AssignedTo"] = job.CreatedBy.__dict__ | job_dict["AssignedTo"] = job.CreatedBy.__dict__ | ||||
job_dict["Vehicle"] = job.Vehicle.__dict__ | |||||
result.append(job_dict) | result.append(job_dict) | ||||
return maintenancejobs | return maintenancejobs | ||||
@@ -64,6 +68,7 @@ def get_maintenance_job(maintenancejob_id: int, db: Session): | |||||
maintenancejob.TotalCost = calculate_total_cost(maintenancejob.CarParts) | maintenancejob.TotalCost = calculate_total_cost(maintenancejob.CarParts) | ||||
# print(result.TotalCost) | # print(result.TotalCost) | ||||
maintenancejob.AssignedTo = maintenancejob.CreatedBy.__dict__ | maintenancejob.AssignedTo = maintenancejob.CreatedBy.__dict__ | ||||
maintenancejob.Vehicle = maintenancejob.Vehicle.__dict__ | |||||
print(maintenancejob.AssignedTo) | print(maintenancejob.AssignedTo) | ||||
print("DB Access complete") | print("DB Access complete") | ||||
return maintenancejob | return maintenancejob | ||||
@@ -1,13 +1,18 @@ | |||||
from fastapi import Form, UploadFile | |||||
from pydantic import BaseModel, Field | from pydantic import BaseModel, Field | ||||
from dataclasses import dataclass | |||||
class CreateCarPart(BaseModel): | class CreateCarPart(BaseModel): | ||||
ParentId: int = Field(...) | |||||
Name: str = Field(...) | |||||
Number: str = Field(...) | |||||
Condition: str = Field(...) | |||||
ImageURL: str = Field(...) | |||||
Cost: int = Field(...) | |||||
ParentId: int = Form(...) | |||||
Name: str = Form(...) | |||||
Number: str = Form(...) | |||||
Condition: str = Form(...) | |||||
Cost: int = Form(...) | |||||
image: UploadFile = Form(...) | |||||
class ShowCarPart(BaseModel): | class ShowCarPart(BaseModel): | ||||
@@ -15,5 +20,4 @@ class ShowCarPart(BaseModel): | |||||
Name: str = Field(...) | Name: str = Field(...) | ||||
Number: str = Field(...) | Number: str = Field(...) | ||||
Condition: str = Field(...) | Condition: str = Field(...) | ||||
ImageURL: str = Field(...) | |||||
Cost: int = Field(...) | Cost: int = Field(...) |
@@ -9,8 +9,11 @@ class CreateFuelingTask(BaseModel): | |||||
Cost: int = Field(...) | Cost: int = Field(...) | ||||
FuelRefilled: int = Field(...) | FuelRefilled: int = Field(...) | ||||
GasStationName: str = Field(...) | GasStationName: str = Field(...) | ||||
ImageBefore: str = Field(...) | |||||
ImageAfter: str = Field(...) | |||||
ImageBefore: bytearray = Field(...) | |||||
ImageAfter: bytearray = Field(...) | |||||
model_config={ | |||||
"arbitrary_types_allowed": True | |||||
} | |||||
class OutputFuelingTask(BaseModel): | class OutputFuelingTask(BaseModel): | ||||
VehicleId: int = Field(...) | VehicleId: int = Field(...) | ||||
@@ -19,6 +22,9 @@ class OutputFuelingTask(BaseModel): | |||||
Cost: int = Field(...) | Cost: int = Field(...) | ||||
FuelRefilled: int = Field(...) | FuelRefilled: int = Field(...) | ||||
GasStationName: str = Field(...) | GasStationName: str = Field(...) | ||||
ImageBefore: str = Field(...) | |||||
ImageAfter: str = Field(...) | |||||
Driver: ShowDriver | None | |||||
ImageBefore: bytearray = Field(...) | |||||
ImageAfter: bytearray = Field(...) | |||||
Driver: ShowDriver | None | |||||
model_config={ | |||||
"arbitrary_types_allowed": True | |||||
} |
@@ -3,10 +3,12 @@ from datetime import datetime | |||||
from typing import List, Optional | from typing import List, Optional | ||||
from schemas.carpart import ShowCarPart | from schemas.carpart import ShowCarPart | ||||
from schemas.user import ShowUser | from schemas.user import ShowUser | ||||
from schemas.vehicle import OutputVehicle | |||||
class CreateMaintenanceJob(BaseModel): | class CreateMaintenanceJob(BaseModel): | ||||
Description: str = Field(...) | Description: str = Field(...) | ||||
VehicleID: int = Field(...) | |||||
Date: datetime = Field(...) | Date: datetime = Field(...) | ||||
@@ -16,4 +18,5 @@ class OutputMaintenanceJob(BaseModel): | |||||
Date: datetime | Date: datetime | ||||
CarPartsList: Optional[List[ShowCarPart]] | CarPartsList: Optional[List[ShowCarPart]] | ||||
TotalCost: int | TotalCost: int | ||||
Vehicle: OutputVehicle | |||||
AssignedTo: ShowUser | AssignedTo: ShowUser |