Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

529 create post endpoint #534

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions backend/server/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.contrib.auth.models import User
from rest_framework import serializers
from server.models import Opportunity, OpportunityXMeetings, OpportunityXTech

from server.models import Opportunity, OpportunityXMeetings, OpportunityXTech, Post

class UserSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -27,3 +26,9 @@ class OpportunityXTechSerializer(serializers.ModelSerializer):
class Meta:
model = OpportunityXTech
fields = "__all__"


class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = "__all__"
20 changes: 20 additions & 0 deletions backend/server/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.test import TestCase
from rest_framework.test import RequestsClient
from server.models import Post


HOST = "http://localhost:8000"
BASE_URI = "/api"
Expand Down Expand Up @@ -73,3 +75,21 @@ def test_healthcheck(self):
response = self.client.get(f"{HOST}{BASE_URI}{self.uri}", format="json")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["message"], "healthcheck")

# This test failed with a 400 code even though the CreatePost view worked as intended, remove this comment if test works
class CreatePostTest(TestCase):
def test_create_post(self):
data = {
"role": "Developer",
"subrole": "Backend",
"project": "Project X",
"meetings_times": ["Monday 10am", "Wednesday 5pm"],
"difficulty_level": 3,
"details": {"description": "Project description and requirement..."},
"updated_by_or_token": "user123"
}
print(f"{HOST}{BASE_URI}/create_post")
response = self.client.post("/api/create_post", data, format='json')

self.assertEqual(response.status_code, 201)
self.assertTrue(Post.objects.filter(role="Developer").exists())
1 change: 1 addition & 0 deletions backend/server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
),
name="swagger-ui",
),
path('create_post', views.CreatePost.as_view(), name='create_post'),
]
15 changes: 12 additions & 3 deletions backend/server/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib.auth.models import User
from rest_framework import generics, permissions, renderers, viewsets
from rest_framework import generics, permissions, renderers, viewsets, status
from rest_framework.response import Response
from rest_framework.views import APIView
from server.models import Opportunity
from server.serializers import OpportunitySerializer, UserSerializer
from server.serializers import OpportunitySerializer, UserSerializer, PostSerializer
import time
from django.conf import settings

Expand Down Expand Up @@ -40,4 +40,13 @@ def get(self, request):
"uptime": f"{uptime_hours:.2f} hours",
# "uptime": f"{uptime_seconds:.1f} seconds",
"version": settings.VERSION
})
})

class CreatePost(APIView):
def post(self, request, format=None):
serializer = PostSerializer(data = request.data)
if serializer.is_valid():
print(serializer.validated_data)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)