Skip to content

Commit

Permalink
fix: backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Shavkatjon-O committed Aug 28, 2024
1 parent e8cfed8 commit 6439b92
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 36 deletions.
2 changes: 0 additions & 2 deletions apps/chats/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.db import models
from django.contrib.auth import get_user_model

from apps.common.models import BaseModel

User = get_user_model()
Expand All @@ -18,7 +17,6 @@ def __str__(self):
class Message(BaseModel):
chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name="messages")
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="messages")

content = models.TextField()

def __str__(self):
Expand Down
24 changes: 20 additions & 4 deletions apps/chats/serializers.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
from rest_framework import serializers
from .models import Chat, Message
from django.contrib.auth import get_user_model

User = get_user_model()


class MessageSerializer(serializers.ModelSerializer):
user = serializers.StringRelatedField()

class Meta:
model = Message
fields = [
fields = (
"id",
"chat",
"user",
"content",
]
)


class ChatSerializer(serializers.ModelSerializer):
users = serializers.StringRelatedField(many=True)

class Meta:
model = Chat
fields = [
fields = (
"id",
"title",
"is_group",
"users",
]
)


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
"id",
"email",
)
35 changes: 21 additions & 14 deletions apps/chats/urls.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from . import views

# router = DefaultRouter()
# router.register(r"chats", views.ChatViewSet)
# router.register(r"messages", views.MessageViewSet)

# urlpatterns = [
# path("", include(router.urls)),
# ]

from django.urls import path
from apps.chats import views

urlpatterns = [
path("chats/<int:pk>/", views.ChatViewSet.as_view(), name="chat"),
path("messages/<int:pk>/", views.MessageViewSet.as_view(), name="message"),
path("users/", views.UserListView.as_view(), name="users-list"),
path("chats/", views.ChatListView.as_view(), name="chats-list"),
path("chats/create/", views.ChatCreateView.as_view(), name="chat-create"),
path("chats/<int:pk>/", views.ChatDetailView.as_view(), name="chat-detail"),
path(
"chats/<int:chat_id>/messages/",
views.MessageListView.as_view(),
name="messages-list",
),
path(
"chats/<int:chat_id>/messages/create/",
views.MessageCreateView.as_view(),
name="message-create",
),
path(
"chats/<int:chat_id>/messages/<int:pk>/",
views.MessageDetailView.as_view(),
name="message-detail",
),
]
62 changes: 46 additions & 16 deletions apps/chats/views.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
from rest_framework import viewsets
from .models import Chat, Message
from .serializers import ChatSerializer, MessageSerializer
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework import generics
from apps.chats import models, serializers


# class ChatViewSet(RetrieveAPIView):
# queryset = Chat.objects.all()
# serializer_class = ChatSerializer
class UserListView(generics.ListAPIView):
queryset = models.User.objects.all()
serializer_class = serializers.UserSerializer


class ChatViewSet(RetrieveAPIView):
queryset = Chat.objects.all()
serializer_class = ChatSerializer
class ChatListView(generics.ListAPIView):
queryset = models.Chat.objects.all()
serializer_class = serializers.ChatSerializer

def get_queryset(self):
return self.queryset.filter(users=self.request.user)


class ChatCreateView(generics.CreateAPIView):
queryset = models.Chat.objects.all()
serializer_class = serializers.ChatSerializer

def perform_create(self, serializer):
serializer.save(users=[self.request.user])

class MessageViewSet(ListAPIView):
queryset = Message.objects.all()
serializer_class = MessageSerializer

class ChatDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset = models.Chat.objects.all()
serializer_class = serializers.ChatSerializer

def get_queryset(self):
chat_id = self.kwargs["chat_id"]
return self.queryset.filter(users=self.request.user)


print(self.queryset.filter(chat__id=chat_id))
return self.queryset.filter(chat__id=chat_id)
class MessageListView(generics.ListAPIView):
queryset = models.Message.objects.all()
serializer_class = serializers.MessageSerializer

def get_queryset(self):
return self.queryset.filter(chat=self.kwargs["chat_id"])


class MessageCreateView(generics.CreateAPIView):
queryset = models.Message.objects.all()
serializer_class = serializers.MessageSerializer

def perform_create(self, serializer):
serializer.save(user=self.request.user)


class MessageDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset = models.Message.objects.all()
serializer_class = serializers.MessageSerializer

def get_queryset(self):
return self.queryset.filter(chat=self.kwargs["chat_id"])

0 comments on commit 6439b92

Please sign in to comment.