adding base project
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Azzu Reference Module
|
||||
"""
|
||||
@@ -0,0 +1,51 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.responses import JSONResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Dict, Any
|
||||
|
||||
from app.core.deps import get_db
|
||||
from app.io8ref.Azzu.services import Service
|
||||
|
||||
# Create two routers or one with route aliases
|
||||
router = APIRouter(tags=["Azzu Entity CRUD"])
|
||||
|
||||
# add data
|
||||
@router.post("/Azzu")
|
||||
@router.post("/azzu")
|
||||
def Savedata(data: Dict[str, Any], db: Session = Depends(get_db)):
|
||||
saved = Service.Savedata(db, data)
|
||||
return saved
|
||||
|
||||
# get all
|
||||
@router.get("/Azzu")
|
||||
@router.get("/azzu")
|
||||
def getdetails(db: Session = Depends(get_db)):
|
||||
items = Service.getdetails(db)
|
||||
return items
|
||||
|
||||
# getby id
|
||||
@router.get("/Azzu/{id}")
|
||||
@router.get("/azzu/{id}")
|
||||
def getdetailsbyId(id: int, db: Session = Depends(get_db)):
|
||||
item = Service.getdetailsbyId(db, id)
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return item
|
||||
|
||||
# update by id
|
||||
@router.put("/Azzu/{id}")
|
||||
@router.put("/azzu/{id}")
|
||||
def update(data: Dict[str, Any], id: int, db: Session = Depends(get_db)):
|
||||
updated = Service.update(db, data, id)
|
||||
if not updated:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return updated
|
||||
|
||||
# delete by id
|
||||
@router.delete("/Azzu/{id}")
|
||||
@router.delete("/azzu/{id}")
|
||||
def delete_by_id(id: int, db: Session = Depends(get_db)):
|
||||
success = Service.delete_by_id(db, id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "Deleted successfully"})
|
||||
@@ -0,0 +1,14 @@
|
||||
from sqlalchemy import Column, Integer, String, Text, Float, Date, DateTime, Boolean
|
||||
from datetime import datetime
|
||||
from app.core.database import Base
|
||||
|
||||
class Azzu(Base):
|
||||
__tablename__ = "azzu"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||
# Reference demo fields
|
||||
name = Column(String(255), nullable=True)
|
||||
description = Column(Text, nullable=True)
|
||||
status = Column(String(100), default="ACTIVE")
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
@@ -0,0 +1,22 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from datetime import datetime, date
|
||||
|
||||
class AzzuBase(BaseModel):
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
status: Optional[str] = "ACTIVE"
|
||||
|
||||
class AzzuCreate(AzzuBase):
|
||||
pass
|
||||
|
||||
class AzzuUpdate(AzzuBase):
|
||||
pass
|
||||
|
||||
class AzzuResponse(AzzuBase):
|
||||
id: int
|
||||
created_at: Optional[datetime] = None
|
||||
updated_at: Optional[datetime] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
@@ -0,0 +1,46 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional
|
||||
from app.io8ref.Azzu.models import Azzu
|
||||
from app.io8ref.Azzu.schemas import AzzuCreate, AzzuUpdate
|
||||
|
||||
class AzzuService:
|
||||
def Savedata(self, db: Session, data: dict) -> Azzu:
|
||||
"""Saves a new Azzu record."""
|
||||
# Filter out id if present
|
||||
data_dict = {k: v for k, v in data.items() if k != "id" and hasattr(Azzu, k)}
|
||||
obj = Azzu(**data_dict)
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
def getdetails(self, db: Session) -> List[Azzu]:
|
||||
"""Returns all Azzu records."""
|
||||
return db.query(Azzu).all()
|
||||
|
||||
def getdetailsbyId(self, db: Session, id_val: int) -> Optional[Azzu]:
|
||||
"""Returns an Azzu record by ID."""
|
||||
return db.query(Azzu).filter(Azzu.id == id_val).first()
|
||||
|
||||
def update(self, db: Session, data: dict, id_val: int) -> Optional[Azzu]:
|
||||
"""Updates an Azzu record by ID."""
|
||||
obj = self.getdetailsbyId(db, id_val)
|
||||
if not obj:
|
||||
return None
|
||||
for key, value in data.items():
|
||||
if key != "id" and hasattr(obj, key):
|
||||
setattr(obj, key, value)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
def delete_by_id(self, db: Session, id_val: int) -> bool:
|
||||
"""Deletes an Azzu record by ID."""
|
||||
obj = self.getdetailsbyId(db, id_val)
|
||||
if obj:
|
||||
db.delete(obj)
|
||||
db.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
Service = AzzuService()
|
||||
Reference in New Issue
Block a user