generated from Luis/nextjs-python-web-template
Fully working schemas and database, with api endpoints. two missing.
This commit is contained in:
@@ -4,26 +4,37 @@ from fastapi import APIRouter, Depends
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from .. import sql_crud, sql_db, sql_models
|
from .. import sql_crud, sql_db, sql_models
|
||||||
from ..schemas import Entity, EntityCreate
|
from ..schemas import (
|
||||||
|
Consumer,
|
||||||
|
ConsumerCreate,
|
||||||
|
Entity,
|
||||||
|
EntityCreate,
|
||||||
|
Producer,
|
||||||
|
ProducerCreate,
|
||||||
|
Repository,
|
||||||
|
RepositoryCreate,
|
||||||
|
)
|
||||||
from ..tags import Tags
|
from ..tags import Tags
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
# @router.get("/api/v1/get_producers", response_model=List[Producer], tags=[Tags.producers])
|
@router.get(
|
||||||
# def get_producers(
|
"/api/v1/get_producers", response_model=List[Producer], tags=[Tags.producers]
|
||||||
# skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
|
)
|
||||||
# ) -> List[sql_models.Producer]:
|
def get_producers(
|
||||||
# producers = sql_crud.get_producers(db, skip=skip, limit=limit)
|
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
|
||||||
# return producers
|
) -> List[sql_models.Producer]:
|
||||||
|
producers = sql_crud.get_producers(db, skip=skip, limit=limit)
|
||||||
|
return producers
|
||||||
|
|
||||||
|
|
||||||
# @router.post("/api/v1/create_producer", response_model=Producer, tags=[Tags.producers])
|
@router.post("/api/v1/create_producer", response_model=Producer, tags=[Tags.producers])
|
||||||
# def create_producer(
|
def create_producer(
|
||||||
# producer: ProducerCreate, db: Session = Depends(sql_db.get_db)
|
producer: ProducerCreate, db: Session = Depends(sql_db.get_db)
|
||||||
# ) -> Producer:
|
) -> Producer:
|
||||||
# # todo checken ob schon da ...
|
# todo checken ob schon da ...
|
||||||
# return sql_crud.create_producer(db=db, producer=producer)
|
return sql_crud.create_producer(db=db, producer=producer)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities])
|
@router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities])
|
||||||
@@ -40,3 +51,61 @@ def get_entities(
|
|||||||
) -> List[sql_models.Entity]:
|
) -> List[sql_models.Entity]:
|
||||||
entities = sql_crud.get_entities(db, skip=skip, limit=limit)
|
entities = sql_crud.get_entities(db, skip=skip, limit=limit)
|
||||||
return entities
|
return entities
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/api/v1/create_consumer", response_model=Consumer, tags=[Tags.consumers])
|
||||||
|
def create_consumer(
|
||||||
|
consumer: ConsumerCreate, db: Session = Depends(sql_db.get_db)
|
||||||
|
) -> Consumer:
|
||||||
|
# todo checken ob schon da ...
|
||||||
|
return sql_crud.create_consumer(db=db, consumer=consumer)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/api/v1/get_consumers", response_model=List[Consumer], tags=[Tags.consumers]
|
||||||
|
)
|
||||||
|
def get_consumers(
|
||||||
|
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
|
||||||
|
) -> List[sql_models.Consumer]:
|
||||||
|
consumers = sql_crud.get_consumers(db, skip=skip, limit=limit)
|
||||||
|
return consumers
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
|
# #
|
||||||
|
# REPOSITORY #
|
||||||
|
# #
|
||||||
|
#########################
|
||||||
|
@router.post(
|
||||||
|
"/api/v1/add_to_repository", response_model=Repository, tags=[Tags.repositories]
|
||||||
|
)
|
||||||
|
def add_to_repository(
|
||||||
|
repository: RepositoryCreate, db: Session = Depends(sql_db.get_db)
|
||||||
|
) -> sql_models.Repository:
|
||||||
|
# todo checken ob schon da ...
|
||||||
|
return sql_crud.create_repository(db=db, repository=repository)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/api/v1/get_repositories",
|
||||||
|
response_model=List[Repository],
|
||||||
|
tags=[Tags.repositories],
|
||||||
|
)
|
||||||
|
def get_repositories(
|
||||||
|
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
|
||||||
|
) -> List[sql_models.Repository]:
|
||||||
|
repositories = sql_crud.get_repositories(db, skip=skip, limit=limit)
|
||||||
|
return repositories
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/api/v1/get_repository", response_model=List[Repository], tags=[Tags.repositories]
|
||||||
|
)
|
||||||
|
def get_repository(
|
||||||
|
entity_did: str,
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 100,
|
||||||
|
db: Session = Depends(sql_db.get_db),
|
||||||
|
) -> List[sql_models.Repository]:
|
||||||
|
repository = sql_crud.get_repository_by_did(db, did=entity_did)
|
||||||
|
return repository
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
@@ -14,6 +16,76 @@ class Machine(BaseModel):
|
|||||||
status: Status
|
status: Status
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
|
# #
|
||||||
|
# Producer #
|
||||||
|
# #
|
||||||
|
#########################
|
||||||
|
class ProducerBase(BaseModel):
|
||||||
|
uuid: str = "8e285c0c-4e40-430a-a477-26b3b81e30df"
|
||||||
|
service_name: str = "Carlo's Printing"
|
||||||
|
service_type: str = "3D Printing"
|
||||||
|
endpoint_url: str = "http://127.0.0.1:8000"
|
||||||
|
status: str = "unknown"
|
||||||
|
other: dict = {"test": "test"}
|
||||||
|
|
||||||
|
|
||||||
|
class ProducerCreate(ProducerBase):
|
||||||
|
entity_did: str = "did:sov:test:1234"
|
||||||
|
|
||||||
|
|
||||||
|
class Producer(ProducerCreate):
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
|
# #
|
||||||
|
# Consumer #
|
||||||
|
# #
|
||||||
|
#########################
|
||||||
|
class ConsumerBase(BaseModel):
|
||||||
|
entity_did: str = "did:sov:test:1234"
|
||||||
|
producer_uuid: str = "8e285c0c-4e40-430a-a477-26b3b81e30df"
|
||||||
|
other: dict = {"test": "test"}
|
||||||
|
|
||||||
|
|
||||||
|
class ConsumerCreate(ConsumerBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Consumer(ConsumerCreate):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
|
# #
|
||||||
|
# REPOSITORY #
|
||||||
|
# #
|
||||||
|
#########################
|
||||||
|
class RepositoryBase(ProducerBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class RepositoryCreate(RepositoryBase):
|
||||||
|
entity_did: str = "did:sov:test:1234"
|
||||||
|
|
||||||
|
|
||||||
|
class Repository(RepositoryCreate):
|
||||||
|
time_created: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
|
# #
|
||||||
|
# Entity #
|
||||||
|
# #
|
||||||
|
#########################
|
||||||
class EntityBase(BaseModel):
|
class EntityBase(BaseModel):
|
||||||
did: str = "did:sov:test:1234"
|
did: str = "did:sov:test:1234"
|
||||||
name: str = "C1"
|
name: str = "C1"
|
||||||
@@ -22,50 +94,14 @@ class EntityBase(BaseModel):
|
|||||||
other: dict = {"test": "test"}
|
other: dict = {"test": "test"}
|
||||||
|
|
||||||
|
|
||||||
class Entity(EntityBase):
|
|
||||||
class Config:
|
|
||||||
orm_mode = True
|
|
||||||
|
|
||||||
|
|
||||||
class EntityCreate(EntityBase):
|
class EntityCreate(EntityBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class RepositoryBase(BaseModel):
|
class Entity(EntityCreate):
|
||||||
title: str
|
producers: List[Producer] = []
|
||||||
description: str | None = None
|
consumers: List[Consumer] = []
|
||||||
|
repository: Optional[Repository] = None
|
||||||
|
|
||||||
class RepositoryCreate(RepositoryBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Repository(RepositoryBase):
|
|
||||||
id: int
|
|
||||||
prod_id: str
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
orm_mode = True
|
|
||||||
|
|
||||||
|
|
||||||
class ProducerBase(BaseModel):
|
|
||||||
id: int
|
|
||||||
|
|
||||||
|
|
||||||
class ProducerCreate(ProducerBase):
|
|
||||||
jsonblob: int = Field(
|
|
||||||
42,
|
|
||||||
title="The Json",
|
|
||||||
description="this is the value of json",
|
|
||||||
gt=30,
|
|
||||||
lt=50,
|
|
||||||
list=[1, 2, "3"],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Producer(ProducerBase):
|
|
||||||
id: int
|
|
||||||
repos: list[Repository] = []
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@@ -19,34 +19,69 @@ def get_entities(
|
|||||||
return db.query(sql_models.Entity).offset(skip).limit(limit).all()
|
return db.query(sql_models.Entity).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
|
||||||
# def get_producers(
|
def create_consumer(
|
||||||
# db: Session, skip: int = 0, limit: int = 100
|
db: Session, consumer: schemas.ConsumerCreate
|
||||||
# ) -> List[sql_models.Producer]:
|
) -> sql_models.Consumer:
|
||||||
# return db.query(sql_models.Producer).offset(skip).limit(limit).all()
|
db_consumer = sql_models.Consumer(**consumer.dict())
|
||||||
|
db.add(db_consumer)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_consumer)
|
||||||
|
return db_consumer
|
||||||
|
|
||||||
|
|
||||||
# def create_producer(
|
def get_consumers(
|
||||||
# db: Session, producer: schemas.ProducerCreate
|
db: Session, skip: int = 0, limit: int = 100
|
||||||
# ) -> sql_models.Producer:
|
) -> List[sql_models.Consumer]:
|
||||||
# jsonblob_init = {"test_repo": "jsonblob_create"}
|
return db.query(sql_models.Consumer).offset(skip).limit(limit).all()
|
||||||
# db_producer = sql_models.Producer(jsonblob=jsonblob_init)
|
|
||||||
# db.add(db_producer)
|
|
||||||
# db.commit()
|
|
||||||
# db.refresh(db_producer)
|
|
||||||
# return db_producer
|
|
||||||
|
|
||||||
|
|
||||||
# def get_repositories(
|
def create_producer(
|
||||||
# db: Session, skip: int = 0, limit: int = 100
|
db: Session, producer: schemas.ProducerCreate
|
||||||
# ) -> List[sql_models.Repository]:
|
) -> sql_models.Producer:
|
||||||
# return db.query(sql_models.Repository).offset(skip).limit(limit).all()
|
db_producer = sql_models.Producer(**producer.dict())
|
||||||
|
db.add(db_producer)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_producer)
|
||||||
|
return db_producer
|
||||||
|
|
||||||
|
|
||||||
# def create_repository(
|
def get_producers(
|
||||||
# db: Session, repository: schemas.RepositoryCreate, producers_id: int
|
db: Session, skip: int = 0, limit: int = 100
|
||||||
# ) -> sql_models.Repository:
|
) -> List[sql_models.Producer]:
|
||||||
# db_repository = sql_models.Repository(**repository.dict(), prod_id=producers_id)
|
return db.query(sql_models.Producer).offset(skip).limit(limit).all()
|
||||||
# db.add(db_repository)
|
|
||||||
# db.commit()
|
|
||||||
# db.refresh(db_repository)
|
def create_repository(
|
||||||
# return db_repository
|
db: Session, repository: schemas.RepositoryCreate
|
||||||
|
) -> sql_models.Repository:
|
||||||
|
db_repository = sql_models.Repository(**repository.dict())
|
||||||
|
db.add(db_repository)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_repository)
|
||||||
|
return db_repository
|
||||||
|
|
||||||
|
|
||||||
|
def get_repositories(
|
||||||
|
db: Session, skip: int = 0, limit: int = 100
|
||||||
|
) -> List[sql_models.Repository]:
|
||||||
|
return db.query(sql_models.Repository).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
|
||||||
|
def get_repository_by_uuid(db: Session, uuid: str) -> Optional[sql_models.Repository]:
|
||||||
|
return (
|
||||||
|
db.query(sql_models.Repository)
|
||||||
|
.filter(sql_models.Repository.uuid == uuid)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_repository_by_did(
|
||||||
|
db: Session, did: str, skip: int = 0, limit: int = 100
|
||||||
|
) -> List[sql_models.Repository]:
|
||||||
|
return (
|
||||||
|
db.query(sql_models.Repository)
|
||||||
|
.filter(sql_models.Repository.entity_did == did)
|
||||||
|
.offset(skip)
|
||||||
|
.limit(limit)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,5 +1,16 @@
|
|||||||
from sqlalchemy import JSON, Boolean, Column, DateTime, ForeignKey, Integer, String
|
from sqlalchemy import (
|
||||||
|
JSON,
|
||||||
|
Boolean,
|
||||||
|
Column,
|
||||||
|
DateTime,
|
||||||
|
ForeignKey,
|
||||||
|
Integer,
|
||||||
|
String,
|
||||||
|
Text,
|
||||||
|
UniqueConstraint,
|
||||||
|
)
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
from .sql_db import Base
|
from .sql_db import Base
|
||||||
|
|
||||||
@@ -23,15 +34,15 @@ class Entity(Base):
|
|||||||
## Relations ##
|
## Relations ##
|
||||||
producers = relationship("Producer", back_populates="entity")
|
producers = relationship("Producer", back_populates="entity")
|
||||||
consumers = relationship("Consumer", back_populates="entity")
|
consumers = relationship("Consumer", back_populates="entity")
|
||||||
# repository = relationship("Repository", uselist=False, back_populates="entity")
|
repository = relationship("Repository", uselist=False, back_populates="entity")
|
||||||
|
|
||||||
|
|
||||||
class ProducerAbstract(Base):
|
class ProducerAbstract(Base):
|
||||||
__abstract__ = True
|
__abstract__ = True
|
||||||
|
|
||||||
# Queryable body
|
# Queryable body
|
||||||
id = Column(Integer, primary_key=True, index=True)
|
uuid = Column(Text(length=36), primary_key=True, index=True)
|
||||||
service_name = Column(String, unique=True, index=True)
|
service_name = Column(String, index=True)
|
||||||
service_type = Column(String, index=True)
|
service_type = Column(String, index=True)
|
||||||
endpoint_url = Column(String, index=True)
|
endpoint_url = Column(String, index=True)
|
||||||
status = Column(String, index=True)
|
status = Column(String, index=True)
|
||||||
@@ -49,7 +60,7 @@ class Producer(ProducerAbstract):
|
|||||||
## Relations ##
|
## Relations ##
|
||||||
# One entity can have many producers
|
# One entity can have many producers
|
||||||
entity = relationship("Entity", back_populates="producers")
|
entity = relationship("Entity", back_populates="producers")
|
||||||
entity_did = Column(Integer, ForeignKey("entities.did"))
|
entity_did = Column(String, ForeignKey("entities.did"))
|
||||||
|
|
||||||
# One producer has many consumers
|
# One producer has many consumers
|
||||||
consumers = relationship("Consumer", back_populates="producer")
|
consumers = relationship("Consumer", back_populates="producer")
|
||||||
@@ -61,22 +72,29 @@ class Consumer(Base):
|
|||||||
## Queryable body ##
|
## Queryable body ##
|
||||||
id = Column(Integer, primary_key=True, index=True)
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
|
||||||
|
## Non queryable body ##
|
||||||
|
other = Column(JSON)
|
||||||
|
|
||||||
## Relations ##
|
## Relations ##
|
||||||
# one entity can have many consumers
|
# one entity can have many consumers
|
||||||
entity = relationship("Entity", back_populates="consumers")
|
entity = relationship("Entity", back_populates="consumers")
|
||||||
entity_did = Column(Integer, ForeignKey("entities.did"))
|
entity_did = Column(String, ForeignKey("entities.did"))
|
||||||
|
|
||||||
# one consumer has one producer
|
# one consumer has one producer
|
||||||
producer = relationship("Producer", back_populates="consumers")
|
producer = relationship("Producer", back_populates="consumers")
|
||||||
producer_id = Column(Integer, ForeignKey("producers.id"))
|
producer_uuid = Column(String, ForeignKey("producers.uuid"))
|
||||||
|
|
||||||
|
__table_args__ = (UniqueConstraint("producer_uuid", "entity_did"),)
|
||||||
|
|
||||||
|
|
||||||
# class Repository(ProducerAbstract):
|
class Repository(ProducerAbstract):
|
||||||
# __tablename__ = "repositories"
|
__tablename__ = "repositories"
|
||||||
|
|
||||||
# # one repository has one entity
|
time_created = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
# entity = relationship("Entity", back_populates="repository")
|
|
||||||
# entity_did = Column(Integer, ForeignKey("entities.did"))
|
# one repository has one entity
|
||||||
|
entity = relationship("Entity", back_populates="repository")
|
||||||
|
entity_did = Column(Integer, ForeignKey("entities.did"))
|
||||||
|
|
||||||
|
|
||||||
# TODO: Ask how this works exactly
|
# TODO: Ask how this works exactly
|
||||||
|
|||||||
Reference in New Issue
Block a user