diff --git a/backend/src/resolvers/review.ts b/backend/src/resolvers/review.ts index 3baeb9c1..b644dfe1 100644 --- a/backend/src/resolvers/review.ts +++ b/backend/src/resolvers/review.ts @@ -1,21 +1,51 @@ -import { ObjectId } from "bson"; import User from "../models/User"; import Review from "../models/Review"; +import { bucket } from "../utils/firebase"; +import { ObjectId } from "bson"; + +const upload = (file, uid) => { + const { createReadStream, mimetype } = file; + const newFileName = `review-detail/${Date.now()}`; + + return new Promise((resolve, reject) => { + createReadStream() + .pipe( + bucket.file(newFileName).createWriteStream({ + resumable: false, + gzip: true, + contentType: mimetype, + public: true + }) + ) + .on("error", reject) + .on( + "finish", + resolve( + `https://firebasestorage.googleapis.com/v0/b/${ + bucket.name + }/o/${encodeURIComponent(newFileName)}?alt=media&token=${uid}` + ) + ); + }); +}; + const resolver = { Mutation: { - createReview: async (_, arg, context) => { + createReview: async (_, args, context) => { const { uid } = await context?.user; const userId = await User.findOne({ uid }).select("_id"); - const file = await arg.imageCover; - console.log(file); - // const review = await Review.create({ - // ...arg, - // user: userId, - // status: "draft" - // }); + const file = await args.imageCover; + const downloadUrl = await upload(file, uid); + + const review = await Review.create({ + ...args, + user: userId, + status: "draft", + imageCover: downloadUrl + }); - // return review; + return review; } } }; diff --git a/backend/src/utils/firebase.ts b/backend/src/utils/firebase.ts index e38491d2..29bbcea1 100644 --- a/backend/src/utils/firebase.ts +++ b/backend/src/utils/firebase.ts @@ -8,4 +8,6 @@ const firebaseAdmin = admin.initializeApp({ storageBucket: "revhere-1b66c.appspot.com" }); +export const bucket = firebaseAdmin.storage().bucket(); + export default firebaseAdmin; diff --git a/frontend/pages/create-review-detail.tsx b/frontend/pages/create-review-detail.tsx index 0606675b..0c83ebb7 100644 --- a/frontend/pages/create-review-detail.tsx +++ b/frontend/pages/create-review-detail.tsx @@ -1,14 +1,18 @@ +/* eslint-disable react/display-name */ import React, { FunctionComponent } from 'react'; import { Layouts } from 'components/Layout'; import { withApollo } from 'providers/web-client/with-apollo-client'; import { CreateReview } from 'containers/create-review'; -const page: FunctionComponent = () => { - return ( - - - - ); +export default () => { + return
test
; }; +// const page: FunctionComponent = () => { +// return ( +// +// +// +// ); +// }; -export default withApollo(page, { ssr: false }); +// export default withApollo(page, { ssr: false });