Add Update function to the client

This commit is contained in:
Andrea Fazzi 2020-12-29 18:16:45 +01:00
parent 34696e34f8
commit 3f1632af2b
2 changed files with 30 additions and 14 deletions

View file

@ -173,6 +173,34 @@ func (c *Client) Create(model interface{}) (uint, error) {
return uint(id), nil return uint(id), nil
} }
func (c *Client) Update(model orm.IDer) (uint, error) {
var response renderer.JsonResponse
data, err := json.Marshal(model)
if err != nil {
return 0, err
}
resp, err := c.SendRequest("POST", fmt.Sprintf("/api/%s/%d/update?format=json", reflect.ModelNameLowerPlural(model), model.GetID()), data)
if err != nil {
return 0, err
}
if err := json.Unmarshal(resp, &response); err != nil {
return 0, err
}
if string(response.Error) != "" {
return 0, errors.New(string(response.Error))
}
id, err := strconv.Atoi(string(response.Result))
if err != nil {
return 0, err
}
return uint(id), nil
}
func (c *Client) Delete(model orm.IDer) (uint, error) { func (c *Client) Delete(model orm.IDer) (uint, error) {
var response renderer.JsonResponse var response renderer.JsonResponse
data, err := json.Marshal(model) data, err := json.Marshal(model)

View file

@ -297,19 +297,6 @@ func DefaultRecoverHandler(next http.Handler) http.Handler {
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
// func (h *Handlers) setFlashMessage(w http.ResponseWriter, r *http.Request, key string) error {
// session, err := h.CookieStore.Get(r, "flash-session")
// if err != nil {
// return err
// }
// session.AddFlash(i18n.FlashMessages[key][h.Config.Language])
// err = session.Save(r, w)
// if err != nil {
// return err
// }
// return nil
// }
func (h *Handlers) hasPermission(r *http.Request, path string) bool { func (h *Handlers) hasPermission(r *http.Request, path string) bool {
claims := getClaims(r) claims := getClaims(r)
role := claims["role"].(string) role := claims["role"].(string)
@ -406,8 +393,9 @@ func respondWithError(h *Handlers, w http.ResponseWriter, r *http.Request, err e
if format == "" { if format == "" {
format = renderer.GetContentFormat(r) format = renderer.GetContentFormat(r)
} }
panicMsg := fmt.Sprintf("PANIC: %v\n\n== STACKTRACE ==\n%s", err, debug.Stack())
if h.Config.LogLevel > config.LOG_LEVEL_OFF { if h.Config.LogLevel > config.LOG_LEVEL_OFF {
log.Println("Error:", err) log.Println("Error:", panicMsg)
} }
// FIXME: this call could be superflous when an error occurs // FIXME: this call could be superflous when an error occurs
// in a template execution // in a template execution