initial commit
This commit is contained in:
commit
d40b69f1f9
58 changed files with 7919 additions and 0 deletions
57
backend/routes/task-complete.go
Normal file
57
backend/routes/task-complete.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue