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"
@@ -11,13 +12,15 @@ import (
// WeekPlanHandler handles the week plan page
func WeekPlanHandler(w http.ResponseWriter, r *http.Request) {
weekPlan, err := database.GetWeekPlan()
userID := auth.GetUserID(r)
weekPlan, err := database.GetWeekPlan(userID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
meals, err := database.GetAllMeals()
meals, err := database.GetAllMeals(userID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -224,6 +227,8 @@ func WeekPlanHandler(w http.ResponseWriter, r *http.Request) {
// AddWeekPlanEntryHandler adds a meal to a specific day
func AddWeekPlanEntryHandler(w http.ResponseWriter, r *http.Request) {
userID := auth.GetUserID(r)
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@@ -249,13 +254,13 @@ func AddWeekPlanEntryHandler(w http.ResponseWriter, r *http.Request) {
return
}
if err := database.AddWeekPlanEntry(date, mealID); err != nil {
if err := database.AddWeekPlanEntry(userID, date, mealID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Get the meal details
meal, err := database.GetMealByID(mealID)
meal, err := database.GetMealByID(userID, mealID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -268,7 +273,7 @@ func AddWeekPlanEntryHandler(w http.ResponseWriter, r *http.Request) {
}
// Get the ID of the entry we just added
weekPlan, err := database.GetWeekPlan()
weekPlan, err := database.GetWeekPlan(userID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -305,6 +310,8 @@ func AddWeekPlanEntryHandler(w http.ResponseWriter, r *http.Request) {
// DeleteWeekPlanEntryHandler removes a meal from the week plan
func DeleteWeekPlanEntryHandler(w http.ResponseWriter, r *http.Request) {
userID := auth.GetUserID(r)
idStr := strings.TrimPrefix(r.URL.Path, "/week-plan/")
id, err := strconv.Atoi(idStr)
if err != nil {
@@ -312,7 +319,7 @@ func DeleteWeekPlanEntryHandler(w http.ResponseWriter, r *http.Request) {
return
}
if err := database.DeleteWeekPlanEntry(id); err != nil {
if err := database.DeleteWeekPlanEntry(userID, id); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}