initial commit
This commit is contained in:
commit
d40b69f1f9
58 changed files with 7919 additions and 0 deletions
91
backend/routes/user-tasks.go
Normal file
91
backend/routes/user-tasks.go
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"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"]["/user/{id}/tasks"] = new(UserTasks)
|
||||
}
|
||||
|
||||
type userTasksResponse struct {
|
||||
Tasks []db.Task `json:"tasks"`
|
||||
TaskCategories []db.TaskCategory `json:"task_categories"`
|
||||
Categories []db.Category `json:"categories"`
|
||||
}
|
||||
|
||||
type UserTasks struct{}
|
||||
|
||||
// Authorize implements router.AuthorizedRoute.
|
||||
func (u *UserTasks) Authorize(ctx *router.Context) error {
|
||||
return middleware.Authenticated(ctx)
|
||||
}
|
||||
|
||||
// ValidateParams implements router.ValidatedParamRoute.
|
||||
func (u *UserTasks) ValidateParams(ctx *router.Context) error {
|
||||
if ctx.Parameter("id") == "me" {
|
||||
return nil
|
||||
}
|
||||
return middleware.ValidateUUIDParam(ctx, "id")
|
||||
}
|
||||
|
||||
// Handle implements router.Route.
|
||||
func (u *UserTasks) Handle(ctx *router.Context) error {
|
||||
user := middleware.User(u, ctx)
|
||||
id := ctx.Parameter("id")
|
||||
|
||||
if id == "me" {
|
||||
id = user.ID
|
||||
}
|
||||
|
||||
tx, err := ctx.DB.BeginTx(ctx, &sql.TxOptions{
|
||||
ReadOnly: true,
|
||||
})
|
||||
|
||||
query := ctx.Query.WithTx(tx)
|
||||
|
||||
if err != nil {
|
||||
return ctx.GenericError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
tasks, err := query.TasksForUser(ctx, id)
|
||||
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return ctx.GenericError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
taskIDs := make([]int64, len(tasks))
|
||||
for i, task := range tasks {
|
||||
taskIDs[i] = task.ID
|
||||
}
|
||||
|
||||
taskCategories, err := query.TaskCategoriesOf(ctx, taskIDs)
|
||||
if err != nil {
|
||||
return ctx.GenericError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
categories, err := query.CategoriesOf(ctx, taskIDs)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return ctx.GenericError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return ctx.GenericError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return ctx.JSON(200, userTasksResponse{
|
||||
Tasks: append([]db.Task{}, tasks...),
|
||||
TaskCategories: append([]db.TaskCategory{}, taskCategories...),
|
||||
Categories: append([]db.Category{}, categories...),
|
||||
})
|
||||
}
|
||||
|
||||
var _ router.AuthorizedRoute = new(UserTasks)
|
||||
var _ router.ValidatedParamRoute = new(UserTasks)
|
||||
Loading…
Add table
Add a link
Reference in a new issue