Skip to content

Commit

Permalink
✨ feat: Implemented the deletion of the service
Browse files Browse the repository at this point in the history
  • Loading branch information
xitowzys committed Feb 27, 2023
1 parent 00bf83f commit 06ca61f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 8 deletions.
37 changes: 37 additions & 0 deletions fastapi_gateway_auto_generate/database/DeleteService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from loguru import logger
from ..management.models import DeleteService as delete_service_model
from .models import Services

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from ..Config import Config


class DeleteService():

def __init__(self, db_url: str) -> None:
Session: sessionmaker = sessionmaker(
bind=create_engine(db_url))
self.__session = Session()

def delete_service(self, delete_service_model: delete_service_model) -> bool:

# Добавить проверку существование сервиса [Error Code 1]

try:
# service: Services = Services(
# domain=str(add_service_model.domain),
# port=add_service_model.port,
# name=add_service_model.name_service
# )

service = self.__session.query(Services).filter_by(id=delete_service_model.id_service).first()
# print(delete_service_model.id_service)
self.__session.delete(service)
self.__session.commit()

self.__session.close()
return True
except Exception as e:
logger.debug(f"Ошибка: {str(e)}")
return False
38 changes: 38 additions & 0 deletions fastapi_gateway_auto_generate/database/SetMarkDeleteSerivce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from loguru import logger
from ..management.models import DeleteService as delete_service_model
from .models import Services

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from ..Config import Config


class SetMarkDeleteService():

def __init__(self, db_url: str) -> None:
Session: sessionmaker = sessionmaker(
bind=create_engine(db_url))
self.__session = Session()

def set_mark_delete_service(self, delete_service_model: delete_service_model) -> bool:

# Добавить проверку существование сервиса [Error Code 1]

try:
# service: Services = Services(
# domain=str(add_service_model.domain),
# port=add_service_model.port,
# name=add_service_model.name_service
# )

service = self.__session.query(Services).filter_by(id=delete_service_model.id_service).first()
service.delete = True
# print(delete_service_model.id_service)
# self.__session.delete(service)
self.__session.commit()

self.__session.close()
return True
except Exception as e:
logger.debug(f"Ошибка: {str(e)}")
return False
6 changes: 5 additions & 1 deletion fastapi_gateway_auto_generate/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
from .GetAllServices import GetAllServices
from .StatusService import StatusService
from .UrlService import UrlService
from .DeleteService import DeleteService
from .SetMarkDeleteSerivce import SetMarkDeleteService

__all__: list[str] = [
"AddService",
"GetAllServices",
"StatusService",
"UrlService"
"UrlService",
"DeleteService",
"SetMarkDeleteService"
]
10 changes: 6 additions & 4 deletions fastapi_gateway_auto_generate/database/models/Services.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from ..SQLAlchemy.declarative_base import Base

from sqlalchemy import Column, INTEGER, TEXT
from sqlalchemy import Column, INTEGER, TEXT, BOOLEAN
from sqlalchemy.orm import relationship, backref
from typing import Any
from loguru import logger
Expand All @@ -14,12 +14,13 @@ class Services(Base):
domain: Column = Column(TEXT, nullable=False)
port: Column = Column(INTEGER, nullable=False)
name: Column = Column(TEXT, nullable=False, unique=True)
delete: Column = Column(BOOLEAN, nullable=False, default=False)

urls: Any = relationship(
"UrlServices", backref=backref("services"))
"UrlServices", cascade='all,delete', backref=backref("services"))

status: Any = relationship(
"StatusServices", backref=backref("services"))
"StatusServices", cascade='all,delete', backref=backref("services"))

def obj_to_dict(self) -> dict[str, Column]:

Expand All @@ -40,5 +41,6 @@ def obj_to_dict(self) -> dict[str, Column]:
"port": self.port,
"name": self.name,
"urls": urls,
"status-code": status_code
"status-code": status_code,
"delete": self.delete
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def execute(self, services_result: list[dict[str, Any]], fast_api_app: FastAPI)
dependencies=route_model.dependencies,
)(f=func)

UpdateOpenApiSchemaUsecase().execute(fast_api_app=fast_api_app)
UpdateOpenApiSchemaUsecase().execute(fast_api_app=fast_api_app)

DeleteTmpModelsFilesUsecase().execute()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from fastapi_gateway_auto_generate.database import StatusService, UrlService, GetAllServices
from fastapi_gateway_auto_generate.database import StatusService, UrlService, GetAllServices, DeleteService
from . import *
from ...Config import Config
from loguru import logger
from ..models import RouteModel
from fastapi_gateway_auto_generate.management.models import GetAllInfoServices
from fastapi_gateway_auto_generate.management.models import DeleteService as delete_service_model


class RefreshServicesUsecase:
Expand All @@ -15,6 +16,7 @@ def execute(self, config: Config) -> None:
get_all_info_services_model: GetAllInfoServices = GetAllInfoServices(
page=1)


services, err = GetAllServices(db_url=config.db_url).get_all_services(
get_all_info_services_model=get_all_info_services_model)

Expand All @@ -33,6 +35,12 @@ def execute(self, config: Config) -> None:
del config.fast_api_app.routes[i]
logger.debug(r)

if service['delete']:
# print(service['id'])
DeleteService(db_url=config.db_url).delete_service(
delete_service_model=delete_service_model(id_service=service['id'])
)

services_result = BuildRouteModelsUsecase().execute(
config=config
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from fastapi_gateway_auto_generate.database import SetMarkDeleteService as set_mark_delete_service_database
from fastapi import APIRouter, Depends
from ..models import DeleteService
from ...Config import Config
Expand All @@ -10,4 +11,6 @@ def __init__(self, config: Config) -> None:

@self.route.delete("/service", tags=["Service management"])
async def delete_service(delete_service: DeleteService = Depends()) -> dict[str, str]:
return {"data": "Hello World"}
result = set_mark_delete_service_database(db_url=self.__config.db_url).set_mark_delete_service(
delete_service_model=delete_service)
return result
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""initial
Revision ID: 239ac7daedc7
Revises: b462f70d947f
Create Date: 2023-02-28 01:56:31.995973
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '239ac7daedc7'
down_revision = 'b462f70d947f'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('services', sa.Column('delete', sa.BOOLEAN(), nullable=False))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('services', 'delete')
# ### end Alembic commands ###

0 comments on commit 06ca61f

Please sign in to comment.