Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👽 Finish create review api #17

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions backend/src/resolvers/review.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
};
Expand Down
2 changes: 2 additions & 0 deletions backend/src/utils/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ const firebaseAdmin = admin.initializeApp({
storageBucket: "revhere-1b66c.appspot.com"
});

export const bucket = firebaseAdmin.storage().bucket();

export default firebaseAdmin;
18 changes: 11 additions & 7 deletions frontend/pages/create-review-detail.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Layouts>
<CreateReview />
</Layouts>
);
export default () => {
return <div>test</div>;
};
// const page: FunctionComponent = () => {
// return (
// <Layouts>
// <CreateReview />
// </Layouts>
// );
// };

export default withApollo(page, { ssr: false });
// export default withApollo(page, { ssr: false });