Skip to content

Commit

Permalink
api call to view a streak, shows if user is joined #1
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Jul 15, 2015
1 parent 8b7690e commit 52cfe3b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
24 changes: 24 additions & 0 deletions applications/api.moon
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ api_request = (fn) ->
fn @


format_streak_user = (u) ->
{
pending: u.pending
submissions_count: u.submissions_count
created_at: u.created_at
current_streak: u\get_current_streak!
longest_streak: u\get_longest_streak!
}

format_user = (u) ->
{
id: u.id
Expand Down Expand Up @@ -112,6 +121,21 @@ class StreakApi extends lapis.Application
streaks: [format_streak streak for streak in *@streaks]
}

"/api/1/streak/:id": api_request =>
import Streaks from require "models"
assert_valid @params, {
{"id", is_integer: true}
}

streak = Streaks\find @params.id
assert_error streak\allowed_to_view @current_user
streak_user = streak\find_streak_user @current_user

json: {
streak: format_streak streak
streak_user: format_streak_user streak_user
}

"/api/1/streak/:id/join": api_request respond_to {
POST: =>
find_streak @
Expand Down
13 changes: 13 additions & 0 deletions spec/applications/api_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,17 @@ describe "api", ->
status, res = request_with_key "/api/1/streak/#{streak.id}/leave", post: {}
assert.same true, res.left

it "views streak", ->
streak = factory.Streaks!
status, res = request_with_key "/api/1/streak/#{streak.id}"
assert.truthy res.streak

it "views streak that user is in", ->
streak = factory.Streaks!
factory.StreakUsers streak_id: streak.id, user_id: current_user.id

status, res = request_with_key "/api/1/streak/#{streak.id}"
assert.truthy res.streak
assert.truthy res.streak_user


0 comments on commit 52cfe3b

Please sign in to comment.