<filename>backend/main.py
from typing import Optional
from fastapi import FastAPI, Request, Depends, status
from sqlalchemy.orm import  Session
from sqlalchemy import update, delete
import orm, models
from database import sessionLocal, engine
from fastapi.middleware.cors import CORSMiddleware
import datetime

# Create Models
orm.Base.metadata.create_all(bind=engine)
app = FastAPI()

origins = [
    "http://localhost",
    "http://localhost:8080",
    "http://localhost:2004",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

def get_db():
    db = sessionLocal()
    try: 
        yield db
    finally:
        db.close()

#################################################
## Billiard API ##
#################################################

billiard_path = '/api/billiard/'

tables=[]
###### GET ###################
@app.get(billiard_path)
def get_billiard(request: Request, db: Session= Depends(get_db)):
    # tables = db.query(orm.BilloardTableOrm).all()
    return tables

###### POST ##################
@app.post(billiard_path)
def post_billiard(db: Session= Depends(get_db)):
    if tables:
        id = tables[-1].id + 1
    else:
        id = 1
    table = models.BilloardTable(id=id)
    tables.append(table)
    return table

###### PUT ##################
@app.put(billiard_path)
def put_billiard_date(
    id: int, 
    started: Optional[datetime.datetime] = None,
    time: Optional[int] = None,
    db: Session= Depends(get_db)):
    for i in range (0, len(tables)):
        x = tables[i]
        if x.id==id:
            if started:
                tables[i].started = started
                return status.HTTP_200_OK
            if time:
                tables[i].time = time
                tables[i].money = time*1000
                return time*1000
    return status.HTTP_304_NOT_MODIFIED

###### DELETE ###############
@app.delete(billiard_path)
def del_billiard(response: int,db: Session= Depends(get_db)):
    # stmt = delete(orm.BilloardTableOrm).\
    #     where(orm.BilloardTableOrm.id == response)
    # db.execute(stmt)
    # db.commit()
    for i in range (0, len(tables)):
        x = tables[i]
        if x.id==response:
            tables.pop(i)
            return status.HTTP_200_OK
    return status.HTTP_304_NOT_MODIFIED

#################################################
## Transaction API ##
#################################################

transaction_path = '/api/transaction'
###### GET ###################
@app.get(transaction_path)
def get_tran(db: Session= Depends(get_db)):
    tables = db.query(orm.TransactionOrm).all()
    return tables or status.HTTP_204_NO_CONTENT

###### POST ##################
@app.post(transaction_path)
def post_tran(response: models.Transaction,db: Session= Depends(get_db)):
    model = orm.TransactionOrm(
        table_id = response.table_id,
        started = response.started,
        time = response.time,
        client = response.client,
        paid = response.paid,
        owned = response.owned
    )
    db.add(model)
    db.commit()
    return status.HTTP_200_OK

###### PUT ##################
@app.put(transaction_path)
def put_tran(response: models.Transaction, db: Session= Depends(get_db)):
    stmt = update(orm.TransactionOrm).\
        where(orm.TransactionOrm.id == response.id).\
        values(
            table_id = response.table_id,
            started = response.started,
            time = response.time,
            client = response.client,
            paid = response.paid,
            owned = response.owned
        )
    print(stmt)
    db.execute(stmt)
    db.commit()
    return status.HTTP_200_OK
###### DELETE ###############
@app.delete(transaction_path)
def del_tran(response: int,db: Session= Depends(get_db)):
    stmt = delete(orm.TransactionOrm).\
        where(orm.TransactionOrm.id == response)
    db.execute(stmt)
    db.commit()
    return status.HTTP_200_OK
<filename>backend/main.py
from typing import Optional
from fastapi import FastAPI, Request, Depends, status
from sqlalchemy.orm import  Session
from sqlalchemy import update, delete
import orm, models
from database import sessionLocal, engine
from fastapi.middleware.cors import CORSMiddleware
import datetime

# Create Models
orm.Base.metadata.create_all(bind=engine)
app = FastAPI()

origins = [
    "http://localhost",
    "http://localhost:8080",
    "http://localhost:2004",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

def get_db():
    db = sessionLocal()
    try: 
        yield db
    finally:
        db.close()

#################################################
## Billiard API ##
#################################################

billiard_path = '/api/billiard/'

tables=[]
###### GET ###################
@app.get(billiard_path)
def get_billiard(request: Request, db: Session= Depends(get_db)):
    # tables = db.query(orm.BilloardTableOrm).all()
    return tables

###### POST ##################
@app.post(billiard_path)
def post_billiard(db: Session= Depends(get_db)):
    if tables:
        id = tables[-1].id + 1
    else:
        id = 1
    table = models.BilloardTable(id=id)
    tables.append(table)
    return table

###### PUT ##################
@app.put(billiard_path)
def put_billiard_date(
    id: int, 
    started: Optional[datetime.datetime] = None,
    time: Optional[int] = None,
    db: Session= Depends(get_db)):
    for i in range (0, len(tables)):
        x = tables[i]
        if x.id==id:
            if started:
                tables[i].started = started
                return status.HTTP_200_OK
            if time:
                tables[i].time = time
                tables[i].money = time*1000
                return time*1000
    return status.HTTP_304_NOT_MODIFIED

###### DELETE ###############
@app.delete(billiard_path)
def del_billiard(response: int,db: Session= Depends(get_db)):
    # stmt = delete(orm.BilloardTableOrm).\
    #     where(orm.BilloardTableOrm.id == response)
    # db.execute(stmt)
    # db.commit()
    for i in range (0, len(tables)):
        x = tables[i]
        if x.id==response:
            tables.pop(i)
            return status.HTTP_200_OK
    return status.HTTP_304_NOT_MODIFIED

#################################################
## Transaction API ##
#################################################

transaction_path = '/api/transaction'
###### GET ###################
@app.get(transaction_path)
def get_tran(db: Session= Depends(get_db)):
    tables = db.query(orm.TransactionOrm).all()
    return tables or status.HTTP_204_NO_CONTENT

###### POST ##################
@app.post(transaction_path)
def post_tran(response: models.Transaction,db: Session= Depends(get_db)):
    model = orm.TransactionOrm(
        table_id = response.table_id,
        started = response.started,
        time = response.time,
        client = response.client,
        paid = response.paid,
        owned = response.owned
    )
    db.add(model)
    db.commit()
    return status.HTTP_200_OK

###### PUT ##################
@app.put(transaction_path)
def put_tran(response: models.Transaction, db: Session= Depends(get_db)):
    stmt = update(orm.TransactionOrm).\
        where(orm.TransactionOrm.id == response.id).\
        values(
            table_id = response.table_id,
            started = response.started,
            time = response.time,
            client = response.client,
            paid = response.paid,
            owned = response.owned
        )
    print(stmt)
    db.execute(stmt)
    db.commit()
    return status.HTTP_200_OK
###### DELETE ###############
@app.delete(transaction_path)
def del_tran(response: int,db: Session= Depends(get_db)):
    stmt = delete(orm.TransactionOrm).\
        where(orm.TransactionOrm.id == response)
    db.execute(stmt)
    db.commit()
    return status.HTTP_200_OK
# Create Models ################################################# ## Billiard API ## ################################################# ###### GET ################### # tables = db.query(orm.BilloardTableOrm).all() ###### POST ################## ###### PUT ################## ###### DELETE ############### # stmt = delete(orm.BilloardTableOrm).\ #     where(orm.BilloardTableOrm.id == response) # db.execute(stmt) # db.commit() ################################################# ## Transaction API ## ################################################# ###### GET ################### ###### POST ################## ###### PUT ################## ###### DELETE ###############