@ -3,29 +3,90 @@ import { Http } from '@angular/http';
import { Post } from 'src/app/models/post' ;
import { Author } from 'src/app/models/author' ;
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 ( {
providedIn : 'root'
} )
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 ) { }
public createPost ( pContent : String ) {
const url = environment . graphQLUrl ;
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
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: {
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 ( ) ) ;
} ) ;
}
this . http . post ( url , body ) . subscribe ( response = > {
} ) ;
public createPostActivity ( pContent : String , activityId : String ) {
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
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 ( ) ) ;
} ) ;
}
public upvote ( pPostID : number ) : any {
@ -71,26 +132,31 @@ export class FeedService {
return this . http . post ( environment . graphQLUrl , body ) ;
}
public getAllPosts ( ) : Array < Post > {
const url = environment . graphQLUrl ;
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
this . http . post ( url , this . getBodyForGetAllPosts ( ) )
. subscribe ( response = > {
this . posts = this . renderAllPosts ( response . json ( ) ) ;
public getNewPosts() {
if ( this . newPosts . getValue ( ) . length === 0 ) {
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
this . http . post ( environment . graphQLUrl , this . buildJsonNew ( ) )
. subscribe ( response = > {
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 {
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
return this . http . post ( environment . graphQLUrl , this . getBodyForGetAllPosts ( ) ) ;
public getMostLikedPosts() {
if ( this . mostLikedPosts . getValue ( ) . length === 0 ) {
const headers = new Headers ( ) ;
headers . set ( 'Content-Type' , 'application/json' ) ;
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 : ` {
getPosts ( first : 1000 , offset : 0 ) {
id ,
@ -99,7 +165,13 @@ export class FeedService {
upvotes ,
downvotes ,
userVote ,
deletable
deletable ,
activity {
id
name
description
points
} ,
author {
name ,
handle ,
@ -110,6 +182,56 @@ export class FeedService {
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 {
name ,
handle ,
id } ,
createdAt }
} ` , variables: {
} } ;
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 > {
const posts = new Array < Post > ( ) ;
// 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 temp = new Date ( Number ( post . createdAt ) ) ;
const date = temp . toLocaleString ( 'en-GB' ) ;
posts . push ( new Post ( id , content , htmlContent , upvotes , downvotes , userVote , deletable , date , author ) ) ;
let activity : Activity ;
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 ;
}