added account

This commit is contained in:
2025-10-25 15:55:25 +02:00
parent 72c50549d7
commit 4db5084bc6
15 changed files with 1214 additions and 150 deletions

View File

@@ -2,6 +2,7 @@ package handlers
import (
"html/template"
"mealprep/auth"
"mealprep/database"
"net/http"
"strconv"
@@ -10,7 +11,8 @@ import (
// MealsHandler handles the meals page
func MealsHandler(w http.ResponseWriter, r *http.Request) {
meals, err := database.GetAllMeals()
userID := auth.GetUserID(r)
meals, err := database.GetAllMeals(userID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -72,6 +74,8 @@ func MealsHandler(w http.ResponseWriter, r *http.Request) {
// AddMealHandler handles adding a new meal
func AddMealHandler(w http.ResponseWriter, r *http.Request) {
userID := auth.GetUserID(r)
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@@ -92,7 +96,7 @@ func AddMealHandler(w http.ResponseWriter, r *http.Request) {
return
}
id, err := database.AddMeal(name, description, mealType)
id, err := database.AddMeal(userID, name, description, mealType)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -141,6 +145,8 @@ func AddMealHandler(w http.ResponseWriter, r *http.Request) {
// DeleteMealHandler handles deleting a meal
func DeleteMealHandler(w http.ResponseWriter, r *http.Request) {
userID := auth.GetUserID(r)
idStr := strings.TrimPrefix(r.URL.Path, "/meals/")
id, err := strconv.Atoi(idStr)
if err != nil {
@@ -148,7 +154,7 @@ func DeleteMealHandler(w http.ResponseWriter, r *http.Request) {
return
}
if err := database.DeleteMeal(id); err != nil {
if err := database.DeleteMeal(userID, id); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -158,6 +164,8 @@ func DeleteMealHandler(w http.ResponseWriter, r *http.Request) {
// GetMealIngredientsHandler shows ingredients for a specific meal
func GetMealIngredientsHandler(w http.ResponseWriter, r *http.Request) {
userID := auth.GetUserID(r)
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 3 {
http.Error(w, "Invalid URL", http.StatusBadRequest)
@@ -170,13 +178,13 @@ func GetMealIngredientsHandler(w http.ResponseWriter, r *http.Request) {
return
}
mealIngredients, err := database.GetMealIngredients(mealID)
mealIngredients, err := database.GetMealIngredients(userID, mealID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
allIngredients, err := database.GetAllIngredients()
allIngredients, err := database.GetAllIngredients(userID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -233,6 +241,8 @@ func GetMealIngredientsHandler(w http.ResponseWriter, r *http.Request) {
// AddMealIngredientHandler adds an ingredient to a meal
func AddMealIngredientHandler(w http.ResponseWriter, r *http.Request) {
userID := auth.GetUserID(r)
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 3 {
http.Error(w, "Invalid URL", http.StatusBadRequest)
@@ -262,13 +272,13 @@ func AddMealIngredientHandler(w http.ResponseWriter, r *http.Request) {
return
}
if err := database.AddMealIngredient(mealID, ingredientID, quantity); err != nil {
if err := database.AddMealIngredient(userID, mealID, ingredientID, quantity); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Get the ingredient details to display
ingredients, err := database.GetMealIngredients(mealID)
ingredients, err := database.GetMealIngredients(userID, mealID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -308,6 +318,8 @@ func AddMealIngredientHandler(w http.ResponseWriter, r *http.Request) {
// DeleteMealIngredientHandler removes an ingredient from a meal
func DeleteMealIngredientHandler(w http.ResponseWriter, r *http.Request) {
userID := auth.GetUserID(r)
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 5 {
http.Error(w, "Invalid URL", http.StatusBadRequest)
@@ -326,7 +338,7 @@ func DeleteMealIngredientHandler(w http.ResponseWriter, r *http.Request) {
return
}
if err := database.DeleteMealIngredient(mealID, ingredientID); err != nil {
if err := database.DeleteMealIngredient(userID, mealID, ingredientID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}