The frontend is half wired up, just need to update the button. I also want to update the recipe methods to return the favorite status. This will follow very similar to the way I updated the tags. Another method which can be called to attach the favorite state.
81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"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)
|
|
id := ctx.Param("id")
|
|
|
|
if !domain.IsLoggedIn(ctx) {
|
|
// TODO: Anon view
|
|
ctx.Status(http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
recipeId, _ := strconv.Atoi(id)
|
|
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.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)
|
|
}
|
|
}
|