Ejemplo n.º 1
0
// handleViewMealPlan handles HTTP requests for the meal plan viewer.
func handleViewMealPlan(w http.ResponseWriter, r *http.Request) {
	mpID, ok := getUint64Var(r, "mealplanid")
	if !ok {
		httpError(w, BadRequestError)
		return
	}

	var mp *mpdata.MealPlan

	err := mpdb.WithConnection(func(db *sql.DB) (err error) {
		return mpdb.WithTransaction(db, func(tx *sql.Tx) (err error) {
			mp, err = mpdb.GetMealPlan(tx, mpID)
			return err
		})
	})

	if err != nil {
		serverError(w, err)
		return
	}

	if mp == nil {
		httpError(w, NotFoundError)
		return
	}

	renderTemplate(w, "view-mp.html", mp)
}
Ejemplo n.º 2
0
// handleDeleteMealPlanForm handles HTTP requests for the meal plan deletion
// confirmation page.
func handleDeleteMealPlanForm(w http.ResponseWriter, r *http.Request) {
	mpID, ok := getUint64Var(r, "mealplanid")
	if !ok {
		httpError(w, BadRequestError)
		return
	}

	var mp *mpdata.MealPlan
	var numServings int

	err := mpdb.WithConnection(func(db *sql.DB) (err error) {
		return mpdb.WithTransaction(db, func(tx *sql.Tx) (err error) {
			mp, err = mpdb.GetMealPlan(tx, mpID)
			if err != nil {
				return err
			}

			numServings, err = mpdb.CountServings(tx, mpID)
			return err
		})
	})

	if err != nil {
		serverError(w, err)
		return
	}

	if mp == nil {
		httpError(w, NotFoundError)
		return
	}

	renderTemplate(w, "delete-mp-form.html", deleteMPData{mp, numServings})
}