Created
June 16, 2018 03:35
-
-
Save vietduchelo/c036db445b6c11c3c648e345a4438f51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Http, RequestOptions } from '@angular/http'; | |
import { HttpClient } from '@angular/common/http'; | |
import 'rxjs/add/operator/map'; | |
import 'rxjs/add/operator/catch'; | |
import { Iphim } from './../entity_phim'; | |
import { Observable } from 'rxjs/Observable'; | |
import { Headers } from '@angular/http'; | |
import {Router} from '@angular/router'; | |
@Injectable() | |
export class LoadphimService { | |
phim: Iphim[] = [] | |
constructor(private http: Http, private router: Router) { | |
this.getPhim().subscribe(phim => { | |
this.phim = phim; | |
}) | |
} | |
getPhim(): Observable<Iphim[]> { | |
const url = "https://shielded-lowlands-59603.herokuapp.com/movies"; | |
return this.http.get(url).map(data => { | |
console.log(data.json()); | |
return data.json() as Iphim[]; | |
}) | |
} | |
getOnePhimByID(id: String): Observable<Iphim> { | |
return this.getPhim().map(phim => { | |
return phim.find(phimid => { | |
return phimid._id == id; | |
}) | |
}) | |
} | |
searchPhim(title: String) { | |
return this.getPhim().map(phims => { | |
return phims.filter(phim => { | |
return phim.name.toLowerCase().includes(title.toLowerCase()); | |
}); | |
}); | |
} | |
createPhim(phim: Iphim) { | |
return this.http.post('https://shielded-lowlands-59603.herokuapp.com/movies', phim).map(data => { | |
console.log(data.json); | |
alert("Them thanh cong"); | |
this.router.navigate(['']); | |
return data.json(); | |
}).catch(err => { | |
console.log(err); | |
return err; | |
}) | |
} | |
upDatePhim(id: String, data: Iphim) { | |
return this.http.put('https://shielded-lowlands-59603.herokuapp.com/movies/'+id, data).subscribe(datas => { | |
console.log(datas); | |
alert("Update thanh cong"); | |
this.router.navigate(['']); | |
}) | |
} | |
deletePhim(id: String){ | |
return this.http.delete('https://shielded-lowlands-59603.herokuapp.com/movies/'+id).subscribe(res =>{ | |
alert("Xoa thanh cong"); | |
console.log(res); | |
this.router.navigate(['']); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment