From 6ad4f3a361bf4ee92350dae60ae2f13537d71f2e Mon Sep 17 00:00:00 2001 From: Tsung-Ju Lii Date: Wed, 23 Aug 2023 23:36:15 +0800 Subject: [PATCH] fix dummy view --- .../web/api/dummy/views.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/api/dummy/views.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/api/dummy/views.py index 365caed..5ec243c 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/api/dummy/views.py +++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/api/dummy/views.py @@ -2,7 +2,9 @@ from fastapi import APIRouter from fastapi.param_functions import Depends +{%- if cookiecutter.db_info.name != "mongodb" %} from {{cookiecutter.project_name}}.db.dao.dummy_dao import DummyDAO +{%- endif %} from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel from {{cookiecutter.project_name}}.web.api.dummy.schema import (DummyModelDTO, DummyModelInputDTO) @@ -14,28 +16,44 @@ async def get_dummy_models( limit: int = 10, offset: int = 0, +{%- if cookiecutter.db_info.name != "mongodb" %} dummy_dao: DummyDAO = Depends(), +{%- endif %} ) -> List[DummyModel]: """ Retrieve all dummy objects from the database. :param limit: limit of dummy objects, defaults to 10. :param offset: offset of dummy objects, defaults to 0. +{%- if cookiecutter.db_info.name != "mongodb" %} :param dummy_dao: DAO for dummy models. +{%- endif %} :return: list of dummy objects from database. """ +{%- if cookiecutter.db_info.name != "mongodb" %} return await dummy_dao.get_all_dummies(limit=limit, offset=offset) +{%- else %} + return await DummyModel.find_all(skip=offset, limit=limit).to_list() +{%- endif %} @router.put("/") async def create_dummy_model( new_dummy_object: DummyModelInputDTO, +{%- if cookiecutter.db_info.name != "mongodb" %} dummy_dao: DummyDAO = Depends(), +{%- endif %} ) -> None: """ Creates dummy model in the database. :param new_dummy_object: new dummy model item. +{%- if cookiecutter.db_info.name != "mongodb" %} :param dummy_dao: DAO for dummy models. +{%- endif %} """ +{%- if cookiecutter.db_info.name != "mongodb" %} await dummy_dao.create_dummy_model(name=new_dummy_object.name) +{%- else %} + await DummyModel.insert_one(DummyModel(name=new_dummy_object.name)) +{%- endif %}