Skip to content

Commit

Permalink
api render submissions for streak #1
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Jul 16, 2015
1 parent 99f9217 commit 0bc8327
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
50 changes: 50 additions & 0 deletions applications/api.moon
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 @
Expand Down
19 changes: 17 additions & 2 deletions spec/applications/api_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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!
Expand Down Expand Up @@ -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

0 comments on commit 0bc8327

Please sign in to comment.