|
|
package api
import ( "github.com/gin-gonic/gin" "github.com/gissleh/stufflog/database" "github.com/gissleh/stufflog/internal/auth" "github.com/gissleh/stufflog/internal/generate" "github.com/gissleh/stufflog/internal/slerrors" "github.com/gissleh/stufflog/models" "github.com/gissleh/stufflog/services" "time" )
func Log(g *gin.RouterGroup, db database.Database) { l := services.Loader{DB: db}
g.GET("/", handler("logs", func(c *gin.Context) (interface{}, error) { filter := models.LogFilter{} if value := c.Query("minTime"); value != "" { minTime, err := time.Parse(time.RFC3339Nano, value) if err != nil { return nil, slerrors.BadRequest("Invalid minTime") } minTime = minTime.UTC()
filter.MinTime = &minTime } else { lastMonth := time.Now().Add(-30 * 24 * time.Hour).UTC() filter.MinTime = &lastMonth } if value := c.Query("maxTime"); value != "" { maxTime, err := time.Parse(time.RFC3339Nano, value) if err != nil { return nil, slerrors.BadRequest("Invalid maxTime") } maxTime = maxTime.UTC()
filter.MaxTime = &maxTime }
return l.ListLogs(c, filter) }))
g.GET("/:id", handler("log", func(c *gin.Context) (interface{}, error) { return l.FindLog(c, c.Param("id")) }))
g.POST("/", handler("log", func(c *gin.Context) (interface{}, error) { log := models.Log{} err := c.BindJSON(&log) if err != nil { return nil, slerrors.BadRequest("Invalid JSON") }
task, err := l.FindTask(c.Request.Context(), log.TaskID) if err != nil { return nil, err }
log.ID = generate.LogID() log.UserID = auth.UserID(c) log.TaskID = task.ID log.ItemID = task.ItemID if log.LoggedTime.IsZero() { log.LoggedTime = time.Now().UTC() } else { log.LoggedTime = log.LoggedTime.UTC() }
err = db.Logs().Insert(c.Request.Context(), log) if err != nil { return nil, err }
return &models.LogResult{ Log: log, Task: &task.Task, }, nil }))
g.PUT("/:id", handler("log", func(c *gin.Context) (interface{}, error) { update := models.LogUpdate{} err := c.BindJSON(&update) if err != nil { return nil, slerrors.BadRequest("Invalid JSON") }
log, err := l.FindLog(c.Request.Context(), c.Param("id")) if err != nil { return nil, err }
log.Update(update) err = db.Logs().Update(c.Request.Context(), log.Log) if err != nil { return nil, err }
return log, nil }))
g.DELETE("/:id", handler("log", func(c *gin.Context) (interface{}, error) { log, err := l.FindLog(c.Request.Context(), c.Param("id")) if err != nil { return nil, err }
err = db.Logs().Delete(c.Request.Context(), log.Log) if err != nil { return nil, err }
return log, nil })) }
|