Commit b58376d6 by rakesh.pv

publish part updated codebase

parent fd13b4c5
[MQTT_Connection]
broker = 192.168.0.220
port = 1883
[redis_connection]
host = 127.0.0.1
redis_port = 6379
import uvicorn import uvicorn
if __name__ == '__main__': if __name__ == '__main__':
uvicorn.run("scripts.core.services.main:app",reload=True) uvicorn.run("scripts.core.services.main:app", reload=True)
\ No newline at end of file
from configparser import ConfigParser
try:
config = ConfigParser()
config.read("conf/application.conf")
broker = config.get("MQTT_Connection", "broker")
mqtt_port = config.get("MQTT_Connection", "port")
host = config.get("redis_connection", "host")
redis_port = config.get("redis_connection", "redis_port")
except Exception as e:
print("Exception-", e)
import json
import paho.mqtt.client as mqtt
from fastapi import HTTPException
from scripts.core.handlers.check_availablity import check_seat_avail
from scripts.database.redis.redis_connction import conn
class Tickets:
def __init__(self):
self.client = mqtt.Client()
self.client.connect("192.168.0.220", 1883, 60)
self.booked_list = []
def insert_data_into_db(self, details):
try:
if details.preferred_class not in ["gold", "silver"]:
raise HTTPException(status_code=400, detail="Invalid class type")
seats = details.seat_numbers.split(",")
integer_list = [int(item) for item in seats]
if len(integer_list) != details.no_of_tickets:
raise HTTPException(status_code=400, detail="Number of seat numbers must match the number of tickets")
for seat_ in integer_list:
if seat_ not in self.booked_list:
if not check_seat_avail(conn, details.preferred_class, int(seat_)):
self.booked_list.append(seat_)
if self.booked_list:
raise HTTPException(status_code=400,
detail=f"Seat number that are already booked are : {self.booked_list} \n"
f"seat number you are going to book are {integer_list}")
payload = {details.preferred_class: integer_list}
self.client.publish("ticket", json.dumps(payload))
return {"message": "Ticket booking successful"}
except Exception as e:
return e
from fastapi import APIRouter
from scripts.core.handlers.book_cinema_tickets import Tickets
from scripts.core.handlers.check_availablity import seat_availability
from scripts.database.redis.redis_connction import conn
from scripts.model.user_model import user
router=APIRouter()
@router.get("/")
async def index():
return " cinema hall is working"
@router.get("/class_availabilty")
async def check_seat():
x = input(" enter classs gold or silver")
if x == "Gold":
aa = seat_availability(conn, "Gold")
return aa
if x == "Silver":
aa = seat_availability(conn, "Silver")
return aa
@router.post("/book_cinema_ticket")
async def book_cinema_ticket(details: user):
try:
print(details)
return Tickets().insert_data_into_db(details)
except Exception as e:
print("error ", e)
import paho.mqtt.client as mqtt from fastapi import FastAPI
from scripts.core.routes import app_routes
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("ticket_booking")
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.0.220", 1883, 60)
from fastapi import FastAPI, HTTPException
from scripts.core.handlers.check_availablity import seat_availability, check_seat_avail
from scripts.database.redis.redis_connction import conn
app = FastAPI() app = FastAPI()
booked_seats = [] booked_seats = []
app.include_router(app_routes.router)
@app.get("/class_availabilty")
async def check_seat():
x = input(" enter classs gold or silver")
if x == "Gold":
aa = seat_availability(conn, "Gold")
return aa
if x == "Silver":
aa = seat_availability(conn, "Silver")
return aa
@app.post("/book_ticket")
async def book_ticket(
class_type: str,
number_of_tickets: int,
seat_numbers: list[int],
user_name: str,
user_age: int,
user_phone: int,
):
if class_type not in ["gold", "silver"]:
raise HTTPException(status_code=400, detail="Invalid class type")
if len(seat_numbers) != number_of_tickets:
raise HTTPException(status_code=400, detail="Number of seat numbers must match the number of tickets")
#
for seat_number in seat_numbers:
if seat_number not in booked_seats:
print("""ddsd""")
aa = check_seat_avail(conn, class_type, seat_number)
print("result is ", aa)
if not aa:
booked_seats.append(seat_number)
print(booked_seats)
if booked_seats:
raise HTTPException(status_code=400,
detail=f"Seat number that are already booked are : {booked_seats} \n"
f"seat number you are going to book are {seat_numbers}")
booked_seats.pop()
payload = {class_type: seat_numbers}
client.publish("ticket", str(payload))
client.subscribe("ticket/#")
return {"message": "Ticket booking successful"}
import redis import redis
conn=redis.Redis('127.0.0.1',db=0)
\ No newline at end of file from scripts.config.application_config import host, redis_port
conn=redis.Redis(host,redis_port,db=0)
...@@ -2,9 +2,9 @@ from pydantic import BaseModel ...@@ -2,9 +2,9 @@ from pydantic import BaseModel
class user(BaseModel): class user(BaseModel):
class_type: str mobile_no: int
number_of_tickets: int preferred_class: str
seat_numbers: list[int] no_of_tickets: int
user_name: str seat_numbers: str
user_age: int age: int
user_phone: int
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment