@@ -0,0 +1,153 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gardomatic.kleiax.de/internal/platform/validate"
|
||||
"gardomatic.kleiax.de/internal/storage"
|
||||
)
|
||||
|
||||
type taskPriorityInput struct {
|
||||
Name *string `json:"name"`
|
||||
Value *int `json:"value"`
|
||||
SortOrder *int `json:"sort_order"`
|
||||
Active *bool `json:"active"`
|
||||
}
|
||||
|
||||
func (input taskPriorityInput) apply(value *storage.TaskPriority) {
|
||||
if input.Name != nil {
|
||||
value.Name = strings.TrimSpace(*input.Name)
|
||||
}
|
||||
if input.Value != nil {
|
||||
value.Value = *input.Value
|
||||
}
|
||||
if input.SortOrder != nil {
|
||||
value.SortOrder = *input.SortOrder
|
||||
}
|
||||
if input.Active != nil {
|
||||
value.Active = *input.Active
|
||||
}
|
||||
}
|
||||
func (app *application) listTaskPrioritiesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
app.writeTaskPriorities(w, r, false)
|
||||
}
|
||||
func (app *application) listAdminTaskPrioritiesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
app.writeTaskPriorities(w, r, true)
|
||||
}
|
||||
func (app *application) writeTaskPriorities(w http.ResponseWriter, r *http.Request, includeInactive bool) {
|
||||
values, err := app.models.TaskPriorities.GetAll()
|
||||
if err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
if !includeInactive {
|
||||
active := values[:0]
|
||||
for _, value := range values {
|
||||
if value.Active {
|
||||
active = append(active, value)
|
||||
}
|
||||
}
|
||||
values = active
|
||||
}
|
||||
if err := app.writeJSON(w, http.StatusOK, envelope{"priorities": values}, nil); err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
func (app *application) createAdminTaskPriorityHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var input taskPriorityInput
|
||||
if err := app.readJSON(w, r, &input); err != nil {
|
||||
app.badRequestResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
value := storage.TaskPriority{Active: true}
|
||||
input.apply(&value)
|
||||
if !app.validateTaskPriority(w, r, value) {
|
||||
return
|
||||
}
|
||||
value, err := app.models.TaskPriorities.Insert(value)
|
||||
if err != nil {
|
||||
app.respondToEntityModelError(w, r, err)
|
||||
return
|
||||
}
|
||||
headers := make(http.Header)
|
||||
headers.Set("Location", fmt.Sprintf("/v1/admin/task-priorities/%d", value.ID))
|
||||
if err := app.writeJSON(w, http.StatusCreated, envelope{"priority": value}, headers); err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
func (app *application) updateAdminTaskPriorityHandler(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := app.readIDParam(r)
|
||||
if err != nil {
|
||||
app.notFoundResponse(w, r)
|
||||
return
|
||||
}
|
||||
value, err := app.models.TaskPriorities.Get(id)
|
||||
if err != nil {
|
||||
app.respondToEntityModelError(w, r, err)
|
||||
return
|
||||
}
|
||||
var input taskPriorityInput
|
||||
if err := app.readJSON(w, r, &input); err != nil {
|
||||
app.badRequestResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
input.apply(&value)
|
||||
if !app.validateTaskPriority(w, r, value) {
|
||||
return
|
||||
}
|
||||
value, err = app.models.TaskPriorities.Update(value)
|
||||
if err != nil {
|
||||
app.respondToEntityModelError(w, r, err)
|
||||
return
|
||||
}
|
||||
if err := app.writeJSON(w, http.StatusOK, envelope{"priority": value}, nil); err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
func (app *application) deleteAdminTaskPriorityHandler(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := app.readIDParam(r)
|
||||
if err != nil {
|
||||
app.notFoundResponse(w, r)
|
||||
return
|
||||
}
|
||||
value, err := app.models.TaskPriorities.Get(id)
|
||||
if err != nil {
|
||||
app.respondToEntityModelError(w, r, err)
|
||||
return
|
||||
}
|
||||
value.Active = false
|
||||
if _, err = app.models.TaskPriorities.Update(value); err != nil {
|
||||
app.respondToEntityModelError(w, r, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
func (app *application) validateTaskPriority(w http.ResponseWriter, r *http.Request, value storage.TaskPriority) bool {
|
||||
v := validate.New()
|
||||
storage.ValidateTaskPriority(v, value)
|
||||
if !v.Valid() {
|
||||
app.failedValidationResponse(w, r, v.Errors)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (app *application) validateConfiguredPriority(w http.ResponseWriter, r *http.Request, v *validate.Validator, value int) bool {
|
||||
if app.models.TaskPriorities == nil {
|
||||
return true
|
||||
}
|
||||
priorities, err := app.models.TaskPriorities.GetAll()
|
||||
if err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
return false
|
||||
}
|
||||
for _, priority := range priorities {
|
||||
if priority.Value == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
v.AddError("priority", "must refer to a configured task priority")
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user