From 10877e883722821e8b694489976d7cb8f24f791a Mon Sep 17 00:00:00 2001 From: Claire Shu Date: Mon, 14 May 2018 11:40:32 -0700 Subject: [PATCH] working state with distribute bounty and rerouted addresses and no duplicate key errors --- public/app.js | 11 ++++++----- public/app/js/metamask.js | 2 +- public/app/js/model.js | 20 +++++++++++++------- public/app/js/question-view.js | 7 +++++-- public/app/question_view.html | 7 ++++++- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/public/app.js b/public/app.js index f63f54a..67c106e 100644 --- a/public/app.js +++ b/public/app.js @@ -57,10 +57,11 @@ app.post('/submit_question', function(request, response) { console.log("POST /submit_question", "title: " + request.body.title, "details: " + request.body.details, "user_id: " + request.body.user_id, "bounty: " + request.body.bounty); let bounty = Number(request.body.bounty); + let user_addr = request.body.user_id let placeholder_id = ObjectId("73b312067720199e377e6fb9"); // random 24 digit hex string let questionHash = sha256(bounty + request.body.title + request.body.details + request.body.time_exp + placeholder_id); - model.createQuestion(bounty, request.body.time_exp, request.body.title, request.body.details, placeholder_id, questionHash); + model.createQuestion(bounty, request.body.time_exp, request.body.title, request.body.details, placeholder_id, questionHash, user_addr); response.set('Content-type', 'application/json'); response.status(STATUS_OK); @@ -74,18 +75,18 @@ app.post('/submit_question', function(request, response) { app.post('/upvote', function(request, response) { console.log("POST /upvotes " + "user_ID: " + request.body.user_id + "; answer_ID: " + request.body.answer_id); let answer_id = ObjectId(request.body.answer_id); - let user_id = ObjectId(request.body.user_id); - model.upvoteAnswer(answer_id, user_id); + let user_addr = request.body.user_id; + model.upvoteAnswer(answer_id, user_addr); response.set('Content-type', 'application/json'); response.status(STATUS_OK); response.send(); }) app.post('/add_answer', function(request, response) { - console.log("POST /add_answer " + "question_ID: " + request.body.question_id + "; answer: " + request.body.text) + console.log("POST /add_answer " + "question_ID: " + request.body.question_id + "; answer: " + request.body.text) let placeholder_id = ObjectId("915bed12d3704298d62224fe"); let question_id = ObjectId(request.body.question_id); - model.createAnswer(placeholder_id, question_id, request.body.text); + model.createAnswer(request.body.user_addr, question_id, request.body.text); response.set('Content-type', 'application/json'); response.status(STATUS_OK); response.send(); diff --git a/public/app/js/metamask.js b/public/app/js/metamask.js index 59f1b6f..f3e4e0b 100644 --- a/public/app/js/metamask.js +++ b/public/app/js/metamask.js @@ -33,7 +33,7 @@ function app() { asker_address: userAccount } - addPostRequest.open('POST', '/submit_question') + addPostRequest.open('POST', '/create_user') addPostRequest.setRequestHeader('Content-type', 'application/json') addPostRequest.send(JSON.stringify(user_details)); diff --git a/public/app/js/model.js b/public/app/js/model.js index 7c93189..0c8f69c 100644 --- a/public/app/js/model.js +++ b/public/app/js/model.js @@ -21,8 +21,9 @@ var createUser = async function(username, address) { upvotes: [] }); try { - let savedUser = await newUser.save(); + let savedUser = await schema.User.findOneAndUpdate({address: address}, {$setOnInsert: newUser}, {upsert: true, returnNewDocument: true}); console.log("saved user successfully"); + console.log(savedUser); return savedUser.id; } catch (err) { console.log("err in createUser"); @@ -30,7 +31,7 @@ var createUser = async function(username, address) { } } -var createQuestion = async function(bounty, timeExp, title, body, askerId, questionHash) { +var createQuestion = async function(bounty, timeExp, title, body, askerId, questionHash, askerAddr) { console.log("askerId: " + askerId); let newQuestion = new schema.Question ({ answers: [], @@ -45,7 +46,7 @@ var createQuestion = async function(bounty, timeExp, title, body, askerId, quest }); try { let savedQuestion = await newQuestion.save(); - await schema.User.findOneAndUpdate({_id: askerId}, {$push: {questions: savedQuestion.id}}, {upsert: true}); + await schema.User.findOneAndUpdate({address: askerAddr}, {$push: {questions: savedQuestion.id}}, {upsert: true}); return savedQuestion.id; } catch (err) { console.log("error in create question!"); @@ -61,9 +62,10 @@ var createAnswer = async function(answererId, questionId, body) { body: body }); try { + console.log(newAnswer); let savedAnswer = await newAnswer.save(); let updatedQuestion = await schema.Question.findOneAndUpdate({_id: questionId}, {$push: {answers: savedAnswer.id}}, {upsert: true}); - let udpatedUser = await schema.User.findOneAndUpdate({_id: answererId}, {$push: {answers: savedAnswer.id}}, {upsert: true}); + let updatedUser = await schema.User.findOneAndUpdate({address: answererId}, {$push: {answers: savedAnswer.id}}, {upsert: true}); return savedAnswer.id; } catch (err) { console.log("error in createanswer"); @@ -71,10 +73,14 @@ var createAnswer = async function(answererId, questionId, body) { } } -var upvoteAnswer = async function(answerId, voterId) { +var upvoteAnswer = async function(answerId, voterAddr) { + // voterID: 0xC6941bc0804722076716F4ba131D7B7B663E0a92 + // answerID: 5af9d02e092138575b67a58a + // user id wasnt being used and its actually user address. change schema? or figure out new way to do this try { - let updatedAnswer = await schema.Answer.findOneAndUpdate({_id: answerId}, {$push: {voters: voterId}}, {upsert: true}); - let updatedUser = await schema.User.findOneAndUpdate({_id: voterId}, {$push: {answers: answerId}}, {upsert: true}); + let placeholder_id = ObjectId("73b312067720199e377e6fb9"); // random 24 digit hex string + let updatedAnswer = await schema.Answer.findOneAndUpdate({_id: answerId}, {$push: {voters: placeholder_id}}, {upsert: true}); + let updatedUser = await schema.User.findOneAndUpdate({address: voterAddr}, {$push: {answers: answerId}}, {upsert: true}); console.log("updated answer successfully"); } catch (err) { console.log("error in upvote answer"); diff --git a/public/app/js/question-view.js b/public/app/js/question-view.js index 4b1c1f5..d461c81 100644 --- a/public/app/js/question-view.js +++ b/public/app/js/question-view.js @@ -102,9 +102,11 @@ function submitAnswer() { console.log('omg') var box_text = document.getElementById("answer_input").value console.log("answer_submission " + box_text) + console.log(QuestionView.question_id) + console.log(localStorage.getItem("userAccount")) answer_data = { question_id: QuestionView.question_id, - user_id: localStorage.getItem("userAccount"), + user_addr: localStorage.getItem("userAccount"), text: box_text } PostModel.addAnswer(answer_data) @@ -144,9 +146,10 @@ function closeQuestion() { console.log("winner is " + highest_answerer_id); console.log(question_detail); console.log("call metamask"); - distributeBounty(highest_answerer_id, "hi claire", question_detail.question.questionHash); + distributeBounty(highest_answerer_id, 1234, question_detail.question.questionHash); } }); + xmlQuestionDetail.open("GET", QuestionView.remoteHost + 'question_detail' + "?q_id=" + encodeURIComponent(QuestionView.post_data._id)); xmlQuestionDetail.send(null); } diff --git a/public/app/question_view.html b/public/app/question_view.html index e02bdb1..df6daa2 100644 --- a/public/app/question_view.html +++ b/public/app/question_view.html @@ -14,7 +14,7 @@

Pancake's Q&A Blockchain Site

- @@ -66,6 +66,11 @@

I think Max is very cool. I just want to know exactly how cool.

+ + + + + \ No newline at end of file