Skip to content

Commit

Permalink
Merge pull request #40 from enesonus/OVTF-131-dev
Browse files Browse the repository at this point in the history
OVTF-131-dev:Add Friend (Private/Friend Group) API
  • Loading branch information
enesonus authored Nov 18, 2023
2 parents 10e9062 + 695723f commit b33585b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
path('hello-github/', view=views.hello_github, name='hello-github'),
path('add-song-rating/', view=views.add_song_rating, name='add-song-rating'),
path('remove-friend', view=views.remove_friend, name = 'remove-friend'),
path('add-friend/', view=views.add_friend, name='add_friend'),


]
29 changes: 29 additions & 0 deletions apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,32 @@ def remove_friend(request, userid):
except Exception as e:
return JsonResponse({'error': str(e)}, status=400)

@csrf_exempt
@token_required
def add_friend(request, userid):
try:
if request.method == 'POST':
try:
user_id = request.POST.get('user_id')
friend_id = request.POST.get('friend_id')

user = User.objects.get(firebase_uid=user_id)
friend = User.objects.get(firebase_uid=friend_id)

# Check if the friendship already exists
if Friend.objects.filter(user=user, friend=friend).exists():
return JsonResponse({'detail': 'Friendship already exists'}, status=400)

# Create a new friend instance
Friend.objects.create(user=user, friend=friend)

return JsonResponse({'detail': 'Friend added successfully'}, status=200)

except User.DoesNotExist:
return JsonResponse({'detail': 'User or friend does not exist'}, status=404)

else:
return JsonResponse({'error': 'Invalid method'}, status=400)

except Exception as e:
return JsonResponse({'error': str(e)}, status=400)

0 comments on commit b33585b

Please sign in to comment.