 안녕하세요. 오늘은 블록체인 기반 Dapp에서 친구 추가 기능에 대해서 이야기 해보려고 합니다. <H4>편의상 문체가 제 자신에게 편하게 하는 것이라, 경어가 없는 부분은 이해 부탁 드립니다. </H4> 사실 친구 추가는 굉장히 쉬운 기능이라고 생각이 되지만, 문제는 그 뒤에 딸린 것이다. 친구의 글을 어떻게 화면에 보여줄지, 그리고 어떤 순서로 보여줄지 등등..이 어려운 부분은 UX engineer에게 맡기기로 하고.. 우선 오늘은 친구 추가에 대해서 구현을 해보려고 한다. <H4>Interface</H4> Web쪽에 계신 분들이 CRUD이야기를 많이 하셔서 오늘의 명칭은 이것에 따라 보려고 한다. createfriend readfriends 이것만 복수임의 유의! update는..없으니 생략 deletefriend 각각의 parameter는.. createfriend input : account (친구의 account정보) return : success or fail readfriends input : N/A (session정보를 기반으로 자동으로 account를 추출) return : result : data : friend account array deletefriend : create friend와 동일. <H4>DB구조</H4> account를 key로 하고, 여기에 friend를 pair로 묶는 구조로, 친구가 3명이면... account friend pair가 3개가 되는 단순한 형태이다. DB 이름 : follower <H4>Operation</H4> createfriend follower DB에 account, friend pair를 기록. 만약 기존에 같은 값이 있으면 fail을 return readfriends account를 key로 하여 읽은 값을 그대로 return, data라는 이름의 array + result delete friend account, friend pair로 찾은 값을 삭제하고 결과를 return <h4>coding</h4> 아래는 실제 code입니다. CRUD method들...DB읽기 실패 처리는 있으나..fail return은 없음..! ~~~ const mongo = require('mongodb'); const ObjectId = require('mongodb').ObjectId; const MongoClient = require('mongodb').MongoClient; const url = process.env.MONGODB_URI; exports.createFriend = function(account, friend, callback){ MongoClient.connect(url, function(err, db) { const dbo = db.db("heroku_dg3d93pq"); const myObj = {account : account, follower : friend}; dbo.collection("board").insertOne(myobj, function(err, res){ if (err) throw err; console.log("1 follower inserted", account, friend); callback("OK"); db.close(); }); }); } exports.readFriends = function(account, friend, callback){ MongoClient.connect(url, function(err, db) { const dbo = db.db("heroku_dg3d93pq"); const findQuery = {account : account}; dbo.collection("board").find(findQuery).toArray(function(err, res){ if (err) throw err; console.log("reading follower", res.length); //make body var body = []; for(i = 0;i < res.length; i++){ body.push({ data : res[i].friend }); } callback(body); db.close(); }); }); } exports.deleteFriend = function(account, callback){ MongoClient.connect(url, function(err, db) { const dbo = db.db("heroku_dg3d93pq"); const deleteQuery = {account : account, follower : friend}; dbo.collection("board").deleteOne(deleteQuery, function(err, res){ if (err) throw err; console.log("1 follower deleted", account, friend); callback("OK"); db.close(); }); }); } ~~~ 이건 Post method의 routing처리.. 모두 callback type으로 맞추니 copy & paste수준으로 처리 가능함. ~~~ app.post("/createfriend", function(req, res) { const friend = req.body.account; console.log("createfriend event", friend); follower.createFriend(req.session.account, friend, (result)=>{ res.send(result); }); }); app.post("/deletefriend", function(req, res) { const friend = req.body.account; console.log("deletefriend event", friend); follower.deleteFriend(req.session.account, friend, (result)=>{ res.send(result); }); }); app.post("/readfriends", function(req, res) { console.log("readfriends event"); follower.readFriends(req.session.account,(result)=>{ res.send(result); }); }); ~~~ 보시기에 어려운 부분은 없을것으로 생각되지만..혹시나 궁금하신 것이 있으시면 댓글을 달아주세요.
author | dabble |
---|---|
permlink | 5abbwf |
category | kr-dev |
json_metadata | {"tags":["kr-dev","kr","eos","jjangjjangman"],"image":["https://cdn.steemitimages.com/DQmW4SKLyMPpeyedbY1hjyhr2cpEkXomECcVTnqFx2VJ7XS/image.png"],"app":"steemit/0.1","format":"markdown"} |
created | 2018-07-26 08:15:54 |
last_update | 2018-07-26 08:15:54 |
depth | 0 |
children | 6 |
last_payout | 2018-08-02 08:15:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.468 HBD |
curator_payout_value | 0.049 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 3,462 |
author_reputation | 262,388,560,746 |
root_title | "블록체인에서 친구 추가 기능의 구현" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 66,018,706 |
net_rshares | 272,370,697,728 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
anomaly | 0 | 241,575,070 | 1% | ||
steemitboard | 0 | 441,343,634 | 1% | ||
virus707 | 0 | 110,706,363,889 | 1% | ||
brainstormot | 0 | 40,186,785,275 | 100% | ||
mishana | 0 | 16,254,813,739 | 60% | ||
jeaimetu | 0 | 103,955,401,790 | 100% | ||
chenish | 0 | 584,414,331 | 100% | ||
keysersozet | 0 | 0 | 100% |
문과생은 오늘도 한글이 한글같지 않습니다.ㅠㅠ
author | chenish |
---|---|
permlink | re-dabble-5abbwf-20180727t075321018z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2018-07-27 07:53:15 |
last_update | 2018-07-27 07:53:15 |
depth | 1 |
children | 0 |
last_payout | 2018-08-03 07:53:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.172 HBD |
curator_payout_value | 0.016 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 25 |
author_reputation | 28,849,557,175 |
root_title | "블록체인에서 친구 추가 기능의 구현" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 66,130,023 |
net_rshares | 102,802,441,756 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
jeaimetu | 0 | 102,802,441,756 | 100% | ||
keysersozet | 0 | 0 | 100% |
내가 follow한 사람과, 나를 follow한 사람의 숫자를 구하는 interface가 필요함. 이건 나중에~~~?
author | dabble |
---|---|
permlink | re-dabble-5abbwf-20180726t093300941z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2018-07-26 09:33:00 |
last_update | 2018-07-26 09:33:00 |
depth | 1 |
children | 0 |
last_payout | 2018-08-02 09:33:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.150 HBD |
curator_payout_value | 0.048 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 65 |
author_reputation | 262,388,560,746 |
root_title | "블록체인에서 친구 추가 기능의 구현" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 66,024,171 |
net_rshares | 105,179,376,825 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
jeaimetu | 0 | 105,179,376,825 | 100% | ||
keysersozet | 0 | 0 | 100% |
ID를 포함하여 search를 하는 것이 필요함.
author | dabble |
---|---|
permlink | re-dabble-5abbwf-20180726t093431700z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2018-07-26 09:34:33 |
last_update | 2018-07-26 09:34:33 |
depth | 1 |
children | 0 |
last_payout | 2018-08-02 09:34:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.153 HBD |
curator_payout_value | 0.050 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 27 |
author_reputation | 262,388,560,746 |
root_title | "블록체인에서 친구 추가 기능의 구현" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 66,024,280 |
net_rshares | 107,556,311,895 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
jeaimetu | 0 | 107,556,311,895 | 100% | ||
keysersozet | 0 | 0 | 100% |
Congratulations @dabble! You have completed the following achievement on Steemit and have been rewarded with new badge(s) : [](http://steemitboard.com/@dabble) Award for the number of comments <sub>_Click on the badge to view your Board of Honor._</sub> <sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub> To support your work, I also upvoted your post! > Do you like [SteemitBoard's project](https://steemit.com/@steemitboard)? Then **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
author | steemitboard |
---|---|
permlink | steemitboard-notify-dabble-20180726t110212000z |
category | kr-dev |
json_metadata | {"image":["https://steemitboard.com/img/notify.png"]} |
created | 2018-07-26 11:02:12 |
last_update | 2018-07-26 11:02:12 |
depth | 1 |
children | 0 |
last_payout | 2018-08-02 11:02:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 719 |
author_reputation | 38,975,615,169,260 |
root_title | "블록체인에서 친구 추가 기능의 구현" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 66,030,932 |
net_rshares | 0 |
Congratulations @dabble! You have completed the following achievement on Steemit and have been rewarded with new badge(s) : [](http://steemitboard.com/@dabble) Award for the number of upvotes received <sub>_Click on the badge to view your Board of Honor._</sub> <sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub> > Do you like [SteemitBoard's project](https://steemit.com/@steemitboard)? Then **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
author | steemitboard |
---|---|
permlink | steemitboard-notify-dabble-20180726t204908000z |
category | kr-dev |
json_metadata | {"image":["https://steemitboard.com/img/notify.png"]} |
created | 2018-07-26 20:49:06 |
last_update | 2018-07-26 20:49:06 |
depth | 1 |
children | 0 |
last_payout | 2018-08-02 20:49:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 675 |
author_reputation | 38,975,615,169,260 |
root_title | "블록체인에서 친구 추가 기능의 구현" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 66,082,604 |
net_rshares | 0 |
(jjangjjangman 태그 사용시 댓글을 남깁니다.) [제 0회 짱짱맨배 42일장]5주차 보상글추천, 1,2,3,4주차 보상지급을 발표합니다.(계속 리스팅 할 예정) https://steemit.com/kr/@virus707/0-42-5-1-2-3-4 5주차에 도전하세요 그리고 즐거운 스티밋하세요!
author | virus707 | ||||||
---|---|---|---|---|---|---|---|
permlink | re-dabble-5abbwf-1532729705819t2b44fab5-6a88-4895-b7e5-044dc47405c2uid | ||||||
category | kr-dev | ||||||
json_metadata | {"tags":["support"],"users":["virus707"],"links":["https://steemit.com/kr/@virus707/0-42-5-1-2-3-4"],"app":"null/null","format":"markdown"} | ||||||
created | 2018-07-27 22:15:06 | ||||||
last_update | 2018-07-27 22:15:06 | ||||||
depth | 1 | ||||||
children | 0 | ||||||
last_payout | 2018-08-03 22:15:06 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.157 HBD | ||||||
curator_payout_value | 0.047 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 174 | ||||||
author_reputation | 557,563,606,581,756 | ||||||
root_title | "블록체인에서 친구 추가 기능의 구현" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 66,209,804 | ||||||
net_rshares | 115,106,734,928 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
sou1iane | 0 | 162,481,814 | 2.5% | ||
penghuren | 0 | 205,370,897 | 3.75% | ||
jeaimetu | 0 | 114,738,882,217 | 100% | ||
keysersozet | 0 | 0 | 100% |