Skip to content

Commit 02f3e21

Browse files
committed
update directory
1 parent ea56d73 commit 02f3e21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+21101
-69
lines changed

advanced-topic/formsArray

Lines changed: 0 additions & 1 deletion
This file was deleted.

advanced-topic/import-excel-file

Lines changed: 0 additions & 1 deletion
This file was deleted.
-960 KB
Loading
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { NgModule } from '@angular/core';
2-
import { Routes, RouterModule } from '@angular/router';
3-
import { IndexComponent } from './components/index/index.component';
4-
import { LoginComponent } from './components/login/login.component';
5-
import { AuthGuard } from '../services/auth-guard.service';
6-
1+
import { NgModule } from "@angular/core";
2+
import { Routes, RouterModule } from "@angular/router";
3+
import { IndexComponent } from "./components/index/index.component";
4+
import { LoginComponent } from "./components/login/login.component";
5+
import { AuthGuard } from "../services/auth-guard.service";
76

87
const routes: Routes = [
9-
{ path: 'login', component: LoginComponent },
10-
{ path: 'home', component: IndexComponent, canActivate:[AuthGuard] },
8+
{ path: "login", component: LoginComponent },
9+
{ path: "home", component: IndexComponent },
10+
// { path: "home", component: IndexComponent, canActivate: [AuthGuard] },
1111
];
1212

1313
@NgModule({
14-
imports: [RouterModule.forRoot(routes)],
15-
exports: [RouterModule]
14+
imports: [RouterModule.forRoot(routes)],
15+
exports: [RouterModule],
1616
})
17-
export class AppRoutingModule { }
17+
export class AppRoutingModule {}

advanced-topic/interceptor/src/app/app.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { LoginComponent } from './components/login/login.component';
1212
import { AuthService } from '../services/auth.service';
1313
import { AuthGuard } from '../services/auth-guard.service';
1414
import { HeaderInterceptor } from '../services/interceptor';
15+
import { CacheInterceptorService } from 'src/services/cache-interceptor.service';
1516

1617
@NgModule({
1718
declarations: [
@@ -33,6 +34,11 @@ import { HeaderInterceptor } from '../services/interceptor';
3334
provide: HTTP_INTERCEPTORS,
3435
useClass: HeaderInterceptor,
3536
multi: true
37+
},
38+
{
39+
provide: HTTP_INTERCEPTORS,
40+
useClass: CacheInterceptorService,
41+
multi: true
3642
}
3743
],
3844
bootstrap: [AppComponent]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ul>
22
<li *ngFor="let donut of donut$ | async">
3-
{{ donut.name }}
3+
{{ donut.style }}
44
</li>
55
</ul>

advanced-topic/interceptor/src/app/components/index/index.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export class IndexComponent implements OnInit {
1414
constructor(private service: DonutService) { }
1515

1616
donutsIndex() {
17-
this.donut$ = this.service.getCollection();
18-
console.log(this.donut$);
19-
17+
this.donut$ = this.service.getCollection();
2018
}
2119

2220
// donutShow() {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
HttpEvent,
3+
HttpHandler,
4+
HttpInterceptor,
5+
HttpRequest,
6+
HttpResponse,
7+
} from "@angular/common/http";
8+
import { Injectable } from "@angular/core";
9+
import { Observable, of } from "rxjs";
10+
import { share, tap } from "rxjs/operators";
11+
12+
@Injectable()
13+
export class CacheInterceptorService implements HttpInterceptor {
14+
private cache: Map<string, HttpResponse<any>> = new Map();
15+
constructor() {}
16+
17+
intercept(
18+
req: HttpRequest<any>,
19+
next: HttpHandler
20+
): Observable<HttpEvent<any>> {
21+
if (req.method !== "GET") {
22+
// this.cache.clear();
23+
return next.handle(req);
24+
}
25+
26+
const cachedResponse = this.cache.get(req.url);
27+
console.log("req.url", req.url);
28+
console.log("cachedResponse", cachedResponse);
29+
30+
if (cachedResponse) {
31+
console.log("cachedResponse PRESENT");
32+
return of(cachedResponse.clone());
33+
} else {
34+
console.log("cachedResponse NOT PRESENT");
35+
return next.handle(req).pipe(
36+
tap((stateEvent) => {
37+
if (stateEvent instanceof HttpResponse) {
38+
this.cache.set(req.url, stateEvent.clone());
39+
}
40+
}),
41+
share()
42+
);
43+
}
44+
}
45+
}

advanced-topic/interceptor/src/services/donut.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { HttpClient } from '@angular/common/http';
44
@Injectable()
55
export class DonutService {
66

7-
url: string = 'http://localhost:4000/students';
7+
url: string = 'https://ga-doughnuts.herokuapp.com/doughnuts';
8+
// url: string = 'http://localhost:4000/students';
9+
10+
811

912
constructor(private httpClient: HttpClient) { }
1013

@@ -21,3 +24,6 @@ export class DonutService {
2124
}
2225

2326
}
27+
28+
29+

0 commit comments

Comments
 (0)