Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf99e451

Browse files
committed
Implement /newepisodes
1 parentcdfa662 commitf99e451

File tree

7 files changed

+137
-13
lines changed

7 files changed

+137
-13
lines changed

‎api/api_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ func initTestData() {
5656
CastID:1,
5757
GUID:"guid",
5858
})
59+
store.SaveEpisode(&Episode{
60+
CastID:69,
61+
GUID:"since1",
62+
CrawlTS:32503680000,
63+
})
64+
store.SaveEpisode(&Episode{
65+
CastID:69,
66+
GUID:"since2",
67+
CrawlTS:32503680001,
68+
})
5969
store.AddSubscription(1,1)
6070
}
6171

‎api/casts.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func addCast(c *echo.Context) error {
4444
funcrenameCast(c*echo.Context)error {
4545
id,err:=strconv.ParseUint(c.Param("id"),10,64)
4646
iferr!=nil {
47-
returnerr
47+
returnc.NoContent(400)
4848
}
4949

5050
cast:=store.GetCast(id)
@@ -65,7 +65,7 @@ func renameCast(c *echo.Context) error {
6565
funcremoveCast(c*echo.Context)error {
6666
id,err:=strconv.ParseUint(c.Param("id"),10,64)
6767
iferr!=nil {
68-
returnerr
68+
returnc.NoContent(400)
6969
}
7070

7171
user:=c.Get("user").(*User)

‎api/casts_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ func TestRenameCast(t *testing.T) {
8484
assert.Equal(t,200,res.Code)
8585
assert.Equal(t,"new",store.GetCast(1).Name)
8686

87-
// It should return500 if the ID is invalid
87+
// It should return400 if the ID is invalid
8888
req.URL.Path="/library/casts/nope"
89-
assert.Equal(t,500,req.send().Code)
89+
assert.Equal(t,400,req.send().Code)
9090

9191
// It returns 200 if the cast is not found
9292
req.URL.Path="/library/casts/1337"
@@ -102,7 +102,7 @@ func TestRemoveCast(t *testing.T) {
102102
user:=store.GetUser("test")
103103
assert.NotContains(t,user.Subscriptions,uint64(1))
104104

105-
// It should return500 if the ID is invalid
105+
// It should return400 if the ID is invalid
106106
req.URL.Path="/library/casts/nope"
107-
assert.Equal(t,500,req.send().Code)
107+
assert.Equal(t,400,req.send().Code)
108108
}

‎api/episodes.go‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,67 @@ package api
22

33
import (
44
"strconv"
5+
"time"
56

67
"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo"
78
)
89

10+
typenewEpisodesstruct {
11+
Timestampint64`json:"timestamp"`
12+
Episodes []Episode`json:"episodes"`
13+
}
14+
15+
//
16+
// GET /library/newepisodes
17+
//
918
funcgetNewEpisodes(c*echo.Context)error {
10-
returnnil
19+
now:=time.Now().Unix()
20+
since:=c.Request().URL.Query().Get("since")
21+
ifsince=="" {
22+
returnc.JSON(200,newEpisodes{
23+
Timestamp:now,
24+
Episodes:store.GetEpisodesSince(0),
25+
})
26+
}
27+
28+
ts,err:=strconv.ParseInt(since,10,64)
29+
iferr!=nil {
30+
returnc.NoContent(400)
31+
}
32+
33+
returnc.JSON(200,newEpisodes{
34+
Timestamp:now,
35+
Episodes:store.GetEpisodesSince(ts),
36+
})
1137
}
1238

39+
//
40+
// GET /library/episodes/:castid
41+
//
1342
funcgetEpisodes(c*echo.Context)error {
1443
castid,err:=strconv.ParseUint(c.Param("castid"),10,64)
1544
iferr!=nil {
16-
returnerr
45+
returnc.NoContent(400)
1746
}
1847

1948
returnc.JSON(200,store.GetEpisodesByCast(castid))
2049
}
2150

51+
//
52+
// GET /library/episode/:id
53+
//
2254
funcgetEpisode(c*echo.Context)error {
2355
id,err:=strconv.ParseUint(c.Param("id"),10,64)
2456
iferr!=nil {
25-
returnerr
57+
returnc.NoContent(400)
2658
}
2759

2860
returnc.JSON(200,store.GetEpisode(id))
2961
}
3062

63+
//
64+
// GET /library/episodes/label/:label
65+
//
3166
funcgetEpisodesByLabel(c*echo.Context)error {
3267
returnnil
3368
}

‎api/episodes_test.go‎

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,68 @@
11
package api
22

33
import (
4+
"encoding/json"
5+
"net/url"
46
"testing"
7+
"time"
58

69
"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/stretchr/testify/assert"
710
)
811

12+
funcTestGetNewEpisodes(t*testing.T) {
13+
r:=createRouter()
14+
15+
req:=testRequest(r,"GET","/library/newepisodes?since=32503679999",nil)
16+
req.Header.Set("Authorization","token")
17+
res:=checkNewEpisodes(t,req)
18+
assert.Len(t,res.Episodes,2)
19+
assert.Equal(t,Episode{
20+
ID:2,
21+
CastID:69,
22+
GUID:"since1",
23+
CrawlTS:32503680000,
24+
},res.Episodes[0])
25+
assert.Equal(t,Episode{
26+
ID:3,
27+
CastID:69,
28+
GUID:"since2",
29+
CrawlTS:32503680001,
30+
},res.Episodes[1])
31+
32+
req.URL,_=url.Parse("/library/newepisodes?since=32503680000")
33+
res=checkNewEpisodes(t,req)
34+
assert.Len(t,res.Episodes,1)
35+
assert.Equal(t,Episode{
36+
ID:3,
37+
CastID:69,
38+
GUID:"since2",
39+
CrawlTS:32503680001,
40+
},res.Episodes[0])
41+
42+
req.URL,_=url.Parse("/library/newepisodes?since=32503680001")
43+
res=checkNewEpisodes(t,req)
44+
assert.Len(t,res.Episodes,0)
45+
46+
req.URL,_=url.Parse("/library/newepisodes")
47+
res=checkNewEpisodes(t,req)
48+
assert.True(t,len(res.Episodes)>2)
49+
50+
req.URL,_=url.Parse("/library/newepisodes?since=what")
51+
resp:=req.send()
52+
assert.Equal(t,400,resp.Code)
53+
}
54+
55+
funccheckNewEpisodes(t*testing.T,reqtestReq)newEpisodes {
56+
now:=time.Now().Unix()
57+
res:=req.send()
58+
assert.Equal(t,200,res.Code)
59+
data:=newEpisodes{}
60+
err:=json.Unmarshal(res.Body.Bytes(),&data)
61+
assert.Nil(t,err)
62+
assert.True(t,data.Timestamp>=now)
63+
returndata
64+
}
65+
966
funcTestGetEpisodes(t*testing.T) {
1067
r:=createRouter()
1168

@@ -30,9 +87,9 @@ func TestGetEpisodes(t *testing.T) {
3087
assert.Equal(t,200,res.Code)
3188
assert.Equal(t,testJSON([]Episode{}),res.Body.String())
3289

33-
// It should return500 if the ID is invalid
90+
// It should return400 if the ID is invalid
3491
req.URL.Path="/library/episodes/datcast"
35-
assert.Equal(t,500,req.send().Code)
92+
assert.Equal(t,400,req.send().Code)
3693
}
3794

3895
funcTestGetEpisode(t*testing.T) {
@@ -50,7 +107,7 @@ func TestGetEpisode(t *testing.T) {
50107
assert.Equal(t,200,res.Code)
51108
assert.Equal(t,expectedJSON,res.Body.String())
52109

53-
// It should return500 if the ID is invalid
110+
// It should return400 if the ID is invalid
54111
req.URL.Path="/library/episode/datepisode"
55-
assert.Equal(t,500,req.send().Code)
112+
assert.Equal(t,400,req.send().Code)
56113
}

‎api/store.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type APIStore interface {
2323

2424
GetEpisode(iduint64)*Episode
2525
GetEpisodesByCast(castiduint64) []Episode
26+
GetEpisodesSince(tsint64) []Episode
2627
SaveEpisode(episode*Episode)error
2728
SaveEpisodes(episodes []Episode)error
2829
}

‎api/store_bolt_episodes.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ func (s *BoltStore) GetEpisodesByCast(castid uint64) []Episode {
4646
returnepisodes
4747
}
4848

49+
func (s*BoltStore)GetEpisodesSince(tsint64) []Episode {
50+
episodes:= []Episode{}
51+
52+
s.db.View(func(tx*bolt.Tx)error {
53+
b:=tx.Bucket(boltBucketEpisodes)
54+
c:=tx.Bucket(boltBucketEpisodeCrawlTSIndex).Cursor()
55+
prefix:= []byte(strconv.FormatInt(ts+1,10))
56+
57+
for_,id:=c.Seek(prefix);id!=nil;_,id=c.Next() {
58+
v:=b.Get(id)
59+
episode:=Episode{}
60+
json.Unmarshal(v,&episode)
61+
episodes=append(episodes,episode)
62+
}
63+
64+
returnnil
65+
})
66+
67+
returnepisodes
68+
}
69+
4970
func (s*BoltStore)SaveEpisode(episode*Episode)error {
5071
returns.db.Update(func(tx*bolt.Tx)error {
5172
returns.saveEpisode(tx,episode)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp