57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.red-panda.pet/pandaware/house/backend/db"
|
|
"git.red-panda.pet/pandaware/house/backend/middleware"
|
|
"git.red-panda.pet/pandaware/house/backend/router"
|
|
)
|
|
|
|
func init() {
|
|
routes["POST"]["/task/{id}/complete"] = new(TaskComplete)
|
|
}
|
|
|
|
type taskCompleteResponse struct {
|
|
Task db.Task `json:"task"`
|
|
}
|
|
|
|
type TaskComplete struct{}
|
|
|
|
// ValidateParams implements router.ValidatedParamRoute.
|
|
func (t *TaskComplete) ValidateParams(ctx *router.Context) error {
|
|
return middleware.ValidateIDParam(ctx, "id")
|
|
}
|
|
|
|
// Authorize implements router.AuthorizedRoute.
|
|
func (t *TaskComplete) Authorize(ctx *router.Context) error {
|
|
return middleware.Authenticated(ctx)
|
|
}
|
|
|
|
// Handle implements router.Route.
|
|
func (t *TaskComplete) Handle(ctx *router.Context) error {
|
|
id := middleware.ParamID(t, ctx)
|
|
|
|
tx, err := ctx.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return ctx.GenericError(err, http.StatusInternalServerError)
|
|
}
|
|
|
|
query := ctx.Query.WithTx(tx)
|
|
|
|
task, err := query.CompleteTask(ctx, id)
|
|
if err != nil {
|
|
return ctx.GenericError(err, http.StatusInternalServerError)
|
|
}
|
|
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
return ctx.GenericError(err, http.StatusInternalServerError)
|
|
}
|
|
return ctx.JSON(200, taskCompleteResponse{
|
|
Task: task,
|
|
})
|
|
}
|
|
|
|
var _ router.AuthorizedRoute = new(TaskComplete)
|
|
var _ router.ValidatedParamRoute = new(TaskComplete)
|