Skip to content

Commit

Permalink
feat(api): update plugin api
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinesmilelk authored and BroKun committed Jul 15, 2024
1 parent 9a2e7b3 commit cbbe593
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
56 changes: 56 additions & 0 deletions api/alembic/versions/f3069f28699a_update_plugin_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""update plugin table
Revision ID: f3069f28699a
Revises: ac21c38c5e56
Create Date: 2024-07-11 04:20:39.013033
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'f3069f28699a'
down_revision: Union[str, None] = 'ac21c38c5e56'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('plugin', sa.Column(
'name', sa.String(length=255), nullable=False))
op.add_column('plugin', sa.Column(
'avatar', sa.String(length=255), nullable=True))
op.add_column('plugin', sa.Column('description', sa.Text(), nullable=True))
op.add_column('plugin_api', sa.Column(
'name', sa.String(length=255), nullable=True))
op.add_column('plugin_config', sa.Column(
'plugin_openapi_desc', sa.Text(), nullable=True))
op.drop_column('plugin_config', 'avatar')
op.drop_column('plugin_config', 'description')
op.drop_column('plugin_config', 'name')
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('plugin_config', sa.Column('name', sa.VARCHAR(
length=255), autoincrement=False, nullable=False))
op.add_column('plugin_config', sa.Column(
'description', sa.TEXT(), autoincrement=False, nullable=True))
op.add_column('plugin_config', sa.Column('avatar', sa.VARCHAR(
length=255), autoincrement=False, nullable=True))
op.drop_column('plugin_config', 'plugin_openapi_desc')
op.drop_column('plugin_api', 'name')
op.drop_column('plugin', 'description')
op.drop_column('plugin', 'avatar')
op.drop_column('plugin', 'name')
op.alter_column('messages', 'message_type',
existing_type=sa.Enum(
'MARKDOWN', 'TEXT', name='messagetype'),
type_=sa.VARCHAR(length=50),
existing_nullable=False)
# ### end Alembic commands ###
18 changes: 17 additions & 1 deletion api/routers/plugin/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ def get(session: Session, plugin_id: int) -> PluginORM | None:
return session.query(PluginORM).filter(PluginORM.id == plugin_id).one_or_none()

@staticmethod
def get_all(session: Session, user_id: int) -> Page[PluginModel]:
def get_user_plugin(session: Session, user_id: int) -> Page[PluginModel]:
print('user_id_test', user_id)
return paginate(
session.query(PluginORM)
.filter(PluginORM.created_by == user_id)
.order_by(PluginORM.updated_at.desc())
)

@staticmethod
def get_all_plugin(session: Session) -> Page[PluginModel]:
return paginate(
session.query(PluginORM)
.order_by(PluginORM.updated_at.desc())
)

@staticmethod
def update(session: Session, operator: int, plugin_model: PluginUpdate) -> int:
plugin_id = plugin_model.id
Expand Down Expand Up @@ -69,6 +77,14 @@ def get_plugin_draft(session: Session, plugin_id: int) -> PluginConfigORM | None
PluginConfigORM.is_draft == True
).one_or_none()

@staticmethod
def get_or_create_plugin_draft(session: Session, operator: int, plugin_id: int,) -> PluginConfigORM:
exist = PluginConfigHelper.get_plugin_draft(session, plugin_id)
if exist is None:
return PluginConfigHelper.create(session, operator, PluginConfigCreate(plugin_id=plugin_id, plugin_openapi_desc='', is_draft=True))
else:
return exist

@staticmethod
def create(session: Session, operator: int, config_model: PluginConfigCreate) -> PluginConfigORM:
now = datetime.now()
Expand Down
8 changes: 7 additions & 1 deletion api/routers/plugin/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ def create_plugin(user_id: int, plugin: PluginCreate, session: Session = Depends

@router.get("/", response_model=Page[PluginModel])
def get_plugins(user_id: int, session: Session = Depends(get_db)):
data = PluginHelper.get_all(session, user_id)
data = PluginHelper.get_user_plugin(session, user_id)
return data


@router.get("/", response_model=Page[PluginModel])
def get_all_plugins(session: Session = Depends(get_db)):
data = PluginHelper.get_all_plugin(session)
return data


Expand Down

0 comments on commit cbbe593

Please sign in to comment.