Skip to content

Commit 4875b2b

Browse files
committed
feat(json-server): removed part of json server
1 parent e1eaa98 commit 4875b2b

11 files changed

Lines changed: 61 additions & 82 deletions

File tree

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"version": "1.0.0",
44
"license": "MIT",
55
"scripts": {
6-
"start": "npm run server | npm run ng",
7-
"server": "json-server --port 3000 --watch server/db.json --routes server/routes.json",
6+
"start": "npm run ng",
87
"ng": "ng serve --proxy-config proxy.conf.json",
8+
"deploy": "ng build --prod --base-href /angular4-sample-app/ && angular-cli-ghpages",
99
"tslint": "tslint \"src/**/*.ts\"; exit 0",
1010
"test": "ng test",
1111
"e2e": "npm run pree2e | protractor",
@@ -25,27 +25,27 @@
2525
"@angular/platform-browser-dynamic": "4.2.4",
2626
"@angular/platform-server": "4.2.4",
2727
"@angular/router": "4.2.4",
28-
"bootstrap": "4.0.0-alpha.6",
29-
"core-js": "2.4.1",
30-
"font-awesome": "4.7.0",
31-
"json-server": "0.10.1",
32-
"ng2-translate": "5.0.0",
33-
"rxjs": "5.4.1",
34-
"ts-helpers": "1.1.2",
35-
"zone.js": "0.8.12",
3628
"@types/jasmine": "2.5.53",
3729
"@types/node": "8.0.2",
30+
"angular-cli-ghpages": "0.5.1",
31+
"bootstrap": "4.0.0-alpha.6",
3832
"codelyzer": "3.1.1",
33+
"core-js": "2.4.1",
34+
"font-awesome": "4.7.0",
3935
"jasmine-core": "2.6.4",
4036
"jasmine-spec-reporter": "4.1.1",
4137
"karma": "1.7.0",
4238
"karma-chrome-launcher": "2.2.0",
4339
"karma-cli": "1.0.1",
4440
"karma-jasmine": "1.1.0",
4541
"karma-remap-istanbul": "0.6.0",
42+
"ng2-translate": "5.0.0",
4643
"protractor": "5.1.2",
44+
"rxjs": "5.4.1",
45+
"ts-helpers": "1.1.2",
4746
"ts-node": "3.1.0",
4847
"tslint": "5.4.3",
49-
"typescript": "2.3.4"
48+
"typescript": "2.3.4",
49+
"zone.js": "0.8.12"
5050
}
5151
}

server/db.json

Lines changed: 0 additions & 46 deletions
This file was deleted.

server/routes.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/app/app.translate.factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {TranslateStaticLoader} from 'ng2-translate';
22

33
export function TranslateLoaderFactory(http: any) {
4-
return new TranslateStaticLoader(http, '/assets/i18n', '.json');
4+
return new TranslateStaticLoader(http, 'assets/i18n', '.json');
55
}

src/app/config/app.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const AppConfig: IAppConfig = {
1111
heroById: heroesRoute + '/:id'
1212
},
1313
endpoints: {
14-
heroes: 'api/heroes',
15-
heroesPowers: 'api/heroesPowers'
14+
heroes: 'https://jsonblob.com/api/jsonBlob/11ba5f87-5997-11e7-ae4c-997a6628ed33',
15+
heroesPowers: 'https://jsonblob.com/api/jsonBlob/f8234363-5991-11e7-ae4c-eb7c024ddb08'
1616
}
1717
};

src/app/core/core.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {HeroesModule} from '../heroes/heroes.module';
1212
import {HeroRoutingModule} from '../heroes/heroes-routing.module';
1313

1414
import {NavComponent} from './nav/nav.component';
15-
import {FooterComponent} from "./footer/footer.component";
15+
import {FooterComponent} from './footer/footer.component';
1616

1717

1818
@NgModule({

src/app/heroes/hero-create-new/hero-create-new.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export class HeroFormComponent {
2525

2626
onSubmit() {
2727
this.heroService.create(this.hero)
28-
.then(hero => {
29-
this.heroes.push(hero);
28+
.then(heroes => {
29+
this.heroes = heroes;
3030
this.selectedHero = null;
3131
});
3232
}

src/app/heroes/hero-list/hero-list.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ export class HeroListComponent implements OnInit {
2727
}
2828

2929
getHeroes(): void {
30-
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
30+
this.heroService.getHeroes().then((heroes) => {
31+
this.heroes = heroes;
32+
localStorage.setItem('heroes', JSON.stringify(heroes));
33+
});
3134
}
3235

3336
ngOnInit(): void {

src/app/heroes/hero-search/hero-search.service.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import {Injectable} from '@angular/core';
1+
import {Inject, Injectable} from '@angular/core';
22
import {Http, Response} from '@angular/http';
3-
import {Observable} from 'rxjs';
4-
3+
import {Observable} from 'rxjs';
54
import {Hero} from '../shared/hero.model';
5+
import {IAppConfig} from '../../config/iapp.config';
6+
import {APP_CONFIG} from '../../config/app.config';
67

78
@Injectable()
89
export class HeroSearchService {
9-
constructor(private http: Http) {
10+
constructor(private http: Http,
11+
@Inject(APP_CONFIG) private appConfig: IAppConfig) {
1012
}
1113

1214
search(term: string): Observable<Hero[]> {
1315
return this.http
14-
.get(`api/heroes`)
16+
.get(this.appConfig.endpoints.heroes)
1517
.map((res: Response) => {
1618
let matchedHeroes = [];
1719
let heroes = res.json();

src/app/heroes/shared/hero.service.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,28 @@ export class HeroService {
3636
}
3737

3838
getHeroById(id: number): Promise<Hero> {
39-
const url = `${this.heroesUrl}/${id}`;
39+
const url = `${this.heroesUrl}`;
4040
return this.http.get(url)
4141
.toPromise()
42-
.then(response => response.json() as Hero)
42+
.then((response) => {
43+
const heroesWithId = response.json().filter((hero) => {
44+
return hero.id === id;
45+
});
46+
return heroesWithId.length === 1 ? heroesWithId[0] : {};
47+
})
4348
.catch(HeroService.handleError);
4449
}
4550

46-
create(hero: Hero): Promise<Hero> {
51+
create(hero: Hero): Promise<Array<Hero>> {
52+
let allHeroes = JSON.parse(localStorage.getItem('heroes'));
53+
allHeroes.push({
54+
id: '' + parseInt((Math.random() * (99999 - 100 + 1)), 10) + 100,
55+
name: hero.name,
56+
alterEgo: hero.alterEgo,
57+
power: hero.power
58+
});
4759
return this.http
48-
.post(this.heroesUrl, JSON.stringify({
49-
name: hero.name,
50-
alterEgo: hero.alterEgo,
51-
power: hero.power
52-
}), {headers: this.headers})
60+
.put(this.heroesUrl, JSON.stringify(allHeroes), {headers: this.headers})
5361
.toPromise()
5462
.then(res => res.json())
5563
.catch(HeroService.handleError);
@@ -66,7 +74,7 @@ export class HeroService {
6674

6775
remove(id: number): Promise<void> {
6876
const url = `${this.heroesUrl}/${id}`;
69-
return this.http.delete(url, {headers: this.headers})
77+
return this.http.put(url, {headers: this.headers})
7078
.toPromise()
7179
.then(() => null)
7280
.catch(HeroService.handleError);

0 commit comments

Comments
 (0)