forked from SBalaSundar/fastapi-crud-app-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.py
67 lines (47 loc) · 1.99 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import List
from sqlalchemy.orm import Session
from exceptions import CarInfoInfoAlreadyExistError, CarInfoNotFoundError
from models import CarInfo
from schemas import CreateAndUpdateCar
# Function to get list of car info
def get_all_cars(session: Session, limit: int, offset: int) -> List[CarInfo]:
return session.query(CarInfo).offset(offset).limit(limit).all()
# Function to get info of a particular car
def get_car_info_by_id(session: Session, _id: int) -> CarInfo:
car_info = session.query(CarInfo).get(_id)
if car_info is None:
raise CarInfoNotFoundError
return car_info
# Function to add a new car info to the database
def create_car(session: Session, car_info: CreateAndUpdateCar) -> CarInfo:
car_details = session.query(CarInfo).filter(CarInfo.manufacturer == car_info.manufacturer, CarInfo.modelName == car_info.modelName).first()
if car_details is not None:
raise CarInfoInfoAlreadyExistError
new_car_info = CarInfo(**car_info.dict())
session.add(new_car_info)
session.commit()
session.refresh(new_car_info)
return new_car_info
# Function to update details of the car
def update_car_info(session: Session, _id: int, info_update: CreateAndUpdateCar) -> CarInfo:
car_info = get_car_info_by_id(session, _id)
if car_info is None:
raise CarInfoNotFoundError
car_info.manufacturer = info_update.manufacturer
car_info.modelName = info_update.modelName
car_info.fuelType = info_update.fuelType
car_info.cc = info_update.cc
car_info.gearBox = info_update.gearBox
car_info.onRoadPrice = info_update.onRoadPrice
car_info.seatingCapacity = info_update.seatingCapacity
session.commit()
session.refresh(car_info)
return car_info
# Function to delete a car info from the db
def delete_car_info(session: Session, _id: int):
car_info = get_car_info_by_id(session, _id)
if car_info is None:
raise CarInfoNotFoundError
session.delete(car_info)
session.commit()
return