homepage/src/app/services/dataloader.service.ts
2018-01-02 19:52:12 +01:00

49 lines
1.4 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Post } from '../model/post';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import 'rxjs/add/operator/map';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class DataloaderService {
constructor(private http:HttpClient) { }
getPosts(): Observable<Post[]> {
return this.http.get("assets/posts.json").map(posts=>{
let res:Post[] = [];
//fill it up
for(var index in posts) {
let post:any = posts[index];
console.log('got this key: '+post);
console.log(post);
let temp:Post = new Post(post.post.id);
temp.title = post.post.title;
temp.type = post.type;
res.push(temp);
}
console.log('these are the posts...');
console.log(res);
return res;
});
}
/**
* Fetch the 'full fat' post. This returns HTML content that can be wrapped in any container, no sanitizing done.
* @param filename the filename for which the post needs to be fetched
*/
getPost(type:String, id:String): Observable<String> {
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'text/html', 'Accept':'text/html' })
};
console.log('launching GET');
let url = "assets/post/"+type+"/"+id+"/full.html";
console.log('fetching: '+url);
return this.http.get(url, {responseType: 'text'});
}
}