From 0bc832700895760f2b320c851966fc44a9ec31ae Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 15 Jul 2015 20:18:26 -0700 Subject: [PATCH] api render submissions for streak #1 --- applications/api.moon | 50 +++++++++++++++++++++++++++++++++ spec/applications/api_spec.moon | 19 +++++++++++-- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/applications/api.moon b/applications/api.moon index 1642eac2..f15855ee 100644 --- a/applications/api.moon +++ b/applications/api.moon @@ -37,6 +37,36 @@ format_user = (u) -> display_name: u.display_name } +format_submission = do + fields = { + "title", "description", "published", "comments_count", "allow_comments", + "likes_count", "created_at" + } + + (s) -> + -- error require("moon").dump s + + out = {f, s[f] for f in *fields} + out.user = format_user s.user + out.streak_submission = { + submit_time: s.streak_submission.submit_time + late_submit: s.streak_submission.late_submit + } + out.streaks = [{ + id: streak.id + title: streak.title + } for streak in *s.streaks] + + out.uploads = if s.uploads + [{ + id: upload.id + type: upload.__class.types\to_name upload.type + url: if upload.type == upload.__class.types.image + upload\image_url! + } for upload in *s.uploads] + + out + format_streak = do fields = { "id", "start_date", "end_date", "hour_offset", "title", @@ -129,6 +159,26 @@ class StreakApi extends lapis.Application streak_user: @streak_user and format_streak_user @streak_user } + "/api/1/streak/:id/submissions": api_request => + find_streak @ + assert_page @ + + import Submissions from require "models" + pager = @streak\find_submissions { + per_page: SUBMISSION_PER_PAGE + prepare_submissions: (submissions) -> + Submissions\preload_for_list submissions, { + likes_for: @current_user + } + } + + submissions = pager\get_page @page + + json: { + page: @page + submissions: [format_submission s for s in *submissions] + } + "/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 cae8b1c9..abc1990d 100644 --- a/spec/applications/api_spec.moon +++ b/spec/applications/api_spec.moon @@ -6,7 +6,7 @@ import import request from require "spec.helpers" import truncate_tables from require "lapis.spec.db" -import ApiKeys, Users, Streaks, StreakUsers from require "models" +import ApiKeys, Users, Streaks, StreakUsers, Submissions, StreakSubmissions from require "models" factory = require "spec.factory" @@ -18,7 +18,8 @@ describe "api", -> close_test_server! before_each -> - truncate_tables Users, ApiKeys, Streaks, StreakUsers + truncate_tables Users, ApiKeys, Streaks, StreakUsers, Submissions, + StreakSubmissions it "it should create api key", -> assert factory.ApiKeys! @@ -154,4 +155,18 @@ describe "api", -> assert.truthy res.streak assert.truthy res.streak_user + it "views streak submissions for empty streak", -> + streak = factory.Streaks! + status, res = request_with_key "/api/1/streak/#{streak.id}/submissions" + error res + + + it "views streak submissions", -> + streak = factory.Streaks! + factory.StreakSubmissions streak_id: streak.id + factory.StreakSubmissions streak_id: streak.id + + status, res = request_with_key "/api/1/streak/#{streak.id}/submissions" + assert.truthy res.submissions + assert.same 2, #res.submissions