From 727a636b975ebe128cfff31ac9335fe49a7cfa7b Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 20 Nov 2024 11:22:11 +0100 Subject: [PATCH] nice --- src/app/app.component.ts | 4 ++- src/app/app.config.ts | 3 +- .../main-view/main-view.component.html | 5 +++- .../main-view/main-view.component.ts | 29 +++++++++++++++++-- .../show-details/show-details.component.css | 0 .../show-details/show-details.component.html | 9 ++++++ .../show-details.component.spec.ts | 23 +++++++++++++++ .../show-details/show-details.component.ts | 18 ++++++++++++ .../show-form/show-form.component.ts | 4 +-- .../show-list/show-list.component.html | 7 +++-- .../show-list/show-list.component.ts | 15 ++++++---- src/app/model/ShowDto.ts | 11 +++++++ src/app/model/show.ts | 2 +- src/app/services/api-service.ts | 24 +++++++++++++++ 14 files changed, 138 insertions(+), 16 deletions(-) create mode 100644 src/app/components/show-details/show-details.component.css create mode 100644 src/app/components/show-details/show-details.component.html create mode 100644 src/app/components/show-details/show-details.component.spec.ts create mode 100644 src/app/components/show-details/show-details.component.ts create mode 100644 src/app/model/ShowDto.ts create mode 100644 src/app/services/api-service.ts diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 7290617..b1cfabd 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,6 +1,7 @@ -import { Component } from '@angular/core'; +import {Component, inject} from '@angular/core'; import { RouterOutlet } from '@angular/router'; import {MainViewComponent} from './components/main-view/main-view.component'; +import {ShowService} from './services/show.service'; @Component({ selector: 'app-root', @@ -11,4 +12,5 @@ import {MainViewComponent} from './components/main-view/main-view.component'; }) export class AppComponent { title = 'netflix'; + showService: ShowService = inject(ShowService); } diff --git a/src/app/app.config.ts b/src/app/app.config.ts index a1e7d6f..5ecf61e 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -2,7 +2,8 @@ import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; +import {provideHttpClient} from '@angular/common/http'; export const appConfig: ApplicationConfig = { - providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes)] + providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideHttpClient()] }; diff --git a/src/app/components/main-view/main-view.component.html b/src/app/components/main-view/main-view.component.html index a034650..3cc4664 100644 --- a/src/app/components/main-view/main-view.component.html +++ b/src/app/components/main-view/main-view.component.html @@ -2,9 +2,12 @@

TV-Serien

- +
+ @if (isShowSelected) { + + }
diff --git a/src/app/components/main-view/main-view.component.ts b/src/app/components/main-view/main-view.component.ts index 7bb45e8..02ea0f3 100644 --- a/src/app/components/main-view/main-view.component.ts +++ b/src/app/components/main-view/main-view.component.ts @@ -1,16 +1,41 @@ -import {Component} from '@angular/core'; +import {Component, inject} from '@angular/core'; import {ShowListComponent} from '../show-list/show-list.component'; +import {Show} from '../../model/show'; +import {ShowService} from '../../services/show.service'; import {ShowFormComponent} from '../show-form/show-form.component'; +import {BehaviorSubject} from 'rxjs'; +import {ApiService} from '../../services/api-service'; +import {ShowDetailsComponent} from '../show-details/show-details.component'; @Component({ selector: 'app-main-view', standalone: true, imports: [ ShowListComponent, - ShowFormComponent + ShowFormComponent, + ShowDetailsComponent ], templateUrl: './main-view.component.html', styleUrl: './main-view.component.css' }) export class MainViewComponent { + private dataService: ShowService = inject(ShowService); + private apiService: ApiService = inject(ApiService); + + public shows: Show[] = []; + selectedShow$: BehaviorSubject; + isShowSelected = false; + + constructor() { + this.shows = this.dataService.getShows(); + this.selectedShow$ = new BehaviorSubject(new Show(0, "")) + } + + onSelectedShow(show: Show) { + const title = show.title ?? ''; + this.apiService.getDetailShow(title).subscribe((s) => { + this.selectedShow$.next(s); + }); + this.isShowSelected = true; + } } diff --git a/src/app/components/show-details/show-details.component.css b/src/app/components/show-details/show-details.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/components/show-details/show-details.component.html b/src/app/components/show-details/show-details.component.html new file mode 100644 index 0000000..e18143c --- /dev/null +++ b/src/app/components/show-details/show-details.component.html @@ -0,0 +1,9 @@ +@if (detailShow$ | async; as detailShow) { +
+ +
+

{{ detailShow.title }}

+

+
+} + diff --git a/src/app/components/show-details/show-details.component.spec.ts b/src/app/components/show-details/show-details.component.spec.ts new file mode 100644 index 0000000..53982bc --- /dev/null +++ b/src/app/components/show-details/show-details.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ShowDetailsComponent } from './show-details.component'; + +describe('ShowDetailsComponent', () => { + let component: ShowDetailsComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [ShowDetailsComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(ShowDetailsComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/components/show-details/show-details.component.ts b/src/app/components/show-details/show-details.component.ts new file mode 100644 index 0000000..3201695 --- /dev/null +++ b/src/app/components/show-details/show-details.component.ts @@ -0,0 +1,18 @@ +import {Component, Input} from '@angular/core'; +import {BehaviorSubject} from 'rxjs'; +import {Show} from '../../model/show'; +import {AsyncPipe, NgOptimizedImage} from '@angular/common'; + +@Component({ + selector: 'app-show-details', + standalone: true, + imports: [ + AsyncPipe, + NgOptimizedImage + ], + templateUrl: './show-details.component.html', + styleUrl: './show-details.component.css' +}) +export class ShowDetailsComponent { + @Input() detailShow$?: BehaviorSubject; +} diff --git a/src/app/components/show-form/show-form.component.ts b/src/app/components/show-form/show-form.component.ts index 70f2fd4..3f8d5a1 100644 --- a/src/app/components/show-form/show-form.component.ts +++ b/src/app/components/show-form/show-form.component.ts @@ -14,12 +14,12 @@ import {FormsModule} from '@angular/forms'; }) export class ShowFormComponent { showService: ShowService = inject(ShowService); - show: Show = new Show(null, null); + show: Show = new Show(0, ""); shows: Show[] = this.showService.getShows(); save() { this.shows.push(this.show); - this.show = new Show(null, null); + this.show = new Show(0, ""); } } diff --git a/src/app/components/show-list/show-list.component.html b/src/app/components/show-list/show-list.component.html index 0f4cacb..062c8fe 100644 --- a/src/app/components/show-list/show-list.component.html +++ b/src/app/components/show-list/show-list.component.html @@ -18,10 +18,13 @@ {{ show.id }} {{ show.title }} - + - + + + + } diff --git a/src/app/components/show-list/show-list.component.ts b/src/app/components/show-list/show-list.component.ts index 112708a..f4544b8 100644 --- a/src/app/components/show-list/show-list.component.ts +++ b/src/app/components/show-list/show-list.component.ts @@ -1,6 +1,5 @@ -import {Component, inject} from '@angular/core'; +import {Component, EventEmitter, Input, Output} from '@angular/core'; import {Show} from '../../model/show'; -import {ShowService} from '../../services/show.service'; import {FormsModule} from '@angular/forms'; @Component({ @@ -13,9 +12,13 @@ import {FormsModule} from '@angular/forms'; styleUrl: './show-list.component.css' }) export class ShowListComponent { - showService: ShowService = inject(ShowService); - shows: Show[] = this.showService.getShows(); - editShow: Show = new Show(null, null); + @Input() shows: Show[] = []; + @Output() selectedShow = new EventEmitter(); + editShow: Show = new Show(0, ""); + + showDetails(show: Show) { + this.selectedShow.emit(show); + } edit(show: Show) { this.editShow = show; @@ -24,7 +27,7 @@ export class ShowListComponent { saveEdit() { this.updateShow(this.editShow); - this.editShow = new Show(null, null); + this.editShow = new Show(0, ""); } updateShow(show: Show) { diff --git a/src/app/model/ShowDto.ts b/src/app/model/ShowDto.ts new file mode 100644 index 0000000..f390305 --- /dev/null +++ b/src/app/model/ShowDto.ts @@ -0,0 +1,11 @@ +export class ShowDTO { + constructor( + public id: number, + public title: string, + public image?: { + medium: string; + original: string; + }, + public summary?: string) { + } +} diff --git a/src/app/model/show.ts b/src/app/model/show.ts index e39393a..7be6f90 100644 --- a/src/app/model/show.ts +++ b/src/app/model/show.ts @@ -1,4 +1,4 @@ export class Show { - constructor(public id: number|null, public title: string|null) { + constructor(public id?: number, public title?: string, public image?: string, public summary?: string) { } } diff --git a/src/app/services/api-service.ts b/src/app/services/api-service.ts new file mode 100644 index 0000000..7e4efb6 --- /dev/null +++ b/src/app/services/api-service.ts @@ -0,0 +1,24 @@ +import {inject, Injectable} from '@angular/core'; +import {Show} from '../model/show'; +import {HttpClient} from '@angular/common/http'; +import {map, Observable} from 'rxjs'; +import {ShowDTO} from '../model/ShowDto'; + +@Injectable({ + providedIn: 'root' +}) +export class ApiService { + private baseUrl: string = "https://api.tvmaze.com"; + httpClient: HttpClient = inject(HttpClient); + + getDetailShow(title: string): Observable { + const apiUrl = `${this.baseUrl}/singlesearch/shows?q=${title}`; + let show: Show = new Show(0, title); + + return this.httpClient.get(apiUrl).pipe(map((s) => { + show.summary = s.summary; + show.image = s.image?.medium; + return show; + })); + } +}