diff --git a/src/chigame/api/serializers.py b/src/chigame/api/serializers.py index 9c464b62..b9a344bf 100644 --- a/src/chigame/api/serializers.py +++ b/src/chigame/api/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from chigame.games.models import Chat, Game, Lobby, Message, Tournament, User +from chigame.games.models import Category, Chat, Game, Lobby, Mechanic, Message, Tournament, User from chigame.users.models import Group @@ -45,6 +45,18 @@ def create(self, validated_data): return user +class CategorySerializer(serializers.ModelSerializer): + class Meta: + model = Category + fields = "__all__" + + +class MechanicSerializer(serializers.ModelSerializer): + class Meta: + model = Mechanic + fields = "__all__" + + class MessageSerializer(serializers.ModelSerializer): sender = serializers.EmailField(write_only=True) diff --git a/src/chigame/api/urls.py b/src/chigame/api/urls.py index 3a845ed8..e933692b 100644 --- a/src/chigame/api/urls.py +++ b/src/chigame/api/urls.py @@ -1,24 +1,41 @@ -from django.urls import path +from django.urls import include, path from . import views +game_patterns = [ + path("", views.GameListView.as_view(), name="api-game-list"), + path("/", views.GameDetailView.as_view(), name="api-game-detail"), + path("/categories/", views.GameCategoriesAPIView.as_view(), name="api-game-categories"), + path("/mechanics/", views.GameMechanicsAPIView.as_view(), name="api-game-mechanics"), +] + +lobby_patterns = [ + path("", views.LobbyListView.as_view(), name="api-lobby-list"), + path("/", views.LobbyDetailView.as_view(), name="api-lobby-detail"), +] + +user_patterns = [ + path("", views.UserListView.as_view(), name="api-user-list"), + path("/", views.UserDetailView.as_view(), name="api-user-detail"), + path("/groups/", views.UserGroupsView.as_view(), name="api-user-groups"), + path("/friends/", views.UserFriendsAPIView.as_view(), name="api-user-friends"), +] + +tournament_patterns = [ + path("chat/", views.MessageView.as_view(), name="api-chat-list"), + path("chat/feed/", views.MessageFeedView.as_view(), name="api-chat-detail"), +] + +group_patterns = [ + path("", views.GroupListView.as_view(), name="api-group-list"), + path("/", views.GroupDetailView.as_view(), name="api-group-detail"), + path("/members/", views.GroupMembersView.as_view(), name="api-group-members"), +] + urlpatterns = [ - # GAME API URLS - path("games/", views.GameListView.as_view(), name="api-game-list"), - path("games//", views.GameDetailView.as_view(), name="api-game-detail"), - # LOBBY API URLS - path("lobbies/", views.LobbyListView.as_view(), name="api-lobby-list"), - path("lobbies//", views.LobbyDetailView.as_view(), name="api-lobby-detail"), - # USER API URLS - path("users/", views.UserListView.as_view(), name="api-user-list"), - path("users//", views.UserDetailView.as_view(), name="api-user-detail"), - path("users//friends/", views.UserFriendsAPIView.as_view(), name="api-user-friends"), - path("users//groups/", views.UserGroupsView.as_view(), name="api-user-groups"), - # CHAT API URLS - path("tournaments/chat/", views.MessageView.as_view(), name="api-chat-list"), - path("tournaments/chat/feed/", views.MessageFeedView.as_view(), name="api-chat-detail"), - # GROUP API URLS - path("groups/", views.GroupListView.as_view(), name="api-group-list"), - path("groups//", views.GroupDetailView.as_view(), name="api-group-detail"), - path("groups//members/", views.GroupMembersView.as_view(), name="api-group-members"), + path("games/", include(game_patterns)), + path("lobbies/", include(lobby_patterns)), + path("users/", include(user_patterns)), + path("tournaments/", include(tournament_patterns)), + path("groups/", include(group_patterns)), ] diff --git a/src/chigame/api/views.py b/src/chigame/api/views.py index 74c85756..8d23444b 100644 --- a/src/chigame/api/views.py +++ b/src/chigame/api/views.py @@ -8,9 +8,11 @@ from chigame.api.filters import GameFilter from chigame.api.serializers import ( + CategorySerializer, GameSerializer, GroupSerializer, LobbySerializer, + MechanicSerializer, MessageFeedSerializer, MessageSerializer, UserSerializer, @@ -42,6 +44,26 @@ class GameDetailView(generics.RetrieveUpdateDestroyAPIView): serializer_class = GameSerializer +class GameCategoriesAPIView(generics.ListAPIView): + serializer_class = CategorySerializer + pagination_class = PageNumberPagination + + def get_queryset(self): + game_id = self.kwargs["pk"] + game = Game.objects.get(id=game_id) + return game.categories.all() + + +class GameMechanicsAPIView(generics.ListAPIView): + serializer_class = MechanicSerializer + pagination_class = PageNumberPagination + + def get_queryset(self): + game_id = self.kwargs["pk"] + game = Game.objects.get(id=game_id) + return game.mechanics.all() + + class UserFriendsAPIView(generics.RetrieveAPIView): serializer_class = UserSerializer