Move all firbase dependencies from actions layer to firebaseService layer (#13)

This commit is contained in:
Qolzam
2017-10-12 17:47:26 +07:00
parent c82826bdd4
commit 0785c38d42
48 changed files with 1305 additions and 796 deletions

View File

@@ -3,15 +3,69 @@ import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { ICommentService } from 'services/comments'
import { Comment } from 'domain/comments'
/**
* Firbase comment service
*
*
* @export
* @class CommentService
* @implements {ICommentService}
*/
export class CommentService implements ICommentService {
public addComment: (postId: string, comment: Comment)
=> Promise<string> = (postId, comment) => {
return new Promise<string>((resolve,reject) => {
let commentRef: any = firebaseRef.child(`postComments/${postId}`).push(comment)
commentRef.then(() => {
resolve(commentRef.key)
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public getComments: ()
=> Promise<{ [postId: string]: { [commentId: string]: Comment } }> = () => {
return new Promise<{ [postId: string]: { [commentId: string]: Comment }}>((resolve,reject) => {
let commentsRef: any = firebaseRef.child(`postComments`)
commentsRef.on('value', (snapshot: any) => {
let comments: {[postId: string]: {[commentId: string]: Comment}} = snapshot!.val() || {}
resolve(comments)
})
})
}
}
public updateComment: (userId: string, postId: string, comment: Comment)
=> Promise<void> = (userId, postId, comment) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`postComments/${postId}/${userId}`] = comment
firebaseRef.update(updates)
.then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public deleteComment: (commentId: string, postId: string)
=> Promise<void> = (commentId, postId) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`postComments/${postId}/${commentId}`] = null
firebaseRef.update(updates)
.then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
}