Skip to content

Commit

Permalink
Merge pull request #295 from uchicago-cs/apis/update-game-nested-endp…
Browse files Browse the repository at this point in the history
…oints

Added Nested Endpoints to GET information about Game model (specifically `Categories` and `Mechanics` information)
  • Loading branch information
majorsylvie authored Dec 6, 2023
2 parents b142257 + 44c8469 commit 7303ed3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
14 changes: 13 additions & 1 deletion src/chigame/api/serializers.py
Original file line number Diff line number Diff line change
@@ -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


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

Expand Down
55 changes: 36 additions & 19 deletions src/chigame/api/urls.py
Original file line number Diff line number Diff line change
@@ -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("<int:pk>/", views.GameDetailView.as_view(), name="api-game-detail"),
path("<int:pk>/categories/", views.GameCategoriesAPIView.as_view(), name="api-game-categories"),
path("<int:pk>/mechanics/", views.GameMechanicsAPIView.as_view(), name="api-game-mechanics"),
]

lobby_patterns = [
path("", views.LobbyListView.as_view(), name="api-lobby-list"),
path("<int:pk>/", views.LobbyDetailView.as_view(), name="api-lobby-detail"),
]

user_patterns = [
path("", views.UserListView.as_view(), name="api-user-list"),
path("<slug:slug>/", views.UserDetailView.as_view(), name="api-user-detail"),
path("<slug:slug>/groups/", views.UserGroupsView.as_view(), name="api-user-groups"),
path("<int:pk>/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("<int:pk>/", views.GroupDetailView.as_view(), name="api-group-detail"),
path("<int:pk>/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/<int:pk>/", views.GameDetailView.as_view(), name="api-game-detail"),
# LOBBY API URLS
path("lobbies/", views.LobbyListView.as_view(), name="api-lobby-list"),
path("lobbies/<int:pk>/", views.LobbyDetailView.as_view(), name="api-lobby-detail"),
# USER API URLS
path("users/", views.UserListView.as_view(), name="api-user-list"),
path("users/<slug:slug>/", views.UserDetailView.as_view(), name="api-user-detail"),
path("users/<int:pk>/friends/", views.UserFriendsAPIView.as_view(), name="api-user-friends"),
path("users/<slug:slug>/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/<int:pk>/", views.GroupDetailView.as_view(), name="api-group-detail"),
path("groups/<int:pk>/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)),
]
22 changes: 22 additions & 0 deletions src/chigame/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

from chigame.api.filters import GameFilter
from chigame.api.serializers import (
CategorySerializer,
GameSerializer,
GroupSerializer,
LobbySerializer,
MechanicSerializer,
MessageFeedSerializer,
MessageSerializer,
UserSerializer,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 7303ed3

Please sign in to comment.