Skip to content

Commit

Permalink
register api #1
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Mar 9, 2015
1 parent fd0b734 commit 81d6d7e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
35 changes: 25 additions & 10 deletions applications/api.moon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ lapis = require "lapis"

import assert_valid from require "lapis.validate"
import capture_errors_json, assert_error from require "lapis.application"

import trim_filter from require "lapis.util"
import ApiKeys, Users from require "models"

api_request = (fn) ->
Expand Down Expand Up @@ -32,26 +32,41 @@ format_streak = do

class StreakApi extends lapis.Application
"/api/1/login": capture_errors_json =>
trim_filter @params
assert_valid @params, {
{ "source", one_of: {"ios"} }
{ "source", one_of: ApiKeys.sources }
{ "username", exists: true }
{ "password", exists: true }
}

user = assert_error Users\login @params.username, @params.password
key = ApiKeys\find_or_create user.id, @params.source

json: { :key }

key = unpack ApiKeys\select [[
where user_id = ? and source = ?
]], user.id, ApiKeys.sources\for_db @params.source

unless key
key = ApiKeys\create {
user_id: user.id
source: @params.source
}
"/api/1/register": capture_errors_json =>
trim_filter @params
assert_valid @params, {
{ "source", one_of: ApiKeys.sources }
{ "username", exists: true, min_length: 2, max_length: 25 }
{ "password", exists: true, min_length: 2 }
{ "password_repeat", equals: @params.password }
{ "email", exists: true, min_length: 3 }
}

assert_error @params.email\match(".@."), "invalid email address"
user = assert_error Users\create {
username: @params.username
email: @params.email
password: @params.password
}

key = ApiKeys\find_or_create user.id, @params.source

json: { :key }


-- Streaks user is in
"/api/1/my-streaks": api_request =>
import Users, Streaks from require "models"
Expand Down
14 changes: 14 additions & 0 deletions models/api_keys.moon
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,19 @@ class ApiKeys extends Model
opts.source = @sources\for_db opts.source
Model.create @, opts


@find_or_create: (user_id, source) =>
source = @sources\for_db source

key = unpack @select [[
where user_id = ? and source = ?
]], user_id, source

unless key
key = @create user_id: user_id, :source

key


url_key: => @key

23 changes: 18 additions & 5 deletions spec/applications/api_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ describe "api", ->

assert.same key, res.key


it "should register user", ->
status, res = request "/api/1/register", {
post: {
source: "ios"
username: "leafo"
password: "leafo"
password_repeat: "leafo"
email: "[email protected]"
}
expect: "json"
}

assert.truthy res.key
assert.same 1, #Users\select!


describe "with key", ->
local api_key, current_user

Expand All @@ -68,11 +85,7 @@ describe "api", ->
assert.same 200, status
assert.same {
hosted: {}
joined: {
upcoming: {}
active: {}
completed: {}
}
joined: {}
}, res


Expand Down

0 comments on commit 81d6d7e

Please sign in to comment.