From 52cfe3b946e90ae65524a9b2d056912b2f0d4573 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 14 Jul 2015 20:23:13 -0700 Subject: [PATCH] api call to view a streak, shows if user is joined #1 --- applications/api.moon | 24 ++++++++++++++++++++++++ spec/applications/api_spec.moon | 13 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/applications/api.moon b/applications/api.moon index c0d61abd..191e84c2 100644 --- a/applications/api.moon +++ b/applications/api.moon @@ -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 @@ -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 @ diff --git a/spec/applications/api_spec.moon b/spec/applications/api_spec.moon index 0c36c27d..cae8b1c9 100644 --- a/spec/applications/api_spec.moon +++ b/spec/applications/api_spec.moon @@ -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 +