import { Injectable } from '@angular/core' ; import { SearchResult } from './search-result.model' ; import { Article } from './article.model' ; import { Observable } from 'rxjs/Observable' ; import { Cacheable } from 'rebirth-storage' ; import { RebirthHttp , GET , POST , DELETE , Query , Path , Body } from 'rebirth-http' ; import { environment } from '../../../environments/environment' ; import { HttpClient } from '@angular/common/http' ; export interface IArticleService { getArticles ( pageIndex , pageSize , keyword ? ) : Observable < SearchResult < Article > > ; getArticleByUrl ( articleUrl ) : Observable < Article > ; updateMarkdown ( articleUrl , article ) : Observable < any > ; deleteArticle ( articleUrl ) : Observable < any > ; } @ Injectable ( ) export class OnlineArticleService extends RebirthHttp implements IArticleService { constructor ( http ) { super ( http ) ; } @ Cacheable ( { pool : 'articles' } ) @ GET ( 'article' ) getArticles ( @ Query ( 'pageIndex' ) pageIndex = 1 , @ Query ( 'pageSize' ) pageSize = 10 , @ Query ( 'keyword' ) keyword ? ) { return null ; } @ GET ( 'article/:id' ) getArticleByUrl ( @ Path ( 'id' ) articleUrl ) { return null ; } @ POST ( 'article/:id' ) updateMarkdown ( @ Path ( 'id' ) articleUrl , @ Body article ) { return null ; } @ DELETE ( 'article/:id' ) deleteArticle ( @ Path ( 'id' ) articleUrl ) { return null ; } } @ Injectable ( ) export class GithubArticleService extends RebirthHttp implements IArticleService { constructor ( http ) { super ( http ) ; } getArticles ( pageIndex = 1 , pageSize = 10 , keyword ? ) { return this . innerGetArticles ( ) . map ( res => { const result = res . result || [ ] ; const startIndex = ( pageIndex - 1 ) * pageSize ; return { pageSize , pageIndex , total : result . length , result : result . slice ( startIndex , startIndex + pageSize ) } ; } ) ; } getArticleByUrl ( articleUrl ) { return this . innerGetArticles ( ) . map ( res => { const result = res . result || [ ] ; return result . find ( item => item . url === articleUrl ) ; } ) ; } updateMarkdown ( articleUrl , article ) { return null ; } deleteArticle ( articleUrl ) { return null ; } @ Cacheable ( { pool : 'articles' } ) @ GET ( 'articles.json' ) private innerGetArticles ( ) { return null ; } } @ Injectable ( ) export class ArticleService { private articleService : IArticleService ; constructor ( githubArticleService , onlineArticleService ) { this . articleService = environment . deploy === 'github' ? githubArticleService : onlineArticleService ; } getArticles ( pageIndex , pageSize , keyword ? ) { return this . articleService . getArticles ( pageIndex , pageSize , keyword ) ; } getArticleByUrl ( articleUrl ) { return this . articleService . getArticleByUrl ( articleUrl ) ; } updateMarkdown ( articleUrl , article ) { return this . articleService . updateMarkdown ( articleUrl , article ) ; } deleteArticle ( articleUrl ) { return this . articleService . deleteArticle ( articleUrl ) ; } } export const REBIRTH_ARTICLE_SERVICE_PROVIDERS = [ { provide : GithubArticleService , useClass : GithubArticleService } , { provide : OnlineArticleService , useClass : OnlineArticleService } , { provide : ArticleService , useClass : ArticleService } , ] ;