35 lines
798 B
Go
35 lines
798 B
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"gitea.kleinsense.nl/DariusKlein/kleinTodo/common"
|
|
"gitea.kleinsense.nl/DariusKlein/kleinTodo/common/jwt"
|
|
)
|
|
|
|
func UpdateHandler(w http.ResponseWriter, r *http.Request) {
|
|
// 1. Setup: Auth, Decode, Database Connection
|
|
user, err := jwt.GetVerifiedUser(r)
|
|
if handleError(w, http.StatusUnauthorized, err) {
|
|
return
|
|
}
|
|
|
|
var todo common.Todo
|
|
if handleError(w, http.StatusBadRequest, json.NewDecoder(r.Body).Decode(&todo)) {
|
|
return
|
|
}
|
|
|
|
store, err := common.GetTodoDataStore()
|
|
if handleError(w, http.StatusInternalServerError, err) {
|
|
return
|
|
}
|
|
|
|
// 2. Store the todo (it updates LastModified in the Store method)
|
|
if handleError(w, http.StatusInternalServerError, todo.Store(store, user)) {
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|