Skip to content

Commit

Permalink
Merge pull request #44 from enesonus/OVTF-125
Browse files Browse the repository at this point in the history
OVTF-125: Edit rating of a Song API
  • Loading branch information
enesonus authored Nov 18, 2023
2 parents 397af4f + f8d41b9 commit a1a522c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
path('update-user/', view=views.update_user, name='update-user'),
path('hello-github/', view=views.hello_github, name='hello-github'),
path('add-song-rating/', view=views.add_song_rating, name='add-song-rating'),
path('edit-song-rating/', view=views.edit_song_rating, name='edit-song-rating'),


]
51 changes: 48 additions & 3 deletions apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from users.models import (User, UserSongRating,)



# Create endpoints

@csrf_exempt
Expand Down Expand Up @@ -101,6 +102,7 @@ def delete_user(request, userid):
return HttpResponse(status=404)



@csrf_exempt
@token_required
def update_user(request, userid):
Expand All @@ -126,17 +128,17 @@ def add_song_rating(request, userid):

if userid is None or song_id is None or rating is None or rating_date is None:
return JsonResponse({'error': 'Missing parameter'}, status=400)

try:
user = User.objects.get(firebase_uid=userid)
except User.DoesNotExist:
return JsonResponse({'error': 'User not found'}, status=404)

try:
song = Song.objects.get(song_id=song_id)
except Song.DoesNotExist:
return JsonResponse({'error': 'Song not found'}, status=404)

try:
user_rating, created_rating = UserSongRating.objects.get_or_create(user=user, song=song, rating=rating, date_rated=rating_date)

Expand All @@ -159,3 +161,46 @@ def add_song_rating(request, userid):
@token_required
def hello_github(request):
return JsonResponse({'message': 'Hello from Ovatify Team!'}, status=200)


@csrf_exempt
@token_required
def edit_song_rating(request, userid):
try:
if request.method == 'PUT':
data = request.data
song_id = data.get('song_id')
rating = data.get('rating')
rating_date = datetime.now()

if userid is None or song_id is None or rating is None or rating_date is None:
return JsonResponse({'error': 'Missing parameter'}, status=400)

try:
user = User.objects.get(firebase_uid=userid)
except User.DoesNotExist:
return JsonResponse({'error': 'User not found'}, status=404)

try:
song = Song.objects.get(song_id=song_id)
except Song.DoesNotExist:
return JsonResponse({'error': 'Song not found'}, status=404)

try:
user_rating = UserSongRating.objects.get(user=user, song=song)
except UserSongRating.DoesNotExist:
return JsonResponse({'error': 'User rating not found'}, status=404)

user_rating.rating = rating
user_rating.date_rated = rating_date
user_rating.save()

return JsonResponse({'message': 'User rating updated successfully'}, status=200)
else:
return JsonResponse({'error': 'Invalid method'}, status=400)
except KeyError as e:
logging.error(f"A KeyError occurred: {str(e)}")
return JsonResponse({'error': 'KeyError occurred'}, status=500)
except Exception as e:
logging.error(f"An unexpected error occurred: {str(e)}")
return JsonResponse({'error': 'An unexpected error occurred'}, status=500)

0 comments on commit a1a522c

Please sign in to comment.