import { Cookie } from 'ng2-cookies' ; import { AuthHttp } from 'angular2-jwt' ; import { Http , Headers , RequestOptions } from '@angular/http' ; import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Rx' ; import * as moment from 'moment' ; import { HelpersService } from './helpers.service' ; import { UserService } from './user.service' ; import { config } from './../../../config' ; @ Injectable ( ) export class MoviesService { constructor ( private http , private us , private hs , private auth ) { } moviesFromDate ( format ? ) { const daysAgo = 30 ; const date = moment ( ) . subtract ( daysAgo , 'days' ) ; if ( format ) { return date . format ( format ) ; } return date . toDate ( ) ; } getTopMovies ( ) { const baseUrl = `${ config . themoviedb . endpoint }/discover/movie?` ; const params = [ `api_key=${ config . themoviedb . apiKey }` , `include_adult=false` , `release_date.gte=${ this . moviesFromDate ( 'YYYY-MM-DD' ) }` , `sort_by=popularity.desc` ] . join ( '&' ) ; return this . http . get ( `${ baseUrl }${ params }` ) . map ( res => res . json ( ) ) . flatMap ( res => { const ids = res . results . map ( ( movie ) => movie . id ) ; return this . getMoviesSummary ( ids , res ) ; } ) . map ( ( { tmdb , api } ) => { tmdb . results . forEach ( ( movie ) => { movie [ 'custom_data' ] = api . data . movies [ movie . id ] ; } ) ; return tmdb ; } ) ; } getMovieDetails ( id ) { const url = `${ config . themoviedb . endpoint }/movie/${ id }?api_key=${ config . themoviedb . apiKey }` ; return this . http . get ( url ) . map ( res => res . json ( ) ) ; } getMovieInternalDetails ( id ) { return this . http . get ( `${ config . api }/movies/movie/${ id }/` ) . first ( ) . map ( ( res ) => res . json ( ) ) ; } rateMovie ( id , rating ) { const url = `${ config . api }/movies/rate` ; return this . us . user$ . first ( ) . flatMap ( user => { if ( user ) { return this . auth . post ( url , { id , rating } , { withCredentials : true } ) ; } return this . postRequest ( url , { id , rating } ) ; } ) ; } getMovieRating ( id ) { const url = `${ config . api }/movies/movie/${ id }/rating/` ; return this . postRequest ( url , { } ) ; } removeRating ( id ) { const params = [ `u=${ this . us . getOrSetUsername ( ) }` , `m_id=${ id }` ] . join ( '&' ) ; const options = this . hs . createHeaders ( ) ; return this . http . delete ( `${ config . api }/movies/rate?${ params }` , options ) . first ( ) . map ( res => res . json ( ) ) ; } getComments ( id , page ) { const params = [ `u=${ this . us . getOrSetUsername ( ) }` , `p=${ page }` ] . join ( '&' ) ; return this . http . get ( `${ config . api }/movies/movie/${ id }/comments/?${ params }` ) . map ( res => res . json ( ) ) ; } postComment ( id , body ) { const url = `${ config . api }/movies/comment` ; return this . us . user$ . first ( ) . flatMap ( user => { if ( user ) { return this . auth . post ( url , { id , body } , { withCredentials : true } ) ; } return this . postRequest ( url , { id , body } ) ; } ) ; } removeComment ( id ) { const params = [ `u=${ this . us . getOrSetUsername ( ) }` , `id=${ id }` ] . join ( '&' ) ; return this . http . delete ( `${ config . api }/movies/comment?${ params }` , this . hs . createHeaders ( ) ) . first ( ) . map ( res => res . json ( ) ) ; } private getMoviesSummary ( movieIds , tmdbRes ) { return Observable . combineLatest ( Observable . of ( tmdbRes ) , this . http . get ( `${ config . api }/movies/get-all?ids=${ movieIds . join ( ',' ) }` ) . map ( ( api ) => api . json ( ) ) , ( tmdb , api ) => { return { tmdb , api } ; } ) ; } private postRequest ( url , data ) { const username = this . us . getOrSetUsername ( ) ; const options = this . hs . createHeaders ( ) ; return this . http . post ( url , Object . assign ( { username } , data ) , options ) . map ( res => res . json ( ) ) ; } }