mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-09 22:00:18 -04:00
Updated main.py for API routes to include tags and rolled entities back.
This commit is contained in:
parent
4c3d49004a
commit
a563ca2bfa
|
@ -23,22 +23,22 @@ openapi_tags = {
|
|||
def create(
|
||||
subject: User,
|
||||
tag: Tag,
|
||||
tag_service: TagService
|
||||
tag_service: TagService=Depends()
|
||||
):
|
||||
return tag_service.create(subject, tag)
|
||||
|
||||
@api.get("", response_model=List[Tag], tags=["Tag"])
|
||||
def get_all(
|
||||
subject: User,
|
||||
tag_svc: TagService
|
||||
tag_svc: TagService=Depends()
|
||||
):
|
||||
return tag_svc.get_all()
|
||||
|
||||
@api.put("", response_model=Tag, tags=["Tag"])
|
||||
def delete(
|
||||
def update(
|
||||
subject: User,
|
||||
tag: Tag,
|
||||
tag_svc: TagService
|
||||
tag_svc: TagService=Depends()
|
||||
):
|
||||
return tag_svc.delete(subject, tag)
|
||||
|
||||
|
@ -46,6 +46,6 @@ def delete(
|
|||
def delete(
|
||||
subject: User,
|
||||
tag: Tag,
|
||||
tag_svc: TagService
|
||||
tag_svc: TagService=Depends()
|
||||
):
|
||||
tag_svc.delete(subject, tag)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
""" Defines the table for storing resources """
|
||||
|
||||
# Import our mapped SQL types from SQLAlchemy
|
||||
from sqlalchemy import ForeignKey, Integer, String, DateTime, Enum
|
||||
from sqlalchemy import Integer, String, DateTime, Enum
|
||||
|
||||
# Import mapping capabilities from the SQLAlchemy ORM
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
@ -31,7 +31,9 @@ class ResourceEntity(EntityBase):
|
|||
link: Mapped[str] = mapped_column(String, nullable=False)
|
||||
program: Mapped[Program_Enum] = mapped_column(Enum(Program_Enum), nullable=False)
|
||||
# relationships
|
||||
tags: Mapped[list["ResourceTagEntity"]] = relationship(back_populates="resource")
|
||||
resourceTags: Mapped[list["ResourceTagEntity"]] = relationship(
|
||||
back_populates="resource", cascade="all,delete"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_model(cls, model: Resource) -> Self:
|
||||
|
@ -62,4 +64,4 @@ class ResourceEntity(EntityBase):
|
|||
summary=self.summary,
|
||||
link=self.link,
|
||||
program=self.program,
|
||||
)
|
||||
)
|
|
@ -23,10 +23,12 @@ class ResourceTagEntity(EntityBase):
|
|||
|
||||
# set fields or 'columns' for the user table
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
resource_id: Mapped[int] = mapped_column(ForeignKey("resource.id"), primary_key=True)
|
||||
tag_id: Mapped[int] = mapped_column(ForeignKey("tag.id", primary_key=True))
|
||||
resource: Mapped["ResourceEntity"] = mapped_column(backpopulates="tags")
|
||||
tag: Mapped["TagEntity"] = mapped_column(backpopulates="resource_tags")
|
||||
resourceId: Mapped[int] = mapped_column(ForeignKey("resource.id"))
|
||||
tagId: Mapped[int] = mapped_column(ForeignKey("tag.id"))
|
||||
|
||||
# relationships
|
||||
resource: Mapped["ResourceEntity"] = relationship(back_populates="resourceTags")
|
||||
tag: Mapped["TagEntity"] = relationship(back_populates="resourceTags")
|
||||
|
||||
# @classmethod
|
||||
# def from_model (cls, model: resource_tag_model) -> Self:
|
||||
|
@ -41,4 +43,4 @@ class ResourceTagEntity(EntityBase):
|
|||
# id = self.id,
|
||||
# resourceId = self.resourceId,
|
||||
# tagId = self.tagId,
|
||||
# )
|
||||
# )
|
|
@ -20,7 +20,6 @@ from backend.models.service_model import Service
|
|||
from typing import Self
|
||||
from backend.models.enum_for_models import ProgramTypeEnum
|
||||
|
||||
|
||||
class ServiceEntity(EntityBase):
|
||||
|
||||
# set table name
|
||||
|
@ -33,32 +32,16 @@ class ServiceEntity(EntityBase):
|
|||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
summary: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
requirements: Mapped[list[str]] = mapped_column(ARRAY(String))
|
||||
program: Mapped[ProgramTypeEnum] = mapped_column(
|
||||
Enum(ProgramTypeEnum), nullable=False
|
||||
)
|
||||
program: Mapped[ProgramTypeEnum] = mapped_column(Enum(ProgramTypeEnum), nullable=False)
|
||||
|
||||
# relationships
|
||||
tags: Mapped[list["ServiceTagEntity"]] = relationship(
|
||||
serviceTags: Mapped[list["ServiceTagEntity"]] = relationship(
|
||||
back_populates="service", cascade="all,delete"
|
||||
)
|
||||
|
||||
def to_model(self) -> Service:
|
||||
return Service(
|
||||
id=self.id,
|
||||
name=self.name,
|
||||
status=self.status,
|
||||
summary=self.summary,
|
||||
requirements=self.requirements,
|
||||
program=self.program,
|
||||
)
|
||||
return Service(id=self.id, name=self.name, status=self.status, summary=self.summary, requirements=self.requirements, program=self.program)
|
||||
|
||||
@classmethod
|
||||
def from_model(cls, model: Service) -> Self:
|
||||
return cls(
|
||||
id=model.id,
|
||||
name=model.name,
|
||||
status=model.status,
|
||||
summary=model.summary,
|
||||
requirements=model.requirements,
|
||||
program=model.program,
|
||||
)
|
||||
def from_model(cls, model:Service) -> Self:
|
||||
return cls(id=model.id, name=model.name, status=model.status, summary=model.summary, requirements=model.requirements, program=model.program)
|
|
@ -17,7 +17,10 @@ class ServiceTagEntity(EntityBase):
|
|||
|
||||
# set fields or 'columns' for the user table
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
resource_id: Mapped[int] = mapped_column(ForeignKey("service.id"), primary_key=True)
|
||||
tag_id: Mapped[int] = mapped_column(ForeignKey("tag.id", primary_key=True))
|
||||
service: Mapped["ServiceEntity"] = mapped_column(backpopulates="tags")
|
||||
tag: Mapped["TagEntity"] = mapped_column(backpopulates="service_tags")
|
||||
serviceId: Mapped[int] = mapped_column(ForeignKey("service.id"))
|
||||
tagId: Mapped[int] = mapped_column(ForeignKey("tag.id"))
|
||||
|
||||
# relationships
|
||||
service: Mapped["ServiceEntity"] = relationship(back_populates="serviceTags")
|
||||
tag: Mapped["TagEntity"] = relationship(back_populates="serviceTags")
|
||||
|
|
@ -16,21 +16,22 @@ from ..models.tag_model import Tag
|
|||
|
||||
from typing import Self
|
||||
|
||||
|
||||
class TagEntity(EntityBase):
|
||||
|
||||
# set table name
|
||||
#set table name
|
||||
__tablename__ = "tag"
|
||||
|
||||
# set fields
|
||||
#set fields
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now)
|
||||
content: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
resource_tags: Mapped[list["ResourceTagEntity"]] = relationship(
|
||||
back_populates="tag"
|
||||
)
|
||||
service_tags: Mapped[list["ServiceTagEntity"]] = relationship(back_populates="tag")
|
||||
|
||||
#relationships
|
||||
resourceTags: Mapped[list["ResourceTagEntity"]] = relationship(back_populates="tag", cascade="all,delete")
|
||||
serviceTags: Mapped[list["ServiceTagEntity"]] = relationship(back_populates="tag", cascade="all,delete")
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_model(cls, model: Tag) -> Self:
|
||||
"""
|
||||
|
@ -41,11 +42,11 @@ class TagEntity(EntityBase):
|
|||
Returns:
|
||||
self: The entity
|
||||
"""
|
||||
|
||||
|
||||
return cls(
|
||||
id=model.id,
|
||||
created_at=model.created_at,
|
||||
content=model.id,
|
||||
created_at=model.created_at
|
||||
)
|
||||
|
||||
def to_model(self) -> Tag:
|
||||
|
@ -58,6 +59,6 @@ class TagEntity(EntityBase):
|
|||
|
||||
return Tag(
|
||||
id=self.id,
|
||||
create_at=self.created_at,
|
||||
content=self.content,
|
||||
created_at=self.created_at
|
||||
)
|
||||
)
|
|
@ -1,34 +1,37 @@
|
|||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.middleware.gzip import GZipMiddleware
|
||||
|
||||
from .api import user, health, service, resource
|
||||
|
||||
description = """
|
||||
Welcome to the **COMPASS** RESTful Application Programming Interface.
|
||||
"""
|
||||
|
||||
app = FastAPI(
|
||||
title="Compass API",
|
||||
version="0.0.1",
|
||||
description=description,
|
||||
openapi_tags=[
|
||||
user.openapi_tags,
|
||||
health.openapi_tags,
|
||||
service.openapi_tags,
|
||||
resource.openapi_tags,
|
||||
],
|
||||
)
|
||||
|
||||
app.add_middleware(GZipMiddleware)
|
||||
|
||||
feature_apis = [user, health, service, resource]
|
||||
|
||||
for feature_api in feature_apis:
|
||||
app.include_router(feature_api.api)
|
||||
|
||||
|
||||
# Add application-wide exception handling middleware for commonly encountered API Exceptions
|
||||
@app.exception_handler(Exception)
|
||||
def permission_exception_handler(request: Request, e: Exception):
|
||||
return JSONResponse(status_code=403, content={"message": str(e)})
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.middleware.gzip import GZipMiddleware
|
||||
|
||||
|
||||
|
||||
from .api import user, health, service, resource, tag
|
||||
|
||||
description = """
|
||||
Welcome to the **COMPASS** RESTful Application Programming Interface.
|
||||
"""
|
||||
|
||||
app = FastAPI(
|
||||
title="Compass API",
|
||||
version="0.0.1",
|
||||
description=description,
|
||||
openapi_tags=[
|
||||
user.openapi_tags,
|
||||
health.openapi_tags,
|
||||
service.openapi_tags,
|
||||
resource.openapi_tags,
|
||||
tag.openapi_tags
|
||||
],
|
||||
)
|
||||
|
||||
app.add_middleware(GZipMiddleware)
|
||||
|
||||
feature_apis = [user, health, service, resource, tag]
|
||||
|
||||
for feature_api in feature_apis:
|
||||
app.include_router(feature_api.api)
|
||||
|
||||
|
||||
# Add application-wide exception handling middleware for commonly encountered API Exceptions
|
||||
@app.exception_handler(Exception)
|
||||
def permission_exception_handler(request: Request, e: Exception):
|
||||
return JSONResponse(status_code=403, content={"message": str(e)})
|
||||
|
|
Loading…
Reference in New Issue
Block a user