create account

블록체인에서 친구 추가 기능의 구현 by dabble

View this thread on: hive.blogpeakd.comecency.com
· @dabble ·
$0.52
블록체인에서 친구 추가 기능의 구현
![](https://cdn.steemitimages.com/DQmW4SKLyMPpeyedbY1hjyhr2cpEkXomECcVTnqFx2VJ7XS/image.png)

안녕하세요. 오늘은 블록체인 기반 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);
	  });
  });
~~~

보시기에 어려운 부분은 없을것으로 생각되지만..혹시나 궁금하신 것이 있으시면 댓글을 달아주세요.
👍  , , , , , , ,
properties (23)
authordabble
permlink5abbwf
categorykr-dev
json_metadata{"tags":["kr-dev","kr","eos","jjangjjangman"],"image":["https://cdn.steemitimages.com/DQmW4SKLyMPpeyedbY1hjyhr2cpEkXomECcVTnqFx2VJ7XS/image.png"],"app":"steemit/0.1","format":"markdown"}
created2018-07-26 08:15:54
last_update2018-07-26 08:15:54
depth0
children6
last_payout2018-08-02 08:15:54
cashout_time1969-12-31 23:59:59
total_payout_value0.468 HBD
curator_payout_value0.049 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,462
author_reputation262,388,560,746
root_title"블록체인에서 친구 추가 기능의 구현"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,018,706
net_rshares272,370,697,728
author_curate_reward""
vote details (8)
@chenish ·
$0.19
문과생은 오늘도 한글이 한글같지 않습니다.ㅠㅠ
👍  ,
properties (23)
authorchenish
permlinkre-dabble-5abbwf-20180727t075321018z
categorykr-dev
json_metadata{"tags":["kr-dev"],"app":"steemit/0.1"}
created2018-07-27 07:53:15
last_update2018-07-27 07:53:15
depth1
children0
last_payout2018-08-03 07:53:15
cashout_time1969-12-31 23:59:59
total_payout_value0.172 HBD
curator_payout_value0.016 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length25
author_reputation28,849,557,175
root_title"블록체인에서 친구 추가 기능의 구현"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,130,023
net_rshares102,802,441,756
author_curate_reward""
vote details (2)
@dabble ·
$0.20
내가 follow한 사람과, 나를 follow한 사람의 숫자를 구하는 interface가 필요함. 이건 나중에~~~?
👍  ,
properties (23)
authordabble
permlinkre-dabble-5abbwf-20180726t093300941z
categorykr-dev
json_metadata{"tags":["kr-dev"],"app":"steemit/0.1"}
created2018-07-26 09:33:00
last_update2018-07-26 09:33:00
depth1
children0
last_payout2018-08-02 09:33:00
cashout_time1969-12-31 23:59:59
total_payout_value0.150 HBD
curator_payout_value0.048 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation262,388,560,746
root_title"블록체인에서 친구 추가 기능의 구현"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,024,171
net_rshares105,179,376,825
author_curate_reward""
vote details (2)
@dabble ·
$0.20
ID를 포함하여 search를 하는 것이 필요함.
👍  ,
properties (23)
authordabble
permlinkre-dabble-5abbwf-20180726t093431700z
categorykr-dev
json_metadata{"tags":["kr-dev"],"app":"steemit/0.1"}
created2018-07-26 09:34:33
last_update2018-07-26 09:34:33
depth1
children0
last_payout2018-08-02 09:34:33
cashout_time1969-12-31 23:59:59
total_payout_value0.153 HBD
curator_payout_value0.050 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length27
author_reputation262,388,560,746
root_title"블록체인에서 친구 추가 기능의 구현"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,024,280
net_rshares107,556,311,895
author_curate_reward""
vote details (2)
@steemitboard ·
Congratulations @dabble! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/comments.png)](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**!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-dabble-20180726t110212000z
categorykr-dev
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2018-07-26 11:02:12
last_update2018-07-26 11:02:12
depth1
children0
last_payout2018-08-02 11:02:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length719
author_reputation38,975,615,169,260
root_title"블록체인에서 친구 추가 기능의 구현"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,030,932
net_rshares0
@steemitboard ·
Congratulations @dabble! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/voted.png)](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**!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-dabble-20180726t204908000z
categorykr-dev
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2018-07-26 20:49:06
last_update2018-07-26 20:49:06
depth1
children0
last_payout2018-08-02 20:49:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length675
author_reputation38,975,615,169,260
root_title"블록체인에서 친구 추가 기능의 구현"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,082,604
net_rshares0
@virus707 ·
$0.20
(jjangjjangman 태그 사용시 댓글을 남깁니다.)
[제 0회 짱짱맨배 42일장]5주차 보상글추천, 1,2,3,4주차 보상지급을 발표합니다.(계속 리스팅 할 예정)
https://steemit.com/kr/@virus707/0-42-5-1-2-3-4

5주차에 도전하세요

그리고 즐거운 스티밋하세요!

👍  , , ,
properties (23)
authorvirus707
permlinkre-dabble-5abbwf-1532729705819t2b44fab5-6a88-4895-b7e5-044dc47405c2uid
categorykr-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"}
created2018-07-27 22:15:06
last_update2018-07-27 22:15:06
depth1
children0
last_payout2018-08-03 22:15:06
cashout_time1969-12-31 23:59:59
total_payout_value0.157 HBD
curator_payout_value0.047 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length174
author_reputation557,563,606,581,756
root_title"블록체인에서 친구 추가 기능의 구현"
beneficiaries
0.
accountsteemj
weight250
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,209,804
net_rshares115,106,734,928
author_curate_reward""
vote details (4)