Sfoglia il codice sorgente

Added carparts and maintenanceJobs

main
Madiwka3 1 anno fa
parent
commit
b74c00fdc5
9 ha cambiato i file con 197 aggiunte e 5 eliminazioni
  1. +10
    -1
      app/apis/base.py
  2. +76
    -0
      app/apis/v1/route_maintenancejob.py
  3. +1
    -4
      app/apis/v1/route_task.py
  4. +3
    -0
      app/db/base.py
  5. +15
    -0
      app/db/models/carpart.py
  6. +12
    -0
      app/db/models/maintenancejob.py
  7. +63
    -0
      app/db/repository/maintenancejob.py
  8. +10
    -0
      app/schemas/carpart.py
  9. +7
    -0
      app/schemas/maintenancejob.py

+ 10
- 1
app/apis/base.py Vedi File

@@ -1,10 +1,19 @@
# Base API router -- collecting all APIs here to not clutter main.py
from fastapi import APIRouter

from apis.v1 import route_user, route_vehicle, route_auth, route_task
from apis.v1 import (
route_user,
route_vehicle,
route_auth,
route_task,
route_maintenancejob,
)

api_router = APIRouter()
api_router.include_router(route_user.router, prefix="/user", tags=["users"])
api_router.include_router(route_vehicle.router, prefix="/vehicle", tags=["vehicles"])
api_router.include_router(route_auth.router, prefix="", tags=["auth"])
api_router.include_router(route_task.router, prefix="/task", tags=["task"])
api_router.include_router(
route_maintenancejob.router, prefix="/maintenancejob", tags=["maintenancejob"]
)

+ 76
- 0
app/apis/v1/route_maintenancejob.py Vedi File

@@ -0,0 +1,76 @@
from fastapi import Depends, APIRouter, HTTPException, status
from sqlalchemy.orm import Session
from db.session import get_db
from db.repository.maintenancejob import (
create_new_maintenancejob,
create_car_part,
get_all_maintenance_jobs,
get_maintenance_job,
)
from schemas.maintenancejob import CreateMaintenanceJob
from schemas.carpart import CreateCarPart
from db.models.user import User
from apis.v1.route_auth import get_current_user

router = APIRouter()


@router.post("/", status_code=status.HTTP_201_CREATED)
def create_maintenancejob(
maintenancejob: CreateMaintenanceJob,
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"
)

maintenancejob_res = create_new_maintenancejob(
maintenancejob=maintenancejob, maintenanceworker=current_user.Id, db=db
)
return maintenancejob_res


@router.post("/carpart", status_code=status.HTTP_201_CREATED)
def create_carpart(
car_part: CreateCarPart,
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"
)

car_part_res = create_car_part(car_part=car_part, db=db)
return car_part_res


@router.get("/", status_code=status.HTTP_200_OK)
def get_all_maintenancejobs(
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"
)

maintenancejobs = get_all_maintenance_jobs(db)
return maintenancejobs


@router.get("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
def get_maintenancejob(
maintenance_job_id: int,
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"
)

maintenancejob = get_maintenance_job(maintenance_job_id, db)
return maintenancejob

+ 1
- 4
app/apis/v1/route_task.py Vedi File

@@ -1,7 +1,5 @@
from fastapi import Depends, APIRouter
from fastapi import Depends, APIRouter, HTTPException, status
from sqlalchemy.orm import Session
from fastapi import status, HTTPException
from typing import Annotated
from db.session import get_db
from core.config import settings
from db.repository.drivetask import (
@@ -15,7 +13,6 @@ from db.repository.drivetask import (
from schemas.drivetask import CreateTask
from db.models.user import User
from apis.v1.route_auth import get_current_user
from db.models.drivetask import DriveTask

router = APIRouter()



+ 3
- 0
app/db/base.py Vedi File

@@ -2,3 +2,6 @@
from db.base_class import Base
from db.models.user import User
from db.models.vehicle import Vehicle
from db.models.carpart import CarPart
from db.models.maintenancejob import MaintenanceJob
from db.models.drivetask import DriveTask

+ 15
- 0
app/db/models/carpart.py Vedi File

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


class CarPart(Base):
Id = Column(Integer, primary_key=True, index=True)
# a list of weak entities of class CarPart
ParentId = Column(Integer, ForeignKey("maintenancejob.Id"), nullable=False)
parent = relationship("MaintenanceJob", back_populates="CarParts")
Name = Column(String, nullable=False)
Number = Column(String, nullable=False)
Condition = Column(String, nullable=False)
ImageURL = Column(String, nullable=False)
Cost = Column(Integer, nullable=False)

+ 12
- 0
app/db/models/maintenancejob.py Vedi File

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


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")
Description = Column(String, nullable=False)
Date = Column(DateTime, nullable=False)
MaintenanceWorker = Column(ForeignKey("user.Id"), nullable=False)

+ 63
- 0
app/db/repository/maintenancejob.py Vedi File

@@ -0,0 +1,63 @@
from sqlalchemy.orm import Session
from schemas.maintenancejob import CreateMaintenanceJob
from db.models.maintenancejob import MaintenanceJob
from schemas.carpart import CreateCarPart
from db.models.carpart import CarPart


def create_new_maintenancejob(
maintenancejob: CreateMaintenanceJob, maintenanceworker, db: Session
):
maintenancejob_object = MaintenanceJob(
MaintenanceWorker=maintenanceworker,
Description=maintenancejob.Description,
Date=maintenancejob.Date,
)
print("OBJECT CREATED")
db.add(maintenancejob_object)
db.commit()
db.refresh(maintenancejob_object)
return maintenancejob_object


def create_car_part(car_part: CreateCarPart, db: Session):
car_part_object = CarPart(
ParentId=car_part.ParentId,
Name=car_part.Name,
Number=car_part.Number,
Condition=car_part.Condition,
ImageURL=car_part.ImageURL,
Cost=car_part.Cost,
)
print("OBJECT CREATED")
db.add(car_part_object)
db.commit()
db.refresh(car_part_object)
return car_part_object


def calculate_total_cost(car_parts: CarPart):
total_cost = 0
for part in car_parts:
total_cost += part["Cost"]
return total_cost


def get_all_maintenance_jobs(db: Session):
maintenancejobs = db.query(MaintenanceJob).all()
result = []
for job in maintenancejobs:
job_dict = job.__dict__
job_dict["CarParts"] = [part.__dict__ for part in job.CarParts]
job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
result.append(job_dict)
return maintenancejobs


def get_maintenance_job(maintenancejob_id: int, db: Session):
maintenancejob = (
db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
)
result = maintenancejob.__dict__
result["CarParts"] = [part.__dict__ for part in maintenancejob.CarParts]
return result

+ 10
- 0
app/schemas/carpart.py Vedi File

@@ -0,0 +1,10 @@
from pydantic import BaseModel, Field


class CreateCarPart(BaseModel):
ParentId: int = Field(...)
Name: str = Field(...)
Number: str = Field(...)
Condition: str = Field(...)
ImageURL: str = Field(...)
Cost: int = Field(...)

+ 7
- 0
app/schemas/maintenancejob.py Vedi File

@@ -0,0 +1,7 @@
from pydantic import BaseModel, Field
from datetime import datetime


class CreateMaintenanceJob(BaseModel):
Description: str = Field(...)
Date: datetime = Field(...)

Caricamento…
Annulla
Salva