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

View file

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

View file

@ -101,7 +101,7 @@ export class HomeComponent implements OnInit {
private async loadUserVideoPreferences(): Promise<void> {
try {
const { liked_videos, watched_videos } =
await this.userService.getLikedAndWatchedVideos();
await this.userService.getUserVideoPreferences();
this.favoriteVideos = liked_videos;
this.watchedVideos = watched_videos;
} 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 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 options Additional options for the request.
* @returns An Observable of type HttpEvent<T>.
* @param options Additional HTTP options (e.g., observe, reportProgress).
* @returns An Observable emitting HttpEvent<T> objects.
*/
postWithProgress<T>(
postWithUploadEvents<T>(
endpoint: string,
body: any,
auth: boolean = false,

View file

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

View file

@ -4,6 +4,7 @@ import { ApiService } from './api.service';
import { AuthService } from './auth.service';
import { ResolutionService } from './resolution.service';
import { Video } from '../interfaces/video.interface';
import { HttpEvent } from '@angular/common/http';
@Injectable({
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
* @returns a promise resolving to the video object
* @param formData The FormData object representing the video to upload.
* @returns An Observable emitting HttpEvent objects (e.g., progress, response).
*/
getVideoFiles(videoUrl: number): Promise<any> {
return firstValueFrom(this.apiService.get(`/${videoUrl}`, true));
}
/**
* 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>(
uploadVideoWithProgress(formData: FormData): Observable<HttpEvent<any>> {
return this.apiService.postWithUploadEvents<any>(
'/video/upload/',
formData,
true,