You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
3.9 KiB
169 lines
3.9 KiB
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gisle/stufflog/database"
|
|
"github.com/gisle/stufflog/models"
|
|
"github.com/gisle/stufflog/services"
|
|
"github.com/gisle/stufflog/slerrors"
|
|
"sort"
|
|
)
|
|
|
|
func Period(g *gin.RouterGroup, db database.Database, scoring *services.ScoringService, auth *services.AuthService) {
|
|
type resObj struct {
|
|
Period *models.Period `json:"period,omitempty"`
|
|
Periods []*models.Period `json:"periods,omitempty"`
|
|
Activities []*models.Activity `json:"activities,omitempty"`
|
|
}
|
|
|
|
g.Use(auth.GinSessionMiddleware(true))
|
|
|
|
// GET / – List own periods.
|
|
g.GET("/", func(c *gin.Context) {
|
|
user := auth.UserFromContext(c)
|
|
|
|
periods, err := db.Periods().ListUser(c.Request.Context(), *user)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
|
|
sort.Sort(models.PeriodsByFrom(periods))
|
|
|
|
c.JSON(200, resObj{Periods: periods})
|
|
})
|
|
|
|
// GET / — Find a period
|
|
g.GET("/:id", func(c *gin.Context) {
|
|
user := auth.UserFromContext(c)
|
|
|
|
period, err := db.Periods().FindID(c.Request.Context(), c.Param("id"))
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
if period.UserID != user.ID {
|
|
slerrors.GinRespond(c, slerrors.NotFound("Period"))
|
|
return
|
|
}
|
|
|
|
activities := make([]*models.Activity, 0, 8)
|
|
activityAdded := make(map[string]bool)
|
|
for _, goal := range period.Goals {
|
|
if !activityAdded[goal.ActivityID] {
|
|
activityAdded[goal.ActivityID] = true
|
|
|
|
activity, err := db.Activities().FindID(c.Request.Context(), goal.ActivityID)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
|
|
activities = append(activities, activity)
|
|
}
|
|
}
|
|
|
|
c.JSON(200, resObj{Period: period, Activities: activities})
|
|
})
|
|
|
|
// POST / – Create a period
|
|
g.POST("/", func(c *gin.Context) {
|
|
user := auth.UserFromContext(c)
|
|
period := models.Period{}
|
|
|
|
err := c.BindJSON(&period)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, &slerrors.SLError{Code: 400, Text: err.Error()})
|
|
return
|
|
}
|
|
|
|
period.GenerateIDs()
|
|
period.UserID = user.ID
|
|
period.Logs = make([]models.Log, 0)
|
|
period.ShouldReScore = false
|
|
|
|
if period.Goals == nil {
|
|
period.Goals = make([]models.Goal, 0)
|
|
}
|
|
|
|
err = db.Periods().Insert(c.Request.Context(), period)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(200, resObj{Period: &period})
|
|
})
|
|
|
|
// PATCH /:id – Update one Period
|
|
g.PATCH("/:id", func(c *gin.Context) {
|
|
user := auth.UserFromContext(c)
|
|
updates := make([]*models.PeriodUpdate, 0, 8)
|
|
|
|
err := c.BindJSON(&updates)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, &slerrors.SLError{Code: 400, Text: err.Error()})
|
|
return
|
|
}
|
|
|
|
period, err := db.Periods().FindID(c.Request.Context(), c.Param("id"))
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
if period.UserID != user.ID {
|
|
slerrors.GinRespond(c, slerrors.NotFound("Period"))
|
|
return
|
|
}
|
|
|
|
for _, update := range updates {
|
|
if update.AddLog != nil {
|
|
score, err := scoring.ScoreOne(c.Request.Context(), *period, *update.AddLog)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
update.AddLog.Score = score
|
|
}
|
|
}
|
|
|
|
period, err = db.Periods().Update(c.Request.Context(), *period, updates)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
|
|
if period.ShouldReScore {
|
|
period, err = scoring.ScoreAll(c.Request.Context(), *period)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
c.JSON(200, resObj{Period: period})
|
|
})
|
|
|
|
// DELETE /:id – Delete a period
|
|
g.DELETE("/:id", func(c *gin.Context) {
|
|
user := auth.UserFromContext(c)
|
|
|
|
period, err := db.Periods().FindID(c.Request.Context(), c.Param("id"))
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
if period.UserID != user.ID {
|
|
slerrors.GinRespond(c, slerrors.NotFound("Period"))
|
|
return
|
|
}
|
|
|
|
err = db.Periods().Remove(c.Request.Context(), *period)
|
|
if err != nil {
|
|
slerrors.GinRespond(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(200, resObj{Period: period})
|
|
})
|
|
}
|