diff --git a/src/api/info_api.py b/src/api/info_api.py new file mode 100644 index 0000000..5011075 --- /dev/null +++ b/src/api/info_api.py @@ -0,0 +1,26 @@ +from ninja import Router +from api.info_schema import AnnouncementSchema, SingleAnnouncementSchema +from announcement.models import Announcement +from api.utils import api_response + +# ============================ +# Info Endpoints +# ============================ +router = Router(tags=["info"]) + + +@router.get("/get_announcement", response=SingleAnnouncementSchema) +def get_announcement(request): + try: + announcement = Announcement.objects.last() + announcement_data = AnnouncementSchema.from_orm(announcement) + + return api_response( + success=True, + message="last announcement fetched successfully", + payload=announcement_data.dict(), + ) + except Exception as e: + return api_response( + success=False, message="Error occurd", error=e, status_code=503 + ) diff --git a/src/api/info_schema.py b/src/api/info_schema.py new file mode 100644 index 0000000..839f1c7 --- /dev/null +++ b/src/api/info_schema.py @@ -0,0 +1,21 @@ +from ninja import ModelSchema +from announcement.models import Announcement +from api.schema import ApiResponseSchema, DataSchema + + +class AnnouncementSchema(ModelSchema): + class Meta: + model = Announcement + fields = [ + "id", + "title", + "content", + ] + + +class SingleAnnouncementDataSchema(DataSchema): + payload: AnnouncementSchema + + +class SingleAnnouncementSchema(ApiResponseSchema): + data: SingleAnnouncementDataSchema diff --git a/src/api/urls.py b/src/api/urls.py index d12001e..f7b7814 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -6,6 +6,7 @@ from .auth_api import router as auth_router from .book_api import router as book_router from .author_api import router as author_router +from .info_api import router as info_router from ninja.errors import ValidationError,AuthenticationError from .utils import api_response @@ -36,6 +37,7 @@ def authemtication_errors(request, exc): api.add_router("/auth/",auth_router) api.add_router("/books/",book_router) api.add_router("/authors/",author_router) +api.add_router("/info/",info_router) urlpatterns = [ path("api/v1/", api.urls),