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.
 
 
 
 
 
 

111 lines
2.6 KiB

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 Task(g *gin.RouterGroup, db database.Database) {
l := services.Loader{DB: db}
defaultActive := true
g.GET("/", handler("tasks", func(c *gin.Context) (interface{}, error) {
filter := models.TaskFilter{}
if setting := c.Query("active"); setting != "" {
active := setting == "true"
filter.Active = &active
} else {
filter.Active = &defaultActive
}
return l.ListTasks(c, filter)
}))
g.GET("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
return l.FindTask(c, c.Param("id"))
}))
g.POST("/", handler("task", func(c *gin.Context) (interface{}, error) {
task := models.Task{}
err := c.BindJSON(&task)
if err != nil {
return nil, slerrors.BadRequest("Invalid JSON")
}
if task.EndTime != nil && task.EndTime.Before(time.Now()) {
return nil, slerrors.BadRequest("Project end time must be later than current time.")
}
project, err := l.FindProject(c.Request.Context(), task.ProjectID)
if err != nil {
return nil, err
}
item, err := l.FindItem(c.Request.Context(), task.ItemID)
if err != nil {
return nil, err
}
task.ID = generate.TaskID()
task.UserID = auth.UserID(c)
task.CreatedTime = time.Now().UTC()
task.ItemID = item.ID
task.ProjectID = project.ID
err = db.Tasks().Insert(c.Request.Context(), task)
if err != nil {
return nil, err
}
return &models.TaskResult{
Task: task,
Logs: []*models.Log{},
Item: &item.Item,
CompletedAmount: 0,
}, nil
}))
g.PUT("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
update := models.TaskUpdate{}
err := c.BindJSON(&update)
if err != nil {
return nil, slerrors.BadRequest("Invalid JSON")
}
task, err := l.FindTask(c.Request.Context(), c.Param("id"))
if err != nil {
return nil, err
}
task.Update(update)
err = db.Tasks().Update(c.Request.Context(), task.Task)
if err != nil {
return nil, err
}
return task, nil
}))
g.DELETE("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
task, err := l.FindTask(c.Request.Context(), c.Param("id"))
if err != nil {
return nil, err
}
if len(task.Logs) > 0 {
return nil, slerrors.Forbidden("cannot delete tasks with logs.")
}
err = db.Tasks().Delete(c.Request.Context(), task.Task)
if err != nil {
return nil, err
}
return task, nil
}))
}