@ -3,28 +3,89 @@ import { Http } from '@angular/http';
import { Post } from 'src/app/models/post' ;
import { Post } from 'src/app/models/post' ;
import { Author } from 'src/app/models/author' ;
import { Author } from 'src/app/models/author' ;
import { environment } from 'src/environments/environment' ;
import { environment } from 'src/environments/environment' ;
import { Activity } from 'src/app/models/activity' ;
import { BehaviorSubject } from 'rxjs' ;
import { User } from 'src/app/models/user' ;
@Injectable ( {
@Injectable ( {
providedIn : 'root'
providedIn : 'root'
} )
} )
export class FeedService {
export class FeedService {
posts : Array < Post > ;
public posts : BehaviorSubject < Post [ ] > = new BehaviorSubject ( new Array ( ) ) ;
public newPosts : BehaviorSubject < Post [ ] > = new BehaviorSubject ( new Array ( ) ) ;
public mostLikedPosts : BehaviorSubject < Post [ ] > = new BehaviorSubject ( new Array ( ) ) ;
constructor ( private http : Http ) { }
constructor ( private http : Http ) { }
public createPost ( pContent : String ) {
public createPost ( pContent : String ) {
const url = environment . graphQLUrl ;
const headers = new Headers ( ) ;
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
const body = { query : ` mutation( $ content: String!) {
const body = { query : ` mutation( $ content: String!) {
createPost ( content : $content ) { id }
createPost ( content : $content ) {
id ,
content ,
htmlContent ,
upvotes ,
downvotes ,
userVote ,
deletable ,
activity {
id
name
description
points
} ,
author {
name ,
handle ,
id } ,
createdAt }
} ` , variables: {
} ` , variables: {
content : pContent
content : pContent
} } ;
} } ;
return this . http . post ( environment . graphQLUrl , body ) . subscribe ( response = > {
const updatedposts = this . newPosts . getValue ( ) ;
updatedposts . unshift ( this . renderPost ( response . json ( ) ) ) ;
this . newPosts . next ( updatedposts ) ;
this . posts . next ( this . newPosts . getValue ( ) ) ;
} ) ;
}
public createPostActivity ( pContent : String , activityId : String ) {
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
this . http . post ( url , body ) . subscribe ( response = > {
const body = { query : ` mutation( $ content: String!, $ id: ID) {
createPost ( content : $content activityId : $id ) {
id ,
content ,
htmlContent ,
upvotes ,
downvotes ,
userVote ,
deletable ,
activity {
id
name
description
points
} ,
author {
name ,
handle ,
id } ,
createdAt }
} ` , variables: {
content : pContent ,
id : activityId
} } ;
return this . http . post ( environment . graphQLUrl , body ) . subscribe ( response = > {
const updatedposts = this . newPosts . getValue ( ) ;
updatedposts . unshift ( this . renderPost ( response . json ( ) ) ) ;
this . newPosts . next ( updatedposts ) ;
this . posts . next ( this . newPosts . getValue ( ) ) ;
} ) ;
} ) ;
}
}
@ -71,26 +132,31 @@ export class FeedService {
return this . http . post ( environment . graphQLUrl , body ) ;
return this . http . post ( environment . graphQLUrl , body ) ;
}
}
public getAllPosts ( ) : Array < Post > {
public getNewPosts() {
const url = environment . graphQLUrl ;
if ( this . newPosts . getValue ( ) . length === 0 ) {
const headers = new Headers ( ) ;
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
this . http . post ( environment . graphQLUrl , this . buildJsonNew ( ) )
this . http . post ( url , this . getBodyForGetAllPosts ( ) )
. subscribe ( response = > {
. subscribe ( response = > {
this . posts = this . renderAllPosts ( response . json ( ) ) ;
this . newPosts . next ( this . renderAllPosts ( response . json ( ) ) ) ;
this . posts . next ( this . newPosts . getValue ( ) ) ;
} ) ;
} ) ;
return this . posts ;
} else { this . posts . next ( this . newPosts . getValue ( ) ) ; }
}
}
public getAllPostsRaw ( ) : any {
public getMostLikedPosts() {
if ( this . mostLikedPosts . getValue ( ) . length === 0 ) {
const headers = new Headers ( ) ;
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
return this . http . post ( environment . graphQLUrl , this . getBodyForGetAllPosts ( ) ) ;
this . http . post ( environment . graphQLUrl , this . buildJsonMostLiked ( ) )
. subscribe ( response = > {
this . mostLikedPosts . next ( this . renderAllPosts ( response . json ( ) ) ) ;
this . posts . next ( this . mostLikedPosts . getValue ( ) ) ;
} ) ;
} else { this . posts . next ( this . mostLikedPosts . getValue ( ) ) ; }
}
}
getBodyForGetAllPosts() {
buildJsonNew () {
const body = { query : ` {
const body = { query : ` {
getPosts ( first : 1000 , offset : 0 ) {
getPosts ( first : 1000 , offset : 0 ) {
id ,
id ,
@ -99,7 +165,39 @@ export class FeedService {
upvotes ,
upvotes ,
downvotes ,
downvotes ,
userVote ,
userVote ,
deletable
deletable ,
activity {
id
name
description
points
} ,
author {
name ,
handle ,
id } ,
createdAt }
} ` , variables: {
} } ;
return body ;
}
buildJsonMostLiked() {
const body = { query : ` {
getPosts ( first : 1000 , offset : 0 , sort : TOP ) {
id ,
content ,
htmlContent ,
upvotes ,
downvotes ,
userVote ,
deletable ,
activity {
id
name
description
points
} ,
author {
author {
name ,
name ,
handle ,
handle ,
@ -110,6 +208,30 @@ export class FeedService {
return body ;
return body ;
}
}
public renderPost ( response : any ) : Post {
const post = response . data . createPost ;
const id : number = post . id ;
const content : string = post . content ;
const htmlContent : string = post . htmlContent ;
const upvotes : number = post . upvotes ;
const downvotes : number = post . downvotes ;
const userVote : string = post . userVote ;
const deletable : boolean = post . deletable ;
const author = new Author ( post . author . id , post . author . name , post . author . handle ) ;
const temp = new Date ( Number ( post . createdAt ) ) ;
const date = temp . toLocaleString ( 'en-GB' ) ;
let activity : Activity ;
if ( post . activity ) {
activity = new Activity (
post . activity . id ,
post . activity . name ,
post . activity . description ,
post . activity . points ) ;
} else { activity = null ; }
return new Post ( id , content , htmlContent , upvotes , downvotes , userVote , deletable , date , author , activity ) ;
}
public renderAllPosts ( pResponse : any ) : Array < Post > {
public renderAllPosts ( pResponse : any ) : Array < Post > {
const posts = new Array < Post > ( ) ;
const posts = new Array < Post > ( ) ;
// let options = {year: 'numeric', month: 'short', day: 'numeric', hour: '' }
// let options = {year: 'numeric', month: 'short', day: 'numeric', hour: '' }
@ -124,8 +246,15 @@ export class FeedService {
const author = new Author ( post . author . id , post . author . name , post . author . handle ) ;
const author = new Author ( post . author . id , post . author . name , post . author . handle ) ;
const temp = new Date ( Number ( post . createdAt ) ) ;
const temp = new Date ( Number ( post . createdAt ) ) ;
const date = temp . toLocaleString ( 'en-GB' ) ;
const date = temp . toLocaleString ( 'en-GB' ) ;
let activity : Activity ;
posts . push ( new Post ( id , content , htmlContent , upvotes , downvotes , userVote , deletable , date , author ) ) ;
if ( post . activity ) {
activity = new Activity (
post . activity . id ,
post . activity . name ,
post . activity . description ,
post . activity . points ) ;
} else { activity = null ; }
posts . push ( new Post ( id , content , htmlContent , upvotes , downvotes , userVote , deletable , date , author , activity ) ) ;
}
}
return posts ;
return posts ;
}
}