diff --git a/app/firebaseClient/index.ts b/app/firebaseClient/index.ts new file mode 100644 index 0000000..3f53585 --- /dev/null +++ b/app/firebaseClient/index.ts @@ -0,0 +1,30 @@ +declare const process: any + +import firebase from 'firebase' + +try { + let config = { + apiKey: process.env.API_KEY, + authDomain: process.env.AUTH_DOMAIN, + databaseURL: process.env.DATABASE_URL, + projectId: process.env.PROJECT_ID, + storageBucket: process.env.STORAGE_BUCKET, + messagingSenderId: process.env.MESSAGING_SENDER_ID + } + + firebase.initializeApp(config) +} catch (error) { + console.log('=========Firebase initializer==============') + console.log(error) + console.log('====================================') +} + +// - Storage reference +export let storageRef = firebase.storage().ref() + +// - Database authorize +export let firebaseAuth = firebase.auth +export let firebaseRef = firebase.database().ref() + +// - Firebase default +export default firebase diff --git a/app/firebaseClient/services/authorize/AuthorizeService.ts b/app/firebaseClient/services/authorize/AuthorizeService.ts new file mode 100644 index 0000000..da48f18 --- /dev/null +++ b/app/firebaseClient/services/authorize/AuthorizeService.ts @@ -0,0 +1,106 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebaseClient/' + +import { IAuthorizeService } from 'services/authorize' +import { User } from 'Domain/users' +import { LoginUser, RegisterUserResult } from 'domain/authorize' +import { SocialError } from 'domain/common' + +/** + * Firbase authorize service + * + * @export + * @class AuthorizeService + * @implements {IAuthorizeService} + */ +export class AuthorizeService implements IAuthorizeService { + + /** + * Login the user + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + public login: (email: string, password: string) => Promise = (email, password) => { + + return new Promise((resolve, reject) => { + firebaseAuth() + .signInWithEmailAndPassword(email, password) + .then((result) => { + resolve(new LoginUser(result.uid)) + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + }) + } + + /** + * Logs out the user + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + public logout: () => Promise = () => { + return new Promise((resolve, reject) => { + firebaseAuth() + .signOut() + .then((result) => { + resolve() + }) + .catch((error: any) => { + + reject(new SocialError(error.code, error.message)) + }) + }) + } + + /** + * Register a user + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + public registerUser: (user: User) => Promise = (user) => { + return new Promise((resolve, reject) => { + firebaseAuth() + .createUserWithEmailAndPassword(user.email as string, user.password as string) + .then((signupResult) => { + firebaseRef.child(`users/${signupResult.uid}/info`) + .set({ + ...user, + avatar: 'noImage' + }) + .then((result) => { + resolve(new RegisterUserResult(signupResult.uid)) + }) + .catch((error: any) => reject(new SocialError(error.name, error.message))) + }) + .catch((error: any) => reject(new SocialError(error.code, error.message))) + }) + } + + /** + * Update user password + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + public updatePassword: (newPassword: string) => Promise = (newPassword) => { + + return new Promise((resolve, reject) => { + let user = firebaseAuth().currentUser + if (user) { + user.updatePassword(newPassword).then(() => { + // Update successful. + resolve() + }).catch((error: any) => { + // An error happened. + reject(new SocialError(error.code, error.message)) + }) + } + + }) + } + +} diff --git a/app/firebaseClient/services/authorize/index.ts b/app/firebaseClient/services/authorize/index.ts new file mode 100644 index 0000000..3378979 --- /dev/null +++ b/app/firebaseClient/services/authorize/index.ts @@ -0,0 +1,5 @@ +import { AuthorizeService } from './AuthorizeService' + +export { + AuthorizeService +} diff --git a/app/firebaseClient/services/circles/CircleService.ts b/app/firebaseClient/services/circles/CircleService.ts new file mode 100644 index 0000000..6b7fe3d --- /dev/null +++ b/app/firebaseClient/services/circles/CircleService.ts @@ -0,0 +1,120 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebaseClient/' + +import { SocialError } from 'domain/common' +import { ICircleService } from 'services/circles' +import { Circle, UserFollower } from 'domain/circles' +import { User } from 'domain/users' + +/** + * Firbase circle service + * + * @export + * @class CircleService + * @implements {ICircleService} + */ +export class CircleService implements ICircleService { + + public addCircle: (userId: string, circle: Circle) + => Promise = (userId, circle) => { + return new Promise((resolve,reject) => { + let circleRef = firebaseRef.child(`userCircles/${userId}/circles`).push(circle) + circleRef.then(() => { + resolve(circleRef.key as string) + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + + } + + public addFollowingUser: (userId: string, circleId: string, userCircle: User, userFollower: UserFollower, userFollowingId: string) + => Promise = (userId, circleId, userCircle, userFollower, userFollowingId) => { + return new Promise((resolve,reject) => { + + let updates: any = {} + updates[`userCircles/${userId}/circles/${circleId}/users/${userFollowingId}`] = userCircle + updates[`userCircles/${userFollowingId}/circles/-Followers/users/${userId}`] = userFollower + + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + } + public deleteFollowingUser: (userId: string, circleId: string, userFollowingId: string) + => Promise = (userId, circleId, userFollowingId) => { + return new Promise((resolve,reject) => { + + let updates: any = {} + updates[`userCircles/${userId}/circles/${circleId}/users/${userFollowingId}`] = null + updates[`userCircles/${userFollowingId}/circles/-Followers/users/${userId}`] = null + + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + } + public updateCircle: (userId: string, circleId: string, circle: Circle) + => Promise = (userId, circleId, circle) => { + return new Promise((resolve,reject) => { + + let updates: any = {} + updates[`userCircles/${userId}/circles/${circleId}`] = circle + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + } + + public deleteCircle: (userId: string, circleId: string) + => Promise = (userId, circleId) => { + return new Promise((resolve,reject) => { + + let updates: any = {} + updates[`userCircles/${userId}/circles/${circleId}`] = null + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + } + public getCircles: (userId: string) => Promise<{ [circleId: string]: Circle }> = (userId) => { + return new Promise<{ [circleId: string]: Circle }>((resolve,reject) => { + let circlesRef: any = firebaseRef.child(`userCircles/${userId}/circles`) + + circlesRef.once('value').then((snapshot: any) => { + let circles: any = snapshot.val() || {} + let parsedCircles: { [circleId: string]: Circle } = {} + Object.keys(circles).forEach((circleId) => { + + parsedCircles[circleId] = { + id: circleId, + ...circles[circleId] + } + }) + resolve(parsedCircles) + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + } +} diff --git a/app/firebaseClient/services/circles/index.ts b/app/firebaseClient/services/circles/index.ts new file mode 100644 index 0000000..e907c11 --- /dev/null +++ b/app/firebaseClient/services/circles/index.ts @@ -0,0 +1,5 @@ +import { CircleService } from './CircleService' + +export { + CircleService +} diff --git a/app/firebaseClient/services/comments/CommentService.ts b/app/firebaseClient/services/comments/CommentService.ts new file mode 100644 index 0000000..7538090 --- /dev/null +++ b/app/firebaseClient/services/comments/CommentService.ts @@ -0,0 +1,71 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebaseClient/' + +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 = (postId, comment) => { + return new Promise((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 = (userId, postId, comment) => { + return new Promise((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 = (commentId, postId) => { + return new Promise((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)) + }) + }) + } +} diff --git a/app/firebaseClient/services/comments/index.ts b/app/firebaseClient/services/comments/index.ts new file mode 100644 index 0000000..e4f9a1e --- /dev/null +++ b/app/firebaseClient/services/comments/index.ts @@ -0,0 +1,5 @@ +import { CommentService } from './CommentService' + +export { + CommentService +} \ No newline at end of file diff --git a/app/firebaseClient/services/common/CommonService.ts b/app/firebaseClient/services/common/CommonService.ts new file mode 100644 index 0000000..847d8b4 --- /dev/null +++ b/app/firebaseClient/services/common/CommonService.ts @@ -0,0 +1,17 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebaseClient/' + +import { SocialError } from 'domain/common' +import { ICommonService } from 'services/common' + +/** + * Firbase common service + * + * @export + * @class CommonService + * @implements {ICommonService} + */ +export class CommonService implements ICommonService { + + +} \ No newline at end of file diff --git a/app/firebaseClient/services/common/index.ts b/app/firebaseClient/services/common/index.ts new file mode 100644 index 0000000..7470140 --- /dev/null +++ b/app/firebaseClient/services/common/index.ts @@ -0,0 +1,5 @@ +import { CommonService } from './CommonService' + +export { + CommonService +} \ No newline at end of file diff --git a/app/firebaseClient/services/imageGallery/ImageGalleryService.ts b/app/firebaseClient/services/imageGallery/ImageGalleryService.ts new file mode 100644 index 0000000..1fbaae9 --- /dev/null +++ b/app/firebaseClient/services/imageGallery/ImageGalleryService.ts @@ -0,0 +1,110 @@ +// - Import react components +import { firebaseRef, firebaseAuth, storageRef } from 'app/firebaseClient/' + +import { SocialError } from 'domain/common' +import { IImageGalleryService } from 'services/imageGallery' +import { Image } from 'domain/imageGallery' + +/** + * Firbase image gallery service + * + * @export + * @class ImageGalleryService + * @implements {IImageGalleryService} + */ +export class ImageGalleryService implements IImageGalleryService { + + public getImageGallery: (userId: string) + => Promise = (userId) => { + return new Promise((resolve,reject) => { + let imagesRef: any = firebaseRef.child(`userFiles/${userId}/files/images`) + + imagesRef.once('value').then((snapshot: any) => { + let images = snapshot.val() || {} + let parsedImages: Image[] = [] + Object.keys(images).forEach((imageId) => { + parsedImages.push({ + id: imageId, + ...images[imageId] + }) + }) + resolve(parsedImages) + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + } + + public saveImage: (userId: string, image: Image) + => Promise = (userId, image) => { + return new Promise((resolve,reject) => { + + let imageRef = firebaseRef.child(`userFiles/${userId}/files/images`).push(image) + imageRef.then(() => { + resolve(imageRef.key!) + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + } + + public deleteImage: (userId: string, imageId: string) + => Promise = (userId, imageId) => { + return new Promise((resolve,reject) => { + + let updates: any = {} + updates[`userFiles/${userId}/files/images/${imageId}`] = null + + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + + }) + } + + public uploadImage: (file: any, fileName: string, progressCallback: Function) + => Promise = (file, fileName, progressCallback) => { + return new Promise((resolve,reject) => { + + let storegeFile: any = storageRef.child(`images/${fileName}`) + + // Upload file + let task: any = storegeFile.put(file) + + // Upload storage bar + task.on('state_changed', (snapshot: any) => { + let percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100 + progressCallback(percentage) + + }, (error: any) => { + reject(new SocialError(error.code, error.message)) + }, () => { + resolve() + }) + }) + } + + public downloadImage: (fileName: string) + => Promise = (fileName) => { + return new Promise((resolve,reject) => { + + // Create a reference to the file we want to download + let starsRef: any = storageRef.child(`images/${fileName}`) + + // Get the download URL + starsRef.getDownloadURL().then((url: string) => { + resolve(url) + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + }) + } +} diff --git a/app/firebaseClient/services/imageGallery/index.ts b/app/firebaseClient/services/imageGallery/index.ts new file mode 100644 index 0000000..787b63d --- /dev/null +++ b/app/firebaseClient/services/imageGallery/index.ts @@ -0,0 +1,5 @@ +import { ImageGalleryService } from './ImageGalleryService' + +export { + ImageGalleryService +} \ No newline at end of file diff --git a/app/firebaseClient/services/index.ts b/app/firebaseClient/services/index.ts new file mode 100644 index 0000000..0dee94c --- /dev/null +++ b/app/firebaseClient/services/index.ts @@ -0,0 +1,21 @@ +import { AuthorizeService } from './authorize' +import { CircleService } from './circles' +import { CommentService } from './comments' +import { CommonService } from './common' +import { ImageGalleryService } from './imageGallery' +import { NotificationService } from './notifications' +import { PostService } from './posts' +import { UserService } from './users' +import { VoteService } from './votes' + +export { + AuthorizeService, + CircleService, + CommentService, + CommonService, + ImageGalleryService, + NotificationService, + PostService, + UserService, + VoteService +} diff --git a/app/firebaseClient/services/notifications/index.ts b/app/firebaseClient/services/notifications/index.ts new file mode 100644 index 0000000..943f9ef --- /dev/null +++ b/app/firebaseClient/services/notifications/index.ts @@ -0,0 +1,5 @@ +import { NotificationService } from './NotificationService' + +export { + NotificationService +} \ No newline at end of file diff --git a/app/firebaseClient/services/notifications/notificationService.ts b/app/firebaseClient/services/notifications/notificationService.ts new file mode 100644 index 0000000..488a145 --- /dev/null +++ b/app/firebaseClient/services/notifications/notificationService.ts @@ -0,0 +1,72 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebaseClient/' + +import { SocialError } from 'domain/common' +import { Notification } from 'domain/notifications' +import { INotificationService } from 'services/notifications' + +/** + * Firbase notification service + * + * @export + * @class NotificationService + * @implements {INotificationService} + */ +export class NotificationService implements INotificationService { + + public addNotification: (notification: Notification) + => Promise = (notification: Notification) => { + return new Promise((resolve,reject) => { + firebaseRef.child(`userNotifies/${notification.notifyRecieverUserId}`) + .push(notification) + .then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + }) + } + + public getNotifications: (userId: string) + => Promise<{ [notifyId: string]: Notification }> = (userId) => { + return new Promise<{ [notifyId: string]: Notification }>((resolve,reject) => { + let notifiesRef: any = firebaseRef.child(`userNotifies/${userId}`) + notifiesRef.on('value', (snapshot: any) => { + let notifies: {[notifyId: string]: Notification} = snapshot.val() || {} + resolve(notifies) + }) + }) + } + + public deleteNotification: (notificationId: string, userId: string) + => Promise = (notificationId, userId) => { + return new Promise((resolve, reject) => { + let updates: any = {} + updates[`userNotifies/${userId}/${notificationId}`] = null + firebaseRef.update(updates) + .then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + }) + } + + public setSeenNotification: (notificationId: string, userId: string, notification: Notification) + => Promise = (notificationId, userId, notification) => { + return new Promise((resolve, reject) => { + let updates: any = {} + updates[`userNotifies/${userId}/${notificationId}`] = notification + firebaseRef.update(updates) + .then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)) + }) + }) + } + +} diff --git a/app/firebaseClient/services/posts/PostService.ts b/app/firebaseClient/services/posts/PostService.ts new file mode 100644 index 0000000..1fdb70d --- /dev/null +++ b/app/firebaseClient/services/posts/PostService.ts @@ -0,0 +1,101 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebaseClient/' + +import { SocialError } from 'domain/common' +import { Post } from 'domain/posts' +import { IPostService } from 'services/posts' + +/** + * Firbase post service + * + * @export + * @class PostService + * @implements {IPostService} + */ +export class PostService implements IPostService { + + public addPost: (userId: string, post: Post) + => Promise = (userId, post) => { + return new Promise((resolve,reject) => { + let postRef: any = firebaseRef.child(`userPosts/${userId}/posts`).push(post) + postRef.then(() => { + resolve(postRef.key) + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + + public updatePost: (userId: string, postId: string, post: Post) + => Promise = (userId, postId, post) => { + return new Promise((resolve,reject) => { + let updates: any = {} + updates[`userPosts/${userId}/posts/${postId}`] = post + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + + public deletePost: (userId: string, postId: string) + => Promise = (userId, postId) => { + return new Promise((resolve,reject) => { + let updates: any = {} + updates[`userPosts/${userId}/posts/${postId}`] = null + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + + public getPosts: (userId: string) + => Promise<{ [postId: string]: Post }> = (userId) => { + return new Promise<{ [postId: string]: Post }>((resolve,reject) => { + + let postsRef: any = firebaseRef.child(`userPosts/${userId}/posts`) + postsRef.once('value').then((snapshot: any) => { + let posts: any = snapshot.val() || {} + let parsedPosts: { [postId: string]: Post } = {} + + Object.keys(posts).forEach((postId) => { + parsedPosts[postId] = { + id: postId, + ...posts[postId] + } + }) + resolve(parsedPosts) + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + + public getPostById: (userId: string, postId: string) + => Promise = (userId, postId) => { + return new Promise((resolve,reject) => { + + let postsRef: any = firebaseRef.child(`userPosts/${userId}/posts/${postId}`) + + postsRef.once('value').then((snapshot: any) => { + let newPost = snapshot.val() || {} + let post: Post = { + id: postId, + ...newPost + } + resolve(post) + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + + }) + } +} diff --git a/app/firebaseClient/services/posts/index.ts b/app/firebaseClient/services/posts/index.ts new file mode 100644 index 0000000..e9c7fd6 --- /dev/null +++ b/app/firebaseClient/services/posts/index.ts @@ -0,0 +1,5 @@ +import { PostService } from './PostService' + +export { + PostService +} \ No newline at end of file diff --git a/app/firebaseClient/services/users/UserService.ts b/app/firebaseClient/services/users/UserService.ts new file mode 100644 index 0000000..4ab99ba --- /dev/null +++ b/app/firebaseClient/services/users/UserService.ts @@ -0,0 +1,73 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebaseClient/' + +import { SocialError } from 'domain/common' +import { Profile } from 'domain/users' +import { IUserService } from 'services/users' + +/** + * Firbase user service + * + * @export + * @class UserService + * @implements {IUserService} + */ +export class UserService implements IUserService { + public getUserProfile: (userId: string) + => Promise = (userId) => { + return new Promise((resolve,reject) => { + let userProfileRef: any = firebaseRef.child(`users/${userId}/info`) + + userProfileRef.once('value').then((snapshot: any) => { + let userProfile: Profile = snapshot.val() || {} + resolve(userProfile) + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + + public updateUserProfile: (userId: string, profile: Profile) + => Promise = (userId, profile) => { + return new Promise((resolve,reject) => { + let updates: any = {} + + updates[`users/${userId}/info`] = profile + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + public getUsersProfile: (userId: string) + => Promise<{ [userId: string]: Profile }> = (userId) => { + return new Promise<{ [userId: string]: Profile }>((resolve,reject) => { + let usersProfileRef: any = firebaseRef.child(`users`) + + usersProfileRef.once('value').then((snapshot: any) => { + let usersProfile: any = snapshot.val() || {} + let parsedusersProfile: {[userId: string]: Profile} = {} + Object.keys(usersProfile).forEach((userKey) => { + if (userId !== userKey) { + let userInfo = usersProfile[userKey].info + parsedusersProfile[userKey] = { + avatar: userInfo.avatar, + email: userInfo.email, + fullName: userInfo.fullName, + banner: userInfo.banner, + tagLine: userInfo.tagLine + } + } + }) + resolve(parsedusersProfile) + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + +} diff --git a/app/firebaseClient/services/users/index.ts b/app/firebaseClient/services/users/index.ts new file mode 100644 index 0000000..d883994 --- /dev/null +++ b/app/firebaseClient/services/users/index.ts @@ -0,0 +1,5 @@ +import { UserService } from './UserService' + +export { + UserService +} \ No newline at end of file diff --git a/app/firebaseClient/services/votes/VoteService.ts b/app/firebaseClient/services/votes/VoteService.ts new file mode 100644 index 0000000..223afbc --- /dev/null +++ b/app/firebaseClient/services/votes/VoteService.ts @@ -0,0 +1,57 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebaseClient/' + +import { SocialError } from 'domain/common' +import { Vote } from 'domain/votes' +import { IVoteService } from 'services/votes' + +/** + * Firbase vote service + * + * @export + * @class VoteService + * @implements {IVoteService} + */ +export class VoteService implements IVoteService { + + public addVote: (vote: Vote) + => Promise = (vote) => { + return new Promise((resolve,reject) => { + let voteRef = firebaseRef.child(`postVotes/${vote.postId}`) + .push(vote) + voteRef.then(() => { + resolve(voteRef.key!) + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + public getVotes: () + => Promise<{ [postId: string]: { [voteId: string]: Vote } }> = () => { + return new Promise<{ [postId: string]: { [voteId: string]: Vote } }>((resolve,reject) => { + let votesRef: any = firebaseRef.child(`postVotes`) + + votesRef.on('value',(snapshot: any) => { + let postVotes: {[postId: string]: {[voteId: string]: Vote}} = snapshot.val() || {} + resolve(postVotes) + }) + + }) + } + + public deleteVote: (voteId: string, postId: string) + => Promise = (voteId, postId) => { + return new Promise((resolve,reject) => { + let updates: any = {} + updates[`postVotes/${postId}/${voteId}`] = null + firebaseRef.update(updates).then(() => { + resolve() + }) + .catch((error: any) => { + reject(new SocialError(error.code,error.message)) + }) + }) + } + +} diff --git a/app/firebaseClient/services/votes/index.ts b/app/firebaseClient/services/votes/index.ts new file mode 100644 index 0000000..8859aa9 --- /dev/null +++ b/app/firebaseClient/services/votes/index.ts @@ -0,0 +1,5 @@ +import { VoteService } from './VoteService' + +export { + VoteService +}