The UI is wired up and connected as well, for the profile page. Next step is to create the favorites page.
90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
domain "github.com/haydenhargreaves/Potion/internal/domain/server"
|
|
)
|
|
|
|
func GetUserRecipes(ctx *gin.Context) {
|
|
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
|
|
|
|
// Ensure logged in
|
|
if !domain.IsLoggedIn(ctx) {
|
|
ctx.JSON(http.StatusUnauthorized, gin.H{
|
|
"status": http.StatusUnauthorized,
|
|
"message": "User is not authorized to access this endpoint. Please login to continue.",
|
|
"recipes": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
userId, ok := ctx.MustGet("userId").(int)
|
|
if !ok {
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{
|
|
"status": http.StatusInternalServerError,
|
|
"message": "Unable to access user id from store.",
|
|
"recipes": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
recipes, err := deps.RecipeService.GetUserRecipes(userId)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusBadRequest, gin.H{
|
|
"status": http.StatusBadRequest,
|
|
"message": fmt.Sprintf("Could not get user recipes. %s", err.Error()),
|
|
"recipes": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"status": http.StatusOK,
|
|
"message": "User recipes successfully retrieved.",
|
|
"recipes": recipes,
|
|
})
|
|
}
|
|
|
|
func GetUserFavoriteRecipes(ctx *gin.Context) {
|
|
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
|
|
|
|
// Ensure logged in
|
|
if !domain.IsLoggedIn(ctx) {
|
|
ctx.JSON(http.StatusUnauthorized, gin.H{
|
|
"status": http.StatusUnauthorized,
|
|
"message": "User is not authorized to access this endpoint. Please login to continue.",
|
|
"recipes": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
userId, ok := ctx.MustGet("userId").(int)
|
|
if !ok {
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{
|
|
"status": http.StatusInternalServerError,
|
|
"message": "Unable to access user id from store.",
|
|
"recipes": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
recipes, err := deps.RecipeService.GetUserFavoriteRecipes(userId)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusBadRequest, gin.H{
|
|
"status": http.StatusBadRequest,
|
|
"message": fmt.Sprintf("Could not get user recipes. %s", err.Error()),
|
|
"recipes": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"status": http.StatusOK,
|
|
"message": "User recipes successfully retrieved.",
|
|
"recipes": recipes,
|
|
})
|
|
}
|