|
1 | 1 | from django.shortcuts import render |
2 | 2 |
|
3 | | -# Create your views here. |
| 3 | +from rest_framework import generics, status |
| 4 | +from rest_framework.response import Response |
| 5 | +from rest_framework.views import APIView |
| 6 | +from .models import PomodoroSession |
| 7 | +from .serializers import PomodoroSessionSerializer |
| 8 | + |
| 9 | +class StartPomodoroSession(APIView): |
| 10 | + def post(self, request, id): |
| 11 | + session = PomodoroSession.objects.filter(id=id).first() |
| 12 | + if not session: |
| 13 | + return Response({"error": "Pomodoro session not found"}, status=status.HTTP_404_NOT_FOUND) |
| 14 | + |
| 15 | + session.start() |
| 16 | + return Response({"message": "Pomodoro session started"}, status=status.HTTP_200_OK) |
| 17 | + |
| 18 | +class PausePomodoroSession(APIView): |
| 19 | + def post(self, request, id): |
| 20 | + session = PomodoroSession.objects.filter(id=id).first() |
| 21 | + if not session: |
| 22 | + return Response({"error": "Pomodoro session not found"}, status=status.HTTP_404_NOT_FOUND) |
| 23 | + |
| 24 | + session.pause() |
| 25 | + return Response({"message": "Pomodoro session paused"}, status=status.HTTP_200_OK) |
| 26 | + |
| 27 | +class ResumePomodoroSession(APIView): |
| 28 | + def post(self, request, id): |
| 29 | + session = PomodoroSession.objects.filter(id=id).first() |
| 30 | + if not session: |
| 31 | + return Response({"error": "Pomodoro session not found"}, status=status.HTTP_404_NOT_FOUND) |
| 32 | + |
| 33 | + session.resume() |
| 34 | + return Response({"message": "Pomodoro session resumed"}, status=status.HTTP_200_OK) |
| 35 | + |
| 36 | +class CancelPomodoroSession(APIView): |
| 37 | + def post(self, request, id): |
| 38 | + session = PomodoroSession.objects.filter(id=id).first() |
| 39 | + if not session: |
| 40 | + return Response({"error": "Pomodoro session not found"}, status=status.HTTP_404_NOT_FOUND) |
| 41 | + |
| 42 | + session.cancel() |
| 43 | + return Response({"message": "Pomodoro session canceled"}, status=status.HTTP_200_OK) |
| 44 | + |
| 45 | +class CompletePomodoroSession(APIView): |
| 46 | + def post(self, request, id): |
| 47 | + session = PomodoroSession.objects.filter(id=id).first() |
| 48 | + if not session: |
| 49 | + return Response({"error": "Pomodoro session not found"}, status=status.HTTP_404_NOT_FOUND) |
| 50 | + |
| 51 | + session.complete() |
| 52 | + return Response({"message": "Pomodoro session completed"}, status=status.HTTP_200_OK) |
| 53 | + |
| 54 | +class GetPomodoroSessionStatus(APIView): |
| 55 | + def get(self, request, id): |
| 56 | + session = PomodoroSession.objects.filter(id=id).first() |
| 57 | + if not session: |
| 58 | + return Response({"error": "Pomodoro session not found"}, status=status.HTTP_404_NOT_FOUND) |
| 59 | + |
| 60 | + serializer = PomodoroSessionSerializer(session) |
| 61 | + return Response(serializer.data, status=status.HTTP_200_OK) |
| 62 | + |
| 63 | +class GetPomodoroSessionHistory(generics.ListAPIView): |
| 64 | + queryset = PomodoroSession.objects.all() |
| 65 | + serializer_class = PomodoroSessionSerializer |
0 commit comments