Skip to content

Commit

Permalink
implement announcment endpoints in info api
Browse files Browse the repository at this point in the history
  • Loading branch information
MrArsenic621 committed Nov 16, 2024
1 parent c5c53eb commit b9c9e88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/api/info_api.py
Original file line number Diff line number Diff line change
@@ -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
)
21 changes: 21 additions & 0 deletions src/api/info_schema.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit b9c9e88

Please sign in to comment.