diff --git a/frontend/src/app/core/core.module.ts b/frontend/src/app/core/core.module.ts index 4d0d0a6..62404ed 100644 --- a/frontend/src/app/core/core.module.ts +++ b/frontend/src/app/core/core.module.ts @@ -10,6 +10,10 @@ import { SidebarComponent } from './components/sidebar/sidebar.component'; import { NavbarComponent } from './components/navbar/navbar.component'; import { RouterModule } from '@angular/router'; import { EntrySeenService } from './services/entry-seen.service'; +import { EpisodeService } from './services/episode.service'; +import { EpisodeSeenService } from './services/episode-seen.service'; +import { ThemeService } from './services/theme.service'; +import { SeasonService } from './services/season.service'; @NgModule({ declarations: [SidebarComponent, NavbarComponent], @@ -21,6 +25,12 @@ import { EntrySeenService } from './services/entry-seen.service'; EntrySeenService, UserCollectionService, ToggleService, + EpisodeService, + EpisodeSeenService, + SeasonService, + + ToggleService, + ThemeService, ], exports: [SidebarComponent, NavbarComponent], }) diff --git a/frontend/src/app/core/services/episode.service.ts b/frontend/src/app/core/services/episode.service.ts new file mode 100644 index 0000000..08d2b84 --- /dev/null +++ b/frontend/src/app/core/services/episode.service.ts @@ -0,0 +1,12 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; + +@Injectable({ + providedIn: 'root', +}) +export class EpisodeService { + private backend_apiUrl = 'http://localhost:4000/api/v1'; + private apiAction = 'episodes'; + + constructor(private http: HttpClient) {} +} diff --git a/frontend/src/app/core/services/season.service.ts b/frontend/src/app/core/services/season.service.ts index 7cb1977..1a234f5 100644 --- a/frontend/src/app/core/services/season.service.ts +++ b/frontend/src/app/core/services/season.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Season } from '../../shared/models/season.model'; +import { Episode } from '../../shared/models/episode.models'; @Injectable({ providedIn: 'root', @@ -24,4 +25,10 @@ export class SeasonService { [null] ); } + + getSeasonEpisodes(id: number) { + return this.http.get( + `${this.backend_apiUrl}/${this.apiAction}/${id}/episodes` + ); + } } diff --git a/frontend/src/app/shared/cards/season-main-card/season-main-card.component.html b/frontend/src/app/shared/cards/season-main-card/season-main-card.component.html index 4e15ff2..799520c 100644 --- a/frontend/src/app/shared/cards/season-main-card/season-main-card.component.html +++ b/frontend/src/app/shared/cards/season-main-card/season-main-card.component.html @@ -6,13 +6,17 @@
{{season.name}}

{{season.air_date | dateFormat:'yyyy'}}

-
+
-
+
- -
- +
+ {{episodesWatchedCount}}/{{this.season.episode_count}} +
+
+
diff --git a/frontend/src/app/shared/cards/season-main-card/season-main-card.component.ts b/frontend/src/app/shared/cards/season-main-card/season-main-card.component.ts index 515f2e1..8649bfb 100644 --- a/frontend/src/app/shared/cards/season-main-card/season-main-card.component.ts +++ b/frontend/src/app/shared/cards/season-main-card/season-main-card.component.ts @@ -8,6 +8,8 @@ import { transition, animate, } from '@angular/animations'; +import { Episode } from '../../models/episode.models'; +import { EpisodeSeenService } from '../../../core/services/episode-seen.service'; @Component({ selector: 'app-season-main-card', @@ -29,21 +31,26 @@ import { }) export class SeasonMainCardComponent implements OnInit { @Input() season: Season = {} as Season; + isChecked: boolean = false; + isLocked: boolean = false; - constructor(private seasonService: SeasonService) {} + constructor( + private seasonService: SeasonService, + private episodeSeenService: EpisodeSeenService + ) {} ngOnInit(): void { - // // TODO: GET SEASON EPISODES - // this.seasonService.getSeasonById(this.season.id).subscribe({ - // next: (season) => { - // console.log(season); - // this.season = season; - // }, - // error: (error) => { - // console.log(`Couldn't fetch season data:`, error); - // // TODO show notification - // }, - // }); + // TODO: GET SEASON EPISODES + this.seasonService.getSeasonEpisodes(this.season.id).subscribe({ + next: (episodes) => { + console.log(episodes); + this.season.episodes = episodes; + }, + error: (error) => { + console.log(`Couldn't fetch season episodes data:`, error); + // TODO show notification + }, + }); } get posterPath() { @@ -53,8 +60,9 @@ export class SeasonMainCardComponent implements OnInit { return 'http://image.tmdb.org/t/p/w154/' + this.season.poster_path; } - get episodesWatchedCount() { + get episodesWatchedPercentage() { let count = 0; + if (!this.season.episodes) return 0; this.season.episodes.forEach((ep) => { if (ep.date_first_seen != null) count++; }); @@ -64,4 +72,58 @@ export class SeasonMainCardComponent implements OnInit { let value = (count * 100) / this.season.episode_count; return value; } + + get episodesWatchedCount() { + let count = 0; + if (!this.season.episodes) return 0; + this.season.episodes.forEach((ep) => { + if (ep.date_first_seen != null) count++; + }); + + return count; + } + handleCheckChange() { + this.isChecked = true; + + if (this.isLocked) return; + this.isLocked = true; + + if (this.isChecked) { + for (let i = this.season.episode_count; i > 0; i--) { + if (this.season.episodes[i - 1].date_first_seen != null) { + let currentEpisode = this.season.episodes[i]; + this.episodeSeenService + .createEpisodeSeen(currentEpisode.id, new Date(), 'Me, My Self & I') + .subscribe({ + next: (episodeSeen) => { + console.log(`Episode marked as seen`, episodeSeen); + this.season.episodes[i].date_first_seen = new Date(); + this.season.episodes[i].date_last_seen = new Date(); + // TODO show notification + this.isChecked = false; + this.isLocked = false; + }, + error: (error) => { + console.log(`Couldn't mark episode as seen:`, error); + // TODO show notification + this.isChecked = false; + this.isLocked = false; + }, + }); + break; + } + } + + // console.log('checking'); + // this.executeFunction() + // .then(() => { + // this.isChecked = false; + // console.log('unchecking'); + // }) + // .catch((error) => { + // console.error('Failed to execute function:', error); + // this.isChecked = false; + // }); + } + } }