initial commit
This commit is contained in:
commit
d40b69f1f9
58 changed files with 7919 additions and 0 deletions
57
backend/routes/task-get.go
Normal file
57
backend/routes/task-get.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["GET"]["/task"] = new(TasksGET)
|
||||
}
|
||||
|
||||
type tasksResponse struct {
|
||||
Tasks []db.Task `json:"tasks"`
|
||||
Categories []db.Category `json:"categories"`
|
||||
}
|
||||
|
||||
type TasksGET struct{}
|
||||
|
||||
// Authorize implements router.AuthorizedRoute.
|
||||
func (t *TasksGET) Authorize(ctx *router.Context) error {
|
||||
return middleware.Authenticated(ctx)
|
||||
}
|
||||
|
||||
// Handle implements router.Route.
|
||||
func (t *TasksGET) Handle(ctx *router.Context) error {
|
||||
tx, err := ctx.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return ctx.GenericError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
query := ctx.Query.WithTx(tx)
|
||||
|
||||
tasks, err := query.AllTasks(ctx)
|
||||
if err != nil {
|
||||
return ctx.GenericError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
taskIDs := make([]int64, len(tasks))
|
||||
for i, t := range tasks {
|
||||
taskIDs[i] = t.ID
|
||||
}
|
||||
|
||||
categories, err := query.CategoriesOf(ctx, taskIDs)
|
||||
if err != nil {
|
||||
return ctx.GenericError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return ctx.JSON(200, tasksResponse{
|
||||
Tasks: tasks,
|
||||
Categories: categories,
|
||||
})
|
||||
}
|
||||
|
||||
var _ router.AuthorizedRoute = new(TasksGET)
|
||||
Loading…
Add table
Add a link
Reference in a new issue