diff --git a/backend/app/migrations/0002_wordbookmark.py b/backend/app/migrations/0002_wordbookmark.py new file mode 100644 index 00000000..fc54dcc6 --- /dev/null +++ b/backend/app/migrations/0002_wordbookmark.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2.17 on 2024-12-11 17:03 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('app', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='WordBookmark', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bookmarked_words', to=settings.AUTH_USER_MODEL)), + ('word', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bookmarks', to='app.word')), + ], + options={ + 'unique_together': {('user', 'word')}, + }, + ), + ] diff --git a/backend/app/migrations/0003_alter_wordbookmark_word.py b/backend/app/migrations/0003_alter_wordbookmark_word.py new file mode 100644 index 00000000..2329712f --- /dev/null +++ b/backend/app/migrations/0003_alter_wordbookmark_word.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.17 on 2024-12-11 17:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0002_wordbookmark'), + ] + + operations = [ + migrations.AlterField( + model_name='wordbookmark', + name='word', + field=models.CharField(max_length=255), + ), + ] diff --git a/backend/app/models.py b/backend/app/models.py index 87fbf57c..b9bb3733 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -148,7 +148,18 @@ class Word(models.Model): def __str__(self): return self.word + + +class WordBookmark(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bookmarked_words') + word = models.CharField(max_length=255) + class Meta: + unique_together = ('user', 'word') + + def __str__(self): + return f"{self.user.username} bookmarked {self.word.word}" + class Relationship(models.Model): word = models.ForeignKey(Word, on_delete=models.CASCADE, related_name="relationships") related_word = models.ForeignKey(Word, on_delete=models.CASCADE, related_name="related_to") diff --git a/backend/app/urls.py b/backend/app/urls.py index df02eeda..75aef604 100644 --- a/backend/app/urls.py +++ b/backend/app/urls.py @@ -1,6 +1,6 @@ from django.urls import path from app.views import * -from app.views_directory.wordviews import get_turkish_translation, get_lexvo_info, get_word_meanings, fetch_english_words +from app.views_directory.wordviews import get_turkish_translation, get_lexvo_info, get_word_meanings, fetch_english_words, bookmark_word, unbookmark_word, get_bookmarked_words from app.views_directory.profileviews import view_profile, update_profile, view_other_profile, view_followers, view_following from app.views_directory.follow_unfollow import follow_user, unfollow_user from app.views_directory.authentication_endpoints import RegisterView, LoginView, LogoutView, RefreshTokenView @@ -38,6 +38,10 @@ path('quiz/recommend//', quiz_views.get_quiz_recommendations, name="recommend_quiz"), path('quiz/review_latest//', quiz_views.get_latest_quiz_review, name="review_latest_quiz"), + path('word/bookmark//', bookmark_word, name='bookmark_word'), + path('word/unbookmark//', unbookmark_word, name='unbookmark_word'), + path('word/bookmarks/', get_bookmarked_words, name='get_bookmarked_words'), + path('create-post/',create_post, name='create_post'), path('signup/', RegisterView.as_view(), name='auth_register'), diff --git a/backend/app/views_directory/wordviews.py b/backend/app/views_directory/wordviews.py index 22818536..07d164ef 100644 --- a/backend/app/views_directory/wordviews.py +++ b/backend/app/views_directory/wordviews.py @@ -17,11 +17,63 @@ from ..serializers import * from django.shortcuts import render from ..models import Tags -from ..models import Quiz, Relationship, Word, Translation +from ..models import Quiz, Relationship, Word, Translation, WordBookmark from ..word_service import lexvo_manager import requests +@api_view(['POST']) +@permission_classes([IsAuthenticated]) +def bookmark_word(request, word): + """ + Bookmark a word for the user. + """ + try: + user = request.user + bookmark, created = WordBookmark.objects.get_or_create(user=user, word=word) + if not created: + return Response({"error": "Word already bookmarked"}, status=status.HTTP_400_BAD_REQUEST) + + return Response({"message": "Word bookmarked successfully"}, status=status.HTTP_201_CREATED) + + except Exception as e: + return Response({"error": f"An error occurred: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + +@api_view(['POST']) +@permission_classes([IsAuthenticated]) +def unbookmark_word(request, word): + """ + Unbookmark a word for the user. + """ + try: + user = request.user + bookmark = WordBookmark.objects.filter(user=user, word=word).first() + if not bookmark: + return Response({"error": "Word not bookmarked"}, status=status.HTTP_400_BAD_REQUEST) + + bookmark.delete() + return Response({"message": "Word unbookmarked successfully"}, status=status.HTTP_200_OK) + + except Exception as e: + return Response({"error": f"An error occurred: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +def get_bookmarked_words(request): + """ + Get all words bookmarked by the user. + """ + try: + user = request.user + bookmarks = WordBookmark.objects.filter(user=user) + bookmarked_words = [bookmark.word for bookmark in bookmarks] + return Response({"bookmarked_words": bookmarked_words}, status=status.HTTP_200_OK) + + except Exception as e: + return Response({"error": f"An error occurred: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + @api_view(['GET']) def get_lexvo_info(request,word): try: