refactor: rename liked videos to favorite videos
This commit is contained in:
parent
d6c8776467
commit
255784c327
7 changed files with 26 additions and 26 deletions
|
|
@ -13,7 +13,7 @@ class CustomUserAdmin(UserAdmin):
|
||||||
'Individual data',
|
'Individual data',
|
||||||
{
|
{
|
||||||
'fields': (
|
'fields': (
|
||||||
'liked_videos',
|
'favorite_videos',
|
||||||
'watched_videos',
|
'watched_videos',
|
||||||
'verify_email_token',
|
'verify_email_token',
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from video_app.models import Video
|
||||||
|
|
||||||
class CustomUser(AbstractUser):
|
class CustomUser(AbstractUser):
|
||||||
verify_email_token = models.CharField(max_length=20, blank=True, null=True)
|
verify_email_token = models.CharField(max_length=20, blank=True, null=True)
|
||||||
liked_videos = models.ManyToManyField(Video, related_name='liked_by_users', blank=True)
|
favorite_videos = models.ManyToManyField(Video, related_name='favorite_by_users', blank=True)
|
||||||
watched_videos = models.ManyToManyField(Video, related_name='watched_by_users', blank=True)
|
watched_videos = models.ManyToManyField(Video, related_name='watched_by_users', blank=True)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,13 @@ from .models import CustomUser
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = CustomUser
|
model = CustomUser
|
||||||
fields = ["id", "username", "email", "liked_videos", "watched_videos"]
|
fields = ["id", "username", "email", "favorite_videos", "watched_videos"]
|
||||||
|
|
||||||
|
|
||||||
class LikedVideosSerializer(serializers.ModelSerializer):
|
class FavoriteVideosSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = CustomUser
|
model = CustomUser
|
||||||
fields = ['liked_videos']
|
fields = ['favorite_videos']
|
||||||
|
|
||||||
class WatchedVideosSerializer(serializers.ModelSerializer):
|
class WatchedVideosSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ from rest_framework.decorators import api_view, permission_classes
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from .serializer import LikedVideosSerializer, WatchedVideosSerializer, UserSerializer
|
from .serializer import FavoriteVideosSerializer, WatchedVideosSerializer, UserSerializer
|
||||||
from .models import CustomUser
|
from .models import CustomUser
|
||||||
|
|
||||||
@api_view(['GET', 'POST'])
|
@api_view(['GET', 'POST'])
|
||||||
|
|
@ -47,14 +47,14 @@ def user_video_preferences(request, id):
|
||||||
|
|
||||||
@api_view(['PUT'])
|
@api_view(['PUT'])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
def user_liked_videos(request, id):
|
def user_favorite_videos(request, id):
|
||||||
try:
|
try:
|
||||||
user = CustomUser.objects.get(pk=id)
|
user = CustomUser.objects.get(pk=id)
|
||||||
except CustomUser.DoesNotExist:
|
except CustomUser.DoesNotExist:
|
||||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
if request.method == 'PUT':
|
if request.method == 'PUT':
|
||||||
serializer = LikedVideosSerializer(user, data=request.data, partial=True)
|
serializer = FavoriteVideosSerializer(user, data=request.data, partial=True)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ urlpatterns = [
|
||||||
# Users URLs
|
# Users URLs
|
||||||
path('users/', user_views.users_list, name='users_list'),
|
path('users/', user_views.users_list, name='users_list'),
|
||||||
path('users/<int:id>/video-prefs/', user_views.user_video_preferences, name='user_video_preferences'),
|
path('users/<int:id>/video-prefs/', user_views.user_video_preferences, name='user_video_preferences'),
|
||||||
path('users/<int:id>/liked/', user_views.user_liked_videos, name='user_favorite_videos'),
|
path('users/<int:id>/favorites/', user_views.user_favorite_videos, name='user_favorite_videos'),
|
||||||
path('users/<int:id>/watched/', user_views.user_watched_videos, name='user_watched_videos'),
|
path('users/<int:id>/watched/', user_views.user_watched_videos, name='user_watched_videos'),
|
||||||
|
|
||||||
# Authentication URLs
|
# Authentication URLs
|
||||||
|
|
|
||||||
|
|
@ -96,13 +96,13 @@ export class HomeComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the user's liked and watched video IDs.
|
* Loads the user's favorites and watched video IDs.
|
||||||
*/
|
*/
|
||||||
private async loadUserVideoPreferences(): Promise<void> {
|
private async loadUserVideoPreferences(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const { liked_videos, watched_videos } =
|
const { favorite_videos, watched_videos } =
|
||||||
await this.userService.getUserVideoPreferences();
|
await this.userService.getUserVideoPreferences();
|
||||||
this.favoriteVideos = liked_videos;
|
this.favoriteVideos = favorite_videos;
|
||||||
this.watchedVideos = watched_videos;
|
this.watchedVideos = watched_videos;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load user video preferences:', error);
|
console.error('Failed to load user video preferences:', error);
|
||||||
|
|
@ -123,7 +123,7 @@ export class HomeComponent implements OnInit {
|
||||||
*/
|
*/
|
||||||
onFavoriteVideoChange(favorites: number[]): void {
|
onFavoriteVideoChange(favorites: number[]): void {
|
||||||
this.favoriteVideos = favorites;
|
this.favoriteVideos = favorites;
|
||||||
this.updateLikedVideos();
|
this.updateFavoriteVideos();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -143,13 +143,13 @@ export class HomeComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates user's liked videos in the backend.
|
* Updates user's favorite videos in the backend.
|
||||||
*/
|
*/
|
||||||
private updateLikedVideos(): void {
|
private updateFavoriteVideos(): void {
|
||||||
const body = {
|
const body = {
|
||||||
liked_videos: this.favoriteVideos,
|
favorite_videos: this.favoriteVideos,
|
||||||
};
|
};
|
||||||
this.userService.updateLikedVideos(body);
|
this.userService.updateFavoriteVideos(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -14,29 +14,29 @@ export class UserService {
|
||||||
constructor(private apiService: ApiService) {}
|
constructor(private apiService: ApiService) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the list of videos liked and watched by the current user
|
* Fetch the list of favorite and watched videos for the current user
|
||||||
*
|
*
|
||||||
* @returns a promise resolving to an object with two properties:
|
* @returns a promise resolving to an object with two properties:
|
||||||
* - `liked_videos`: a list of video IDs liked by the current user
|
* - `favorite_videos`: a list of video IDs favorited by the current user
|
||||||
* - `watched_videos`: a list of video IDs watched by the current user
|
* - `watched_videos`: a list of video IDs watched by the current user
|
||||||
*/
|
*/
|
||||||
getUserVideoPreferences(): Promise<any> {
|
getUserVideoPreferences(): Promise<any> {
|
||||||
return firstValueFrom(
|
return firstValueFrom(
|
||||||
this.apiService.get(`/user/${this.currentUserId}/video-prefs/`, true)
|
this.apiService.get(`/users/${this.currentUserId}/video-prefs/`, true)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the list of liked videos for the current user
|
* Update the list of favorite videos for the current user
|
||||||
*
|
*
|
||||||
* @param likedVideos a list of video IDs liked by the current user
|
* @param favoriteVideos a list of video IDs favorited by the current user
|
||||||
* @returns a promise resolved when the update is successful
|
* @returns a promise resolved when the update is successful
|
||||||
*/
|
*/
|
||||||
updateLikedVideos(likedVideos: any): Promise<any> {
|
updateFavoriteVideos(favoriteVideos: any): Promise<any> {
|
||||||
return firstValueFrom(
|
return firstValueFrom(
|
||||||
this.apiService.put(
|
this.apiService.put(
|
||||||
`/user/${this.currentUserId}/liked/`,
|
`/users/${this.currentUserId}/favorites/`,
|
||||||
likedVideos,
|
favoriteVideos,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
@ -51,7 +51,7 @@ export class UserService {
|
||||||
updateWatchedVideos(watchedVideos: any): Promise<any> {
|
updateWatchedVideos(watchedVideos: any): Promise<any> {
|
||||||
return firstValueFrom(
|
return firstValueFrom(
|
||||||
this.apiService.put(
|
this.apiService.put(
|
||||||
`/user/${this.currentUserId}/watched/`,
|
`/users/${this.currentUserId}/watched/`,
|
||||||
watchedVideos,
|
watchedVideos,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue