From 5417ac06368db69cec42f073b63c9a0bb9b71220 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Fri, 25 Apr 2025 09:04:24 +0200 Subject: [PATCH] docs: add comments to functions for better understanding --- frontend/src/app/services/movie.service.ts | 26 +++++++++++++++++++ frontend/src/app/services/token.service.ts | 19 ++++++++++++++ frontend/src/app/services/user.service.ts | 13 ++++++++++ .../error-toast/error-toast.component.ts | 13 ++++++++++ .../components/header/header.component.ts | 9 +++++++ 5 files changed, 80 insertions(+) diff --git a/frontend/src/app/services/movie.service.ts b/frontend/src/app/services/movie.service.ts index 3eb06b5..144df3e 100644 --- a/frontend/src/app/services/movie.service.ts +++ b/frontend/src/app/services/movie.service.ts @@ -21,20 +21,46 @@ export class MovieService { private authService: AuthService ) {} + /** + * Fetch all movies available on the server + * + * @returns a promise resolving to an array of movie objects + */ getAllMovies(): Promise { return firstValueFrom(this.apiService.get('/video/', true)); } + /** + * Fetch a movie by its video URL + * + * @param videoUrl the ID of the movie to fetch + * @returns a promise resolving to the movie object + */ getMovieFiles(videoUrl: number): Promise { return firstValueFrom(this.apiService.get(`/${videoUrl}`, true)); } + /** + * Upload a movie to the server + * + * @param formData the FormData object representing the movie to upload + * @returns a promise resolved when the upload is successful + */ uploadMovie(formData: FormData): Promise { return firstValueFrom( this.apiService.post('/video/upload/', formData, true) ); } + /** + * Checks if a movie with the given video ID has been uploaded in the following resolutions: 360p, 720p, 1080p. + * + * @param videoID the ID of the movie to check + * @returns an observable that emits an object with the following properties: + * - '360': a boolean indicating whether the movie has been uploaded in 360p resolution + * - '720': a boolean indicating whether the movie has been uploaded in 720p resolution + * - '1080': a boolean indicating whether the movie has been uploaded in 1080p resolution + */ isMovieResolutionUploaded( videoID: number ): Observable<{ [resolution: string]: boolean }> { diff --git a/frontend/src/app/services/token.service.ts b/frontend/src/app/services/token.service.ts index 8181939..9612f09 100644 --- a/frontend/src/app/services/token.service.ts +++ b/frontend/src/app/services/token.service.ts @@ -9,12 +9,24 @@ export class TokenService { constructor(private router: Router) {} + /** + * Store the given authentication token in either local or session storage. + * + * @param token The authentication token to store. + * @param storage Whether to store the token in local storage (true) or session storage (false). + */ setToken(token: string, storage: boolean): void { storage ? localStorage.setItem(this.TOKEN_KEY, token) : sessionStorage.setItem(this.TOKEN_KEY, token); } + /** + * Returns the authentication token if it exists in either local or session storage. + * Note that session storage takes precedence over local storage. + * + * @returns The authentication token if found, or null otherwise. + */ getToken(): string | null { const tokenFromSession = sessionStorage.getItem(this.TOKEN_KEY); if (tokenFromSession) { @@ -25,11 +37,18 @@ export class TokenService { return tokenFromLocal; } + /** + * Remove the authentication token from both local and session storage. + * Note that this does not navigate the user away from the current page. + */ removeToken(): void { localStorage.removeItem(this.TOKEN_KEY); sessionStorage.removeItem(this.TOKEN_KEY); } + /** + * Clear the session by removing the token from storage and navigating to the home route. + */ clearSession(): void { this.removeToken(); this.router.navigate(['/']); diff --git a/frontend/src/app/services/user.service.ts b/frontend/src/app/services/user.service.ts index 62bcbc3..4649a67 100644 --- a/frontend/src/app/services/user.service.ts +++ b/frontend/src/app/services/user.service.ts @@ -10,12 +10,25 @@ export class UserService { constructor(private apiService: ApiService) {} + /** + * Fetch the list of movies liked and watched by the current user + * + * @returns a promise resolving to an object with two properties: + * - `liked_movies`: a list of movie IDs liked by the current user + * - `watched_movies`: a list of movie IDs watched by the current user + */ getLikedAndWatchedMovies(): Promise { return firstValueFrom( this.apiService.get(`/users/${this.currentUserId}/`, true) ); } + /** + * Update the list of liked movies for the current user + * + * @param likedMovies a list of movie IDs liked by the current user + * @returns a promise resolved when the update is successful + */ updateLikedMovies(likedMovies: any): Promise { return firstValueFrom( this.apiService.put( diff --git a/frontend/src/app/shared/components/error-toast/error-toast.component.ts b/frontend/src/app/shared/components/error-toast/error-toast.component.ts index 805728c..37615f6 100644 --- a/frontend/src/app/shared/components/error-toast/error-toast.component.ts +++ b/frontend/src/app/shared/components/error-toast/error-toast.component.ts @@ -13,16 +13,29 @@ export class ErrorToastComponent implements OnInit { constructor(private errorService: ErrorService) {} + /** + * Listens to the errorText$ observable and updates the component's errorText + * property with the latest error message. + */ ngOnInit(): void { this.errorService.errorText$.subscribe((message: string) => { this.errorText = message; }); } + /** + * Stops the propagation of an event to prevent the component from closing + * when the user clicks on the error message. + * @param event The MouseEvent to stop propagating. + */ stopPropagation(event: MouseEvent) { event.stopPropagation(); } + /** + * Clears the current error message from the error service when the user clicks + * anywhere outside the error toast. + */ closeError() { this.errorService.clearError(); } diff --git a/frontend/src/app/shared/components/header/header.component.ts b/frontend/src/app/shared/components/header/header.component.ts index b072838..97c6a5d 100644 --- a/frontend/src/app/shared/components/header/header.component.ts +++ b/frontend/src/app/shared/components/header/header.component.ts @@ -20,10 +20,19 @@ export class HeaderComponent { private tokenService: TokenService ) {} + /** + * Emits the moviesChange event with the newMovies array as the payload. + * This is used to reset the movies displayed in the movie-list component. + * @param newMovies The new array of movies to be displayed. + */ backToOverview(newMovies: any[]) { this.moviesChange.emit(newMovies); } + /** + * Logs out the user by calling the logout method of AuthService and clears the session using TokenService. + * If an error occurs during the process, it is logged to the console. + */ async logout() { try { await this.authService.logout();