{"action":"created","comment":{"url":"https://api.github.com/repos/katerina-berestova/short-links/pulls/comments/122046090","pull_request_review_id":44119595,"id":122046090,"diff_hunk":"@@ -0,0 +1,87 @@\n+\"use strict\";\n+\n+const config = require('../../config');\n+const mysql = require('mysql');\n+const sha1 = require('sha1');\n+const {host, user, database, password} = config.db;\n+const connection = mysql.createConnection({ host, user, database, password });\n+\n+connection.connect(err => {\n+  if (err)\n+    return console.error('error connecting: ' + err.stack);\n+  return console.log('connected as id ' + connection.threadId);\n+});\n+\n+function getRandomInt(min, max) {\n+  return Math.random() * (max - min) + min;\n+};\n+\n+function insertRow(url, code) {\n+  return new Promise((resolve, reject) => {\n+    const row = { url, code };\n+    connection.query('INSERT INTO link SET ?', row, (error, results) => {\n+      if (error) return reject(error);\n+      resolve(row);\n+    });\n+  });\n+};\n+\n+function getRowByUrl(url) {\n+  return new Promise((resolve, reject) => {\n+    connection.query('SELECT code FROM link WHERE `url` = ?', url , (error, results) => {\n+      if (error) return reject(error);\n+      resolve(results[0]);\n+    });\n+  });\n+};\n+\n+function getRowByCode(code) {\n+  return new Promise((resolve, reject) => {\n+    connection.query('SELECT url FROM link WHERE `code` = ?', code, (error, results) => {\n+      if (error) return reject(error);\n+      resolve(results[0]);\n+    });\n+  });\n+};\n+\n+function generateUniqueCode() {\n+  return new Promise((resolve, reject) => {\n+    function generate() {\n+      const length = 6;\n+      var hash = sha1(new Date().getTime());\n+      var start = getRandomInt(0, 34);\n+      var code = hash.substring(start, start+length).toUpperCase();\n+      getRowByCode(code).then(result => {\n+        if (!result) generate();\n+        resolve(code);\n+      }).catch(error => reject(error));\n+    }\n+    generate();\n+  });\n+};\n+\n+function save(url) {\n+  return new Promise((resolve, reject) => {\n+    getRowByUrl(url).then(row => {\n+      if (!row) {\n+        generateUniqueCode().then(code => {\n+          return insertRow(url, code)\n+        }).then(row => resolve(row.code));\n+      } else\n+      resolve(row.code);\n+    }).catch(error => reject(error));\n+  });\n+};\n+\n+function getUrlByCode(code) {\n+  return new Promise((resolve, reject) => {\n+    getRowByCode(code).then(row => {\n+      if (!row)\n+        resolve(null);\n+      else\n+        resolve(row.url);\n+    }).catch(error => reject(error));\n+  });\n+};\n+\n+module.exports = { save, getUrlByCode };","path":"app/model/url.js","position":87,"original_position":87,"commit_id":"5bc2ab19595c4d29b814032877f6779826a9a645","original_commit_id":"5bc2ab19595c4d29b814032877f6779826a9a645","user":{"login":"tshashkova","id":14924102,"avatar_url":"https://avatars0.githubusercontent.com/u/14924102?v=3","gravatar_id":"","url":"https://api.github.com/users/tshashkova","html_url":"https://github.com/tshashkova","followers_url":"https://api.github.com/users/tshashkova/followers","following_url":"https://api.github.com/users/tshashkova/following{/other_user}","gists_url":"https://api.github.com/users/tshashkova/gists{/gist_id}","starred_url":"https://api.github.com/users/tshashkova/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tshashkova/subscriptions","organizations_url":"https://api.github.com/users/tshashkova/orgs","repos_url":"https://api.github.com/users/tshashkova/repos","events_url":"https://api.github.com/users/tshashkova/events{/privacy}","received_events_url":"https://api.github.com/users/tshashkova/received_events","type":"User","site_admin":false},"body":"Это нужно писать после всех объявлений переменных. Но до функций. А то не понятно, что вообще экспортируется","created_at":"2017-06-14T19:45:49Z","updated_at":"2017-06-14T19:54:36Z","html_url":"https://github.com/katerina-berestova/short-links/pull/1#discussion_r122046090","pull_request_url":"https://api.github.com/repos/katerina-berestova/short-links/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/katerina-berestova/short-links/pulls/comments/122046090"},"html":{"href":"https://github.com/katerina-berestova/short-links/pull/1#discussion_r122046090"},"pull_request":{"href":"https://api.github.com/repos/katerina-berestova/short-links/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/katerina-berestova/short-links/pulls/1","id":125186530,"html_url":"https://github.com/katerina-berestova/short-links/pull/1","diff_url":"https://github.com/katerina-berestova/short-links/pull/1.diff","patch_url":"https://github.com/katerina-berestova/short-links/pull/1.patch","issue_url":"https://api.github.com/repos/katerina-berestova/short-links/issues/1","number":1,"state":"open","locked":false,"title":"Develop","user":{"login":"katerina-berestova","id":9986202,"avatar_url":"https://avatars1.githubusercontent.com/u/9986202?v=3","gravatar_id":"","url":"https://api.github.com/users/katerina-berestova","html_url":"https://github.com/katerina-berestova","followers_url":"https://api.github.com/users/katerina-berestova/followers","following_url":"https://api.github.com/users/katerina-berestova/following{/other_user}","gists_url":"https://api.github.com/users/katerina-berestova/gists{/gist_id}","starred_url":"https://api.github.com/users/katerina-berestova/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/katerina-berestova/subscriptions","organizations_url":"https://api.github.com/users/katerina-berestova/orgs","repos_url":"https://api.github.com/users/katerina-berestova/repos","events_url":"https://api.github.com/users/katerina-berestova/events{/privacy}","received_events_url":"https://api.github.com/users/katerina-berestova/received_events","type":"User","site_admin":false},"body":"","created_at":"2017-06-12T17:17:35Z","updated_at":"2017-06-14T19:54:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b12110c28dc5fc26b0cd8b9fbef513c7f2e6fae1","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/katerina-berestova/short-links/pulls/1/commits","review_comments_url":"https://api.github.com/repos/katerina-berestova/short-links/pulls/1/comments","review_comment_url":"https://api.github.com/repos/katerina-berestova/short-links/pulls/comments{/number}","comments_url":"https://api.github.com/repos/katerina-berestova/short-links/issues/1/comments","statuses_url":"https://api.github.com/repos/katerina-berestova/short-links/statuses/5bc2ab19595c4d29b814032877f6779826a9a645","head":{"label":"katerina-berestova:develop","ref":"develop","sha":"5bc2ab19595c4d29b814032877f6779826a9a645","user":{"login":"katerina-berestova","id":9986202,"avatar_url":"https://avatars1.githubusercontent.com/u/9986202?v=3","gravatar_id":"","url":"https://api.github.com/users/katerina-berestova","html_url":"https://github.com/katerina-berestova","followers_url":"https://api.github.com/users/katerina-berestova/followers","following_url":"https://api.github.com/users/katerina-berestova/following{/other_user}","gists_url":"https://api.github.com/users/katerina-berestova/gists{/gist_id}","starred_url":"https://api.github.com/users/katerina-berestova/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/katerina-berestova/subscriptions","organizations_url":"https://api.github.com/users/katerina-berestova/orgs","repos_url":"https://api.github.com/users/katerina-berestova/repos","events_url":"https://api.github.com/users/katerina-berestova/events{/privacy}","received_events_url":"https://api.github.com/users/katerina-berestova/received_events","type":"User","site_admin":false},"repo":{"id":94088171,"name":"short-links","full_name":"katerina-berestova/short-links","owner":{"login":"katerina-berestova","id":9986202,"avatar_url":"https://avatars1.githubusercontent.com/u/9986202?v=3","gravatar_id":"","url":"https://api.github.com/users/katerina-berestova","html_url":"https://github.com/katerina-berestova","followers_url":"https://api.github.com/users/katerina-berestova/followers","following_url":"https://api.github.com/users/katerina-berestova/following{/other_user}","gists_url":"https://api.github.com/users/katerina-berestova/gists{/gist_id}","starred_url":"https://api.github.com/users/katerina-berestova/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/katerina-berestova/subscriptions","organizations_url":"https://api.github.com/users/katerina-berestova/orgs","repos_url":"https://api.github.com/users/katerina-berestova/repos","events_url":"https://api.github.com/users/katerina-berestova/events{/privacy}","received_events_url":"https://api.github.com/users/katerina-berestova/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/katerina-berestova/short-links","description":"Short links generator","fork":false,"url":"https://api.github.com/repos/katerina-berestova/short-links","forks_url":"https://api.github.com/repos/katerina-berestova/short-links/forks","keys_url":"https://api.github.com/repos/katerina-berestova/short-links/keys{/key_id}","collaborators_url":"https://api.github.com/repos/katerina-berestova/short-links/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/katerina-berestova/short-links/teams","hooks_url":"https://api.github.com/repos/katerina-berestova/short-links/hooks","issue_events_url":"https://api.github.com/repos/katerina-berestova/short-links/issues/events{/number}","events_url":"https://api.github.com/repos/katerina-berestova/short-links/events","assignees_url":"https://api.github.com/repos/katerina-berestova/short-links/assignees{/user}","branches_url":"https://api.github.com/repos/katerina-berestova/short-links/branches{/branch}","tags_url":"https://api.github.com/repos/katerina-berestova/short-links/tags","blobs_url":"https://api.github.com/repos/katerina-berestova/short-links/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/katerina-berestova/short-links/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/katerina-berestova/short-links/git/refs{/sha}","trees_url":"https://api.github.com/repos/katerina-berestova/short-links/git/trees{/sha}","statuses_url":"https://api.github.com/repos/katerina-berestova/short-links/statuses/{sha}","languages_url":"https://api.github.com/repos/katerina-berestova/short-links/languages","stargazers_url":"https://api.github.com/repos/katerina-berestova/short-links/stargazers","contributors_url":"https://api.github.com/repos/katerina-berestova/short-links/contributors","subscribers_url":"https://api.github.com/repos/katerina-berestova/short-links/subscribers","subscription_url":"https://api.github.com/repos/katerina-berestova/short-links/subscription","commits_url":"https://api.github.com/repos/katerina-berestova/short-links/commits{/sha}","git_commits_url":"https://api.github.com/repos/katerina-berestova/short-links/git/commits{/sha}","comments_url":"https://api.github.com/repos/katerina-berestova/short-links/comments{/number}","issue_comment_url":"https://api.github.com/repos/katerina-berestova/short-links/issues/comments{/number}","contents_url":"https://api.github.com/repos/katerina-berestova/short-links/contents/{+path}","compare_url":"https://api.github.com/repos/katerina-berestova/short-links/compare/{base}...{head}","merges_url":"https://api.github.com/repos/katerina-berestova/short-links/merges","archive_url":"https://api.github.com/repos/katerina-berestova/short-links/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/katerina-berestova/short-links/downloads","issues_url":"https://api.github.com/repos/katerina-berestova/short-links/issues{/number}","pulls_url":"https://api.github.com/repos/katerina-berestova/short-links/pulls{/number}","milestones_url":"https://api.github.com/repos/katerina-berestova/short-links/milestones{/number}","notifications_url":"https://api.github.com/repos/katerina-berestova/short-links/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/katerina-berestova/short-links/labels{/name}","releases_url":"https://api.github.com/repos/katerina-berestova/short-links/releases{/id}","deployments_url":"https://api.github.com/repos/katerina-berestova/short-links/deployments","created_at":"2017-06-12T11:22:57Z","updated_at":"2017-06-12T11:22:57Z","pushed_at":"2017-06-14T14:27:18Z","git_url":"git://github.com/katerina-berestova/short-links.git","ssh_url":"git@github.com:katerina-berestova/short-links.git","clone_url":"https://github.com/katerina-berestova/short-links.git","svn_url":"https://github.com/katerina-berestova/short-links","homepage":null,"size":10,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"katerina-berestova:master","ref":"master","sha":"342483cce233974ee10834929eb446158fcb64ca","user":{"login":"katerina-berestova","id":9986202,"avatar_url":"https://avatars1.githubusercontent.com/u/9986202?v=3","gravatar_id":"","url":"https://api.github.com/users/katerina-berestova","html_url":"https://github.com/katerina-berestova","followers_url":"https://api.github.com/users/katerina-berestova/followers","following_url":"https://api.github.com/users/katerina-berestova/following{/other_user}","gists_url":"https://api.github.com/users/katerina-berestova/gists{/gist_id}","starred_url":"https://api.github.com/users/katerina-berestova/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/katerina-berestova/subscriptions","organizations_url":"https://api.github.com/users/katerina-berestova/orgs","repos_url":"https://api.github.com/users/katerina-berestova/repos","events_url":"https://api.github.com/users/katerina-berestova/events{/privacy}","received_events_url":"https://api.github.com/users/katerina-berestova/received_events","type":"User","site_admin":false},"repo":{"id":94088171,"name":"short-links","full_name":"katerina-berestova/short-links","owner":{"login":"katerina-berestova","id":9986202,"avatar_url":"https://avatars1.githubusercontent.com/u/9986202?v=3","gravatar_id":"","url":"https://api.github.com/users/katerina-berestova","html_url":"https://github.com/katerina-berestova","followers_url":"https://api.github.com/users/katerina-berestova/followers","following_url":"https://api.github.com/users/katerina-berestova/following{/other_user}","gists_url":"https://api.github.com/users/katerina-berestova/gists{/gist_id}","starred_url":"https://api.github.com/users/katerina-berestova/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/katerina-berestova/subscriptions","organizations_url":"https://api.github.com/users/katerina-berestova/orgs","repos_url":"https://api.github.com/users/katerina-berestova/repos","events_url":"https://api.github.com/users/katerina-berestova/events{/privacy}","received_events_url":"https://api.github.com/users/katerina-berestova/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/katerina-berestova/short-links","description":"Short links generator","fork":false,"url":"https://api.github.com/repos/katerina-berestova/short-links","forks_url":"https://api.github.com/repos/katerina-berestova/short-links/forks","keys_url":"https://api.github.com/repos/katerina-berestova/short-links/keys{/key_id}","collaborators_url":"https://api.github.com/repos/katerina-berestova/short-links/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/katerina-berestova/short-links/teams","hooks_url":"https://api.github.com/repos/katerina-berestova/short-links/hooks","issue_events_url":"https://api.github.com/repos/katerina-berestova/short-links/issues/events{/number}","events_url":"https://api.github.com/repos/katerina-berestova/short-links/events","assignees_url":"https://api.github.com/repos/katerina-berestova/short-links/assignees{/user}","branches_url":"https://api.github.com/repos/katerina-berestova/short-links/branches{/branch}","tags_url":"https://api.github.com/repos/katerina-berestova/short-links/tags","blobs_url":"https://api.github.com/repos/katerina-berestova/short-links/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/katerina-berestova/short-links/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/katerina-berestova/short-links/git/refs{/sha}","trees_url":"https://api.github.com/repos/katerina-berestova/short-links/git/trees{/sha}","statuses_url":"https://api.github.com/repos/katerina-berestova/short-links/statuses/{sha}","languages_url":"https://api.github.com/repos/katerina-berestova/short-links/languages","stargazers_url":"https://api.github.com/repos/katerina-berestova/short-links/stargazers","contributors_url":"https://api.github.com/repos/katerina-berestova/short-links/contributors","subscribers_url":"https://api.github.com/repos/katerina-berestova/short-links/subscribers","subscription_url":"https://api.github.com/repos/katerina-berestova/short-links/subscription","commits_url":"https://api.github.com/repos/katerina-berestova/short-links/commits{/sha}","git_commits_url":"https://api.github.com/repos/katerina-berestova/short-links/git/commits{/sha}","comments_url":"https://api.github.com/repos/katerina-berestova/short-links/comments{/number}","issue_comment_url":"https://api.github.com/repos/katerina-berestova/short-links/issues/comments{/number}","contents_url":"https://api.github.com/repos/katerina-berestova/short-links/contents/{+path}","compare_url":"https://api.github.com/repos/katerina-berestova/short-links/compare/{base}...{head}","merges_url":"https://api.github.com/repos/katerina-berestova/short-links/merges","archive_url":"https://api.github.com/repos/katerina-berestova/short-links/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/katerina-berestova/short-links/downloads","issues_url":"https://api.github.com/repos/katerina-berestova/short-links/issues{/number}","pulls_url":"https://api.github.com/repos/katerina-berestova/short-links/pulls{/number}","milestones_url":"https://api.github.com/repos/katerina-berestova/short-links/milestones{/number}","notifications_url":"https://api.github.com/repos/katerina-berestova/short-links/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/katerina-berestova/short-links/labels{/name}","releases_url":"https://api.github.com/repos/katerina-berestova/short-links/releases{/id}","deployments_url":"https://api.github.com/repos/katerina-berestova/short-links/deployments","created_at":"2017-06-12T11:22:57Z","updated_at":"2017-06-12T11:22:57Z","pushed_at":"2017-06-14T14:27:18Z","git_url":"git://github.com/katerina-berestova/short-links.git","ssh_url":"git@github.com:katerina-berestova/short-links.git","clone_url":"https://github.com/katerina-berestova/short-links.git","svn_url":"https://github.com/katerina-berestova/short-links","homepage":null,"size":10,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/katerina-berestova/short-links/pulls/1"},"html":{"href":"https://github.com/katerina-berestova/short-links/pull/1"},"issue":{"href":"https://api.github.com/repos/katerina-berestova/short-links/issues/1"},"comments":{"href":"https://api.github.com/repos/katerina-berestova/short-links/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/katerina-berestova/short-links/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/katerina-berestova/short-links/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/katerina-berestova/short-links/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/katerina-berestova/short-links/statuses/5bc2ab19595c4d29b814032877f6779826a9a645"}}}}
{"actor":{"display_login":"tshashkova"}}