Plan stuff. Log stuff.
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.
 
 
 
 
 

137 lines
3.2 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 Activity(g *gin.RouterGroup, db database.Database, auth *services.AuthService) {
type resObj struct {
Activity *models.Activity `json:"activity,omitempty"`
Activities []*models.Activity `json:"activities,omitempty"`
}
g.Use(auth.GinSessionMiddleware(true))
// GET / – List own activities.
g.GET("/", func(c *gin.Context) {
user := auth.UserFromContext(c)
activities, err := db.Activities().ListUser(c.Request.Context(), *user)
if err != nil {
slerrors.GinRespond(c, err)
return
}
sort.Sort(models.ActivitiesByName(activities))
c.JSON(200, resObj{Activities: activities})
})
// GET / — Find an activity
g.GET("/:id", func(c *gin.Context) {
user := auth.UserFromContext(c)
activity, err := db.Activities().FindID(c.Request.Context(), c.Param("id"))
if err != nil {
slerrors.GinRespond(c, err)
return
}
if activity.UserID != user.ID {
slerrors.GinRespond(c, slerrors.NotFound("Activity"))
return
}
c.JSON(200, resObj{Activity: activity})
})
// POST / – Create an activity
g.POST("/", func(c *gin.Context) {
user := auth.UserFromContext(c)
activity := models.Activity{}
err := c.BindJSON(&activity)
if err != nil {
slerrors.GinRespond(c, &slerrors.SLError{Code: 400, Text: err.Error()})
return
}
activity.UserID = user.ID
activity.GenerateIDs()
err = db.Activities().Insert(c.Request.Context(), activity)
if err != nil {
slerrors.GinRespond(c, err)
return
}
c.JSON(200, resObj{Activity: &activity})
})
// PATCH /:id – Update one activity
g.PATCH("/:id", func(c *gin.Context) {
user := auth.UserFromContext(c)
updates := make([]*models.ActivityUpdate, 0, 8)
err := c.BindJSON(&updates)
if err != nil {
slerrors.GinRespond(c, &slerrors.SLError{Code: 400, Text: err.Error()})
return
}
activity, err := db.Activities().FindID(c.Request.Context(), c.Param("id"))
if err != nil {
slerrors.GinRespond(c, err)
return
}
if activity.UserID != user.ID {
slerrors.GinRespond(c, slerrors.NotFound("Activity"))
return
}
activity, err = db.Activities().Update(c.Request.Context(), *activity, updates)
if err != nil {
slerrors.GinRespond(c, err)
return
}
c.JSON(200, resObj{Activity: activity})
})
// DELETE /:id – Delete an activity
g.DELETE("/:id", func(c *gin.Context) {
user := auth.UserFromContext(c)
activity, err := db.Activities().FindID(c.Request.Context(), c.Param("id"))
if err != nil {
slerrors.GinRespond(c, err)
return
}
if activity.UserID != user.ID {
slerrors.GinRespond(c, slerrors.NotFound("Activity"))
return
}
periods, err := db.Periods().ListActivity(c.Request.Context(), *activity)
if err != nil {
slerrors.GinRespond(c, slerrors.Wrap(err))
return
}
if len(periods) > 0 {
slerrors.GinRespond(c, slerrors.PreconditionFailed("You cannot delete an activity that's in use."))
return
}
err = db.Activities().Remove(c.Request.Context(), *activity)
if err != nil {
slerrors.GinRespond(c, err)
return
}
c.JSON(200, resObj{Activity: activity})
})
}