/
history.go
68 lines (57 loc) · 1.31 KB
/
history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package controllers
import (
"database/sql"
"net/http"
e "github.com/chances/party-server/errors"
"github.com/chances/party-server/models"
"github.com/chances/party-server/session"
"github.com/gin-gonic/gin"
)
// History controller
type History struct {
Controller
}
// NewHistory creates a new History controller
func NewHistory() History {
newHistory := History{}
newHistory.Setup()
return newHistory
}
// Get the current user's party's History
func (cr *History) Get() gin.HandlerFunc {
return func(c *gin.Context) {
currentParty, err := session.CurrentParty(c)
if err != nil {
if err != sql.ErrNoRows {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
c.JSON(http.StatusNotFound, models.EmptyRespose)
return
}
historyData, err := currentParty.HistoryG().One()
// TODO: Add pagination via JSON DB query
if err != nil {
if err != sql.ErrNoRows {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
c.JSON(http.StatusNotFound, models.EmptyRespose)
return
}
var history []models.Track
err = historyData.Data.Unmarshal(&history)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
c.JSON(http.StatusOK, models.NewResponse(
currentParty.RoomCode+"#history",
"history", cr.RequestURI(c),
history,
))
}
}