Potion/internal/app/handlers/engagement_handler.go
Hayden Hargreaves 15cb03589c (UI/FIX): Fixed the view engagement to only run on clicks.
But that means we have to redirect from the handler. I didn't want to,
but I guess that makes it easier when more pages direct to the recipe
page.
2025-07-15 21:44:20 -07:00

115 lines
2.9 KiB
Go

package handlers
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
domain "github.com/haydenhargreaves/Potion/internal/domain/server"
)
func EngagementViewRecipe(ctx *gin.Context) {
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
recipeId, _ := strconv.Atoi(ctx.Param("id"))
if !domain.IsLoggedIn(ctx) {
if _, err := deps.EngagementService.ViewRecipe(recipeId); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
} else {
ctx.Header("HX-Redirect", fmt.Sprintf(domain.WEB_RECIPE, recipeId))
ctx.Status(http.StatusOK)
}
return
}
userId := ctx.MustGet("userId").(int)
if _, err := deps.EngagementService.UserViewRecipe(userId, recipeId); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
} else {
ctx.Header("HX-Redirect", fmt.Sprintf(domain.WEB_RECIPE, recipeId))
ctx.Status(http.StatusOK)
}
}
func EngagementShareRecipe(ctx *gin.Context) {
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
recipeId, _ := strconv.Atoi(ctx.Param("id"))
if !domain.IsLoggedIn(ctx) {
if _, err := deps.EngagementService.ShareRecipe(recipeId); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
} else {
ctx.Status(http.StatusNoContent)
}
return
}
userId := ctx.MustGet("userId").(int)
if _, err := deps.EngagementService.UserShareRecipe(userId, recipeId); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
} else {
ctx.Status(http.StatusNoContent)
}
}
func EngagementFavoriteRecipe(ctx *gin.Context) {
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
if !domain.IsLoggedIn(ctx) {
ctx.Header("HX-Redirect", domain.WEB_LOGIN)
ctx.Status(http.StatusOK)
return
}
id := ctx.Param("id")
recipeId, _ := strconv.Atoi(id)
userId := ctx.MustGet("userId").(int)
if _, err := deps.EngagementService.UserFavoriteRecipe(userId, recipeId); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
} else {
ctx.Status(http.StatusNoContent)
}
}
func EngagementMakeRecipe(ctx *gin.Context) {
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
if !domain.IsLoggedIn(ctx) {
ctx.Header("HX-Redirect", domain.WEB_LOGIN)
ctx.Status(http.StatusOK)
return
}
id := ctx.Param("id")
recipeId, _ := strconv.Atoi(id)
userId := ctx.MustGet("userId").(int)
if _, err := deps.EngagementService.UserMakeRecipe(userId, recipeId); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": err.Error(),
})
} else {
ctx.Status(http.StatusNoContent)
}
}