{"action":"created","comment":{"url":"https://api.github.com/repos/nnev/kasse/pulls/comments/27963703","id":27963703,"diff_hunk":"@@ -139,6 +143,52 @@ func HandleCard(uid []byte) (Result, error) {\n \treturn PaymentMade, nil\n }\n \n+// RegisterUser creates a new row in the user table, with the given username\n+// and password. It returns a populated User and no error on success. If the\n+// username is already taken, it returns ErrUserExists.\n+func RegisterUser(name string, password []byte) (*User, error) {\n+\tlog.Printf(\"Registering user %s\", name)\n+\n+\ttx, err := db.Beginx()\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\tdefer tx.Rollback()\n+\n+\tpwhash, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\t// We need to check first if the username is already taken, because the\n+\t// error from an insert can't be checked programmatically.\n+\tvar user User\n+\tif err := tx.Get(&user, `SELECT user_id, name, password FROM users WHERE name = $1`, name); err == nil {\n+\t\treturn nil, ErrUserExists\n+\t}\n+\n+\tresult, err := tx.Exec(`INSERT INTO users (name, password) VALUES ($1, $2)`, name, pwhash)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\tid, err := result.LastInsertId()\n+\tif err != nil {\n+\t\tif err := tx.Get(&id, `SELECT user_id FROM users WHERE name = $1`, name); err != nil {\n+\t\t\treturn nil, err\n+\t\t}\n+\t}","path":"main.go","position":56,"original_position":56,"commit_id":"8912b8000dc9307acdc2495423d3e249bdf47636","original_commit_id":"8912b8000dc9307acdc2495423d3e249bdf47636","user":{"login":"Merovius","id":720787,"avatar_url":"https://avatars.githubusercontent.com/u/720787?v=3","gravatar_id":"","url":"https://api.github.com/users/Merovius","html_url":"https://github.com/Merovius","followers_url":"https://api.github.com/users/Merovius/followers","following_url":"https://api.github.com/users/Merovius/following{/other_user}","gists_url":"https://api.github.com/users/Merovius/gists{/gist_id}","starred_url":"https://api.github.com/users/Merovius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Merovius/subscriptions","organizations_url":"https://api.github.com/users/Merovius/orgs","repos_url":"https://api.github.com/users/Merovius/repos","events_url":"https://api.github.com/users/Merovius/events{/privacy}","received_events_url":"https://api.github.com/users/Merovius/received_events","type":"User","site_admin":false},"body":"If LastInsertId returns an error, I fall back to getting the inserted ID manually with a query. So after line 180 id should be correct either way (or an error should be returned).","created_at":"2015-04-08T12:14:38Z","updated_at":"2015-04-08T12:14:38Z","html_url":"https://github.com/nnev/kasse/pull/9#discussion_r27963703","pull_request_url":"https://api.github.com/repos/nnev/kasse/pulls/9","_links":{"self":{"href":"https://api.github.com/repos/nnev/kasse/pulls/comments/27963703"},"html":{"href":"https://github.com/nnev/kasse/pull/9#discussion_r27963703"},"pull_request":{"href":"https://api.github.com/repos/nnev/kasse/pulls/9"}}},"pull_request":{"url":"https://api.github.com/repos/nnev/kasse/pulls/9","id":32850876,"html_url":"https://github.com/nnev/kasse/pull/9","diff_url":"https://github.com/nnev/kasse/pull/9.diff","patch_url":"https://github.com/nnev/kasse/pull/9.patch","issue_url":"https://api.github.com/repos/nnev/kasse/issues/9","number":9,"state":"open","locked":false,"title":"Add function to register new users","user":{"login":"Merovius","id":720787,"avatar_url":"https://avatars.githubusercontent.com/u/720787?v=3","gravatar_id":"","url":"https://api.github.com/users/Merovius","html_url":"https://github.com/Merovius","followers_url":"https://api.github.com/users/Merovius/followers","following_url":"https://api.github.com/users/Merovius/following{/other_user}","gists_url":"https://api.github.com/users/Merovius/gists{/gist_id}","starred_url":"https://api.github.com/users/Merovius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Merovius/subscriptions","organizations_url":"https://api.github.com/users/Merovius/orgs","repos_url":"https://api.github.com/users/Merovius/repos","events_url":"https://api.github.com/users/Merovius/events{/privacy}","received_events_url":"https://api.github.com/users/Merovius/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-04-08T11:28:56Z","updated_at":"2015-04-08T12:14:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5ee8183e273a7be5c32549a574d0c6433c1caace","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/nnev/kasse/pulls/9/commits","review_comments_url":"https://api.github.com/repos/nnev/kasse/pulls/9/comments","review_comment_url":"https://api.github.com/repos/nnev/kasse/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nnev/kasse/issues/9/comments","statuses_url":"https://api.github.com/repos/nnev/kasse/statuses/8912b8000dc9307acdc2495423d3e249bdf47636","head":{"label":"nnev:registration","ref":"registration","sha":"8912b8000dc9307acdc2495423d3e249bdf47636","user":{"login":"nnev","id":6986936,"avatar_url":"https://avatars.githubusercontent.com/u/6986936?v=3","gravatar_id":"","url":"https://api.github.com/users/nnev","html_url":"https://github.com/nnev","followers_url":"https://api.github.com/users/nnev/followers","following_url":"https://api.github.com/users/nnev/following{/other_user}","gists_url":"https://api.github.com/users/nnev/gists{/gist_id}","starred_url":"https://api.github.com/users/nnev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nnev/subscriptions","organizations_url":"https://api.github.com/users/nnev/orgs","repos_url":"https://api.github.com/users/nnev/repos","events_url":"https://api.github.com/users/nnev/events{/privacy}","received_events_url":"https://api.github.com/users/nnev/received_events","type":"Organization","site_admin":false},"repo":{"id":33357818,"name":"kasse","full_name":"nnev/kasse","owner":{"login":"nnev","id":6986936,"avatar_url":"https://avatars.githubusercontent.com/u/6986936?v=3","gravatar_id":"","url":"https://api.github.com/users/nnev","html_url":"https://github.com/nnev","followers_url":"https://api.github.com/users/nnev/followers","following_url":"https://api.github.com/users/nnev/following{/other_user}","gists_url":"https://api.github.com/users/nnev/gists{/gist_id}","starred_url":"https://api.github.com/users/nnev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nnev/subscriptions","organizations_url":"https://api.github.com/users/nnev/orgs","repos_url":"https://api.github.com/users/nnev/repos","events_url":"https://api.github.com/users/nnev/events{/privacy}","received_events_url":"https://api.github.com/users/nnev/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nnev/kasse","description":"NFC Getränkekasse","fork":false,"url":"https://api.github.com/repos/nnev/kasse","forks_url":"https://api.github.com/repos/nnev/kasse/forks","keys_url":"https://api.github.com/repos/nnev/kasse/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nnev/kasse/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nnev/kasse/teams","hooks_url":"https://api.github.com/repos/nnev/kasse/hooks","issue_events_url":"https://api.github.com/repos/nnev/kasse/issues/events{/number}","events_url":"https://api.github.com/repos/nnev/kasse/events","assignees_url":"https://api.github.com/repos/nnev/kasse/assignees{/user}","branches_url":"https://api.github.com/repos/nnev/kasse/branches{/branch}","tags_url":"https://api.github.com/repos/nnev/kasse/tags","blobs_url":"https://api.github.com/repos/nnev/kasse/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nnev/kasse/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nnev/kasse/git/refs{/sha}","trees_url":"https://api.github.com/repos/nnev/kasse/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nnev/kasse/statuses/{sha}","languages_url":"https://api.github.com/repos/nnev/kasse/languages","stargazers_url":"https://api.github.com/repos/nnev/kasse/stargazers","contributors_url":"https://api.github.com/repos/nnev/kasse/contributors","subscribers_url":"https://api.github.com/repos/nnev/kasse/subscribers","subscription_url":"https://api.github.com/repos/nnev/kasse/subscription","commits_url":"https://api.github.com/repos/nnev/kasse/commits{/sha}","git_commits_url":"https://api.github.com/repos/nnev/kasse/git/commits{/sha}","comments_url":"https://api.github.com/repos/nnev/kasse/comments{/number}","issue_comment_url":"https://api.github.com/repos/nnev/kasse/issues/comments{/number}","contents_url":"https://api.github.com/repos/nnev/kasse/contents/{+path}","compare_url":"https://api.github.com/repos/nnev/kasse/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nnev/kasse/merges","archive_url":"https://api.github.com/repos/nnev/kasse/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nnev/kasse/downloads","issues_url":"https://api.github.com/repos/nnev/kasse/issues{/number}","pulls_url":"https://api.github.com/repos/nnev/kasse/pulls{/number}","milestones_url":"https://api.github.com/repos/nnev/kasse/milestones{/number}","notifications_url":"https://api.github.com/repos/nnev/kasse/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nnev/kasse/labels{/name}","releases_url":"https://api.github.com/repos/nnev/kasse/releases{/id}","created_at":"2015-04-03T10:01:58Z","updated_at":"2015-04-07T22:31:25Z","pushed_at":"2015-04-08T11:28:57Z","git_url":"git://github.com/nnev/kasse.git","ssh_url":"git@github.com:nnev/kasse.git","clone_url":"https://github.com/nnev/kasse.git","svn_url":"https://github.com/nnev/kasse","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":4,"forks":0,"open_issues":4,"watchers":0,"default_branch":"master"}},"base":{"label":"nnev:master","ref":"master","sha":"542bc329d0245e6ef88aae2ec8d1549631b9b503","user":{"login":"nnev","id":6986936,"avatar_url":"https://avatars.githubusercontent.com/u/6986936?v=3","gravatar_id":"","url":"https://api.github.com/users/nnev","html_url":"https://github.com/nnev","followers_url":"https://api.github.com/users/nnev/followers","following_url":"https://api.github.com/users/nnev/following{/other_user}","gists_url":"https://api.github.com/users/nnev/gists{/gist_id}","starred_url":"https://api.github.com/users/nnev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nnev/subscriptions","organizations_url":"https://api.github.com/users/nnev/orgs","repos_url":"https://api.github.com/users/nnev/repos","events_url":"https://api.github.com/users/nnev/events{/privacy}","received_events_url":"https://api.github.com/users/nnev/received_events","type":"Organization","site_admin":false},"repo":{"id":33357818,"name":"kasse","full_name":"nnev/kasse","owner":{"login":"nnev","id":6986936,"avatar_url":"https://avatars.githubusercontent.com/u/6986936?v=3","gravatar_id":"","url":"https://api.github.com/users/nnev","html_url":"https://github.com/nnev","followers_url":"https://api.github.com/users/nnev/followers","following_url":"https://api.github.com/users/nnev/following{/other_user}","gists_url":"https://api.github.com/users/nnev/gists{/gist_id}","starred_url":"https://api.github.com/users/nnev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nnev/subscriptions","organizations_url":"https://api.github.com/users/nnev/orgs","repos_url":"https://api.github.com/users/nnev/repos","events_url":"https://api.github.com/users/nnev/events{/privacy}","received_events_url":"https://api.github.com/users/nnev/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nnev/kasse","description":"NFC Getränkekasse","fork":false,"url":"https://api.github.com/repos/nnev/kasse","forks_url":"https://api.github.com/repos/nnev/kasse/forks","keys_url":"https://api.github.com/repos/nnev/kasse/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nnev/kasse/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nnev/kasse/teams","hooks_url":"https://api.github.com/repos/nnev/kasse/hooks","issue_events_url":"https://api.github.com/repos/nnev/kasse/issues/events{/number}","events_url":"https://api.github.com/repos/nnev/kasse/events","assignees_url":"https://api.github.com/repos/nnev/kasse/assignees{/user}","branches_url":"https://api.github.com/repos/nnev/kasse/branches{/branch}","tags_url":"https://api.github.com/repos/nnev/kasse/tags","blobs_url":"https://api.github.com/repos/nnev/kasse/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nnev/kasse/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nnev/kasse/git/refs{/sha}","trees_url":"https://api.github.com/repos/nnev/kasse/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nnev/kasse/statuses/{sha}","languages_url":"https://api.github.com/repos/nnev/kasse/languages","stargazers_url":"https://api.github.com/repos/nnev/kasse/stargazers","contributors_url":"https://api.github.com/repos/nnev/kasse/contributors","subscribers_url":"https://api.github.com/repos/nnev/kasse/subscribers","subscription_url":"https://api.github.com/repos/nnev/kasse/subscription","commits_url":"https://api.github.com/repos/nnev/kasse/commits{/sha}","git_commits_url":"https://api.github.com/repos/nnev/kasse/git/commits{/sha}","comments_url":"https://api.github.com/repos/nnev/kasse/comments{/number}","issue_comment_url":"https://api.github.com/repos/nnev/kasse/issues/comments{/number}","contents_url":"https://api.github.com/repos/nnev/kasse/contents/{+path}","compare_url":"https://api.github.com/repos/nnev/kasse/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nnev/kasse/merges","archive_url":"https://api.github.com/repos/nnev/kasse/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nnev/kasse/downloads","issues_url":"https://api.github.com/repos/nnev/kasse/issues{/number}","pulls_url":"https://api.github.com/repos/nnev/kasse/pulls{/number}","milestones_url":"https://api.github.com/repos/nnev/kasse/milestones{/number}","notifications_url":"https://api.github.com/repos/nnev/kasse/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nnev/kasse/labels{/name}","releases_url":"https://api.github.com/repos/nnev/kasse/releases{/id}","created_at":"2015-04-03T10:01:58Z","updated_at":"2015-04-07T22:31:25Z","pushed_at":"2015-04-08T11:28:57Z","git_url":"git://github.com/nnev/kasse.git","ssh_url":"git@github.com:nnev/kasse.git","clone_url":"https://github.com/nnev/kasse.git","svn_url":"https://github.com/nnev/kasse","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":4,"forks":0,"open_issues":4,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nnev/kasse/pulls/9"},"html":{"href":"https://github.com/nnev/kasse/pull/9"},"issue":{"href":"https://api.github.com/repos/nnev/kasse/issues/9"},"comments":{"href":"https://api.github.com/repos/nnev/kasse/issues/9/comments"},"review_comments":{"href":"https://api.github.com/repos/nnev/kasse/pulls/9/comments"},"review_comment":{"href":"https://api.github.com/repos/nnev/kasse/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nnev/kasse/pulls/9/commits"},"statuses":{"href":"https://api.github.com/repos/nnev/kasse/statuses/8912b8000dc9307acdc2495423d3e249bdf47636"}}}}