package controller

import (
	"encoding/json"
	"io"
	"net/http"

	"xengineering.eu/ceres/model"
)

func Recipe(w http.ResponseWriter, r *http.Request) {
	buf, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	recipe := model.Recipe{}
	err = json.Unmarshal(buf, &recipe)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	err = recipe.Update()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	http.Redirect(w, r, "/recipe/" + recipe.Id, http.StatusSeeOther)
}