refactor: improve code structure and rename variables for clarity

This commit is contained in:
Chneemann 2025-05-16 13:03:18 +02:00
parent 92bf86bca0
commit d6c8776467
6 changed files with 26 additions and 35 deletions

View file

@ -7,7 +7,7 @@ from .models import CustomUser
@api_view(['GET', 'POST']) @api_view(['GET', 'POST'])
@permission_classes([IsAuthenticated]) @permission_classes([IsAuthenticated])
def user_list(request): def users_list(request):
if request.method == 'GET': if request.method == 'GET':
user = CustomUser.objects.all() user = CustomUser.objects.all()
@ -23,7 +23,7 @@ def user_list(request):
@api_view(['GET', 'PUT', 'DELETE']) @api_view(['GET', 'PUT', 'DELETE'])
@permission_classes([IsAuthenticated]) @permission_classes([IsAuthenticated])
def user_detail(request, id): def user_video_preferences(request, id):
try: try:
user = CustomUser.objects.get(pk=id) user = CustomUser.objects.get(pk=id)
@ -47,7 +47,7 @@ def user_detail(request, id):
@api_view(['PUT']) @api_view(['PUT'])
@permission_classes([IsAuthenticated]) @permission_classes([IsAuthenticated])
def user_liked_detail(request, id): def user_liked_videos(request, id):
try: try:
user = CustomUser.objects.get(pk=id) user = CustomUser.objects.get(pk=id)
except CustomUser.DoesNotExist: except CustomUser.DoesNotExist:
@ -62,7 +62,7 @@ def user_liked_detail(request, id):
@api_view(['PUT']) @api_view(['PUT'])
@permission_classes([IsAuthenticated]) @permission_classes([IsAuthenticated])
def user_watched_detail(request, id): def user_watched_videos(request, id):
try: try:
user = CustomUser.objects.get(pk=id) user = CustomUser.objects.get(pk=id)
except CustomUser.DoesNotExist: except CustomUser.DoesNotExist:

View file

@ -28,11 +28,11 @@ urlpatterns = [
path('video/progress/<int:video_id>', video_views.video_progress_view, name='video-progress'), path('video/progress/<int:video_id>', video_views.video_progress_view, name='video-progress'),
# Users URLs # Users URLs
path('users/', user_views.user_list, name='user_list'), path('users/', user_views.users_list, name='users_list'),
path('users/<int:id>/', user_views.user_detail, name='user_detail'), path('users/<int:id>/video-prefs/', user_views.user_video_preferences, name='user_video_preferences'),
path('users/liked/<int:id>/', user_views.user_liked_detail, name='user_liked_detail'), path('users/<int:id>/liked/', user_views.user_liked_videos, name='user_favorite_videos'),
path('users/watched/<int:id>/', user_views.user_watched_detail, name='user_watched_detail'), path('users/<int:id>/watched/', user_views.user_watched_videos, name='user_watched_videos'),
# Authentication URLs # Authentication URLs
path('auth/', AuthView.as_view(), name='auth_view'), path('auth/', AuthView.as_view(), name='auth_view'),
path('auth/login/', LoginView.as_view(), name='login'), path('auth/login/', LoginView.as_view(), name='login'),

View file

@ -101,7 +101,7 @@ export class HomeComponent implements OnInit {
private async loadUserVideoPreferences(): Promise<void> { private async loadUserVideoPreferences(): Promise<void> {
try { try {
const { liked_videos, watched_videos } = const { liked_videos, watched_videos } =
await this.userService.getLikedAndWatchedVideos(); await this.userService.getUserVideoPreferences();
this.favoriteVideos = liked_videos; this.favoriteVideos = liked_videos;
this.watchedVideos = watched_videos; this.watchedVideos = watched_videos;
} catch (error) { } catch (error) {

View file

@ -46,16 +46,16 @@ export class ApiService {
} }
/** /**
* Sends a POST request to the specified endpoint with a request body. * Sends a POST request to the specified endpoint with upload event tracking.
* *
* @template T The expected type of the response. * @template T The expected type of the final response body.
* @param endpoint The API endpoint to send the request to. * @param endpoint The API endpoint to send the request to.
* @param body The request payload to send. * @param body The request payload (e.g., FormData for file upload).
* @param auth Whether to include authentication headers. Defaults to false. * @param auth Whether to include authentication headers. Defaults to false.
* @param options Additional options for the request. * @param options Additional HTTP options (e.g., observe, reportProgress).
* @returns An Observable of type HttpEvent<T>. * @returns An Observable emitting HttpEvent<T> objects.
*/ */
postWithProgress<T>( postWithUploadEvents<T>(
endpoint: string, endpoint: string,
body: any, body: any,
auth: boolean = false, auth: boolean = false,

View file

@ -20,9 +20,9 @@ export class UserService {
* - `liked_videos`: a list of video IDs liked by the current user * - `liked_videos`: a list of video IDs liked 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
*/ */
getLikedAndWatchedVideos(): Promise<any> { getUserVideoPreferences(): Promise<any> {
return firstValueFrom( return firstValueFrom(
this.apiService.get(`/users/${this.currentUserId}/`, true) this.apiService.get(`/user/${this.currentUserId}/video-prefs/`, true)
); );
} }
@ -35,7 +35,7 @@ export class UserService {
updateLikedVideos(likedVideos: any): Promise<any> { updateLikedVideos(likedVideos: any): Promise<any> {
return firstValueFrom( return firstValueFrom(
this.apiService.put( this.apiService.put(
`/users/liked/${this.currentUserId}/`, `/user/${this.currentUserId}/liked/`,
likedVideos, likedVideos,
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(
`/users/watched/${this.currentUserId}/`, `/user/${this.currentUserId}/watched/`,
watchedVideos, watchedVideos,
true true
) )

View file

@ -4,6 +4,7 @@ import { ApiService } from './api.service';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { ResolutionService } from './resolution.service'; import { ResolutionService } from './resolution.service';
import { Video } from '../interfaces/video.interface'; import { Video } from '../interfaces/video.interface';
import { HttpEvent } from '@angular/common/http';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -37,23 +38,13 @@ export class VideoService {
} }
/** /**
* Fetch a video by its video URL * Uploads a video file to the server with progress tracking.
* *
* @param videoUrl the ID of the video to fetch * @param formData The FormData object representing the video to upload.
* @returns a promise resolving to the video object * @returns An Observable emitting HttpEvent objects (e.g., progress, response).
*/ */
getVideoFiles(videoUrl: number): Promise<any> { uploadVideoWithProgress(formData: FormData): Observable<HttpEvent<any>> {
return firstValueFrom(this.apiService.get(`/${videoUrl}`, true)); return this.apiService.postWithUploadEvents<any>(
}
/**
* Upload a video to the server
*
* @param formData the FormData object representing the video to upload
* @returns a promise resolved when the upload is successful
*/
uploadVideoWithProgress(formData: FormData): Observable<any> {
return this.apiService.postWithProgress<any>(
'/video/upload/', '/video/upload/',
formData, formData,
true, true,