The profile list will now properly display the users recipes! The favorites list does not exist yet, since there is no backend support for favoriting/saving recipes. So the list displays the same content as the user recipe list. Same goes for the activity list, not yet implemented.
147 lines
3.8 KiB
Go
147 lines
3.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/gin-gonic/gin"
|
|
domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe"
|
|
domainServer "github.com/haydenhargreaves/Potion/internal/domain/server"
|
|
layouts "github.com/haydenhargreaves/Potion/internal/templates/layouts"
|
|
pages "github.com/haydenhargreaves/Potion/internal/templates/pages"
|
|
)
|
|
|
|
func LoginPage(ctx *gin.Context) {
|
|
title := "Potion - Login"
|
|
page := pages.LoginPage()
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|
|
|
|
func HomePage(ctx *gin.Context) {
|
|
title := "Potion - Home"
|
|
page := pages.HomePage()
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|
|
|
|
func FavoritesPage(ctx *gin.Context) {
|
|
title := "Potion - Favorites"
|
|
page := pages.FavoritesPage()
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|
|
|
|
func CreatePage(ctx *gin.Context) {
|
|
// If not logged in, direct to the login page
|
|
if !domainServer.IsLoggedIn(ctx) {
|
|
ctx.Redirect(http.StatusSeeOther, domainServer.WEB_LOGIN)
|
|
return
|
|
}
|
|
|
|
title := "Potion - Create"
|
|
page := pages.CreatePage()
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|
|
|
|
func ProfilePage(ctx *gin.Context) {
|
|
// If not logged in, direct to the login page
|
|
if !domainServer.IsLoggedIn(ctx) {
|
|
ctx.Redirect(http.StatusSeeOther, domainServer.WEB_LOGIN)
|
|
return
|
|
}
|
|
|
|
// Else, get the user data
|
|
deps := ctx.MustGet("deps").(*domainServer.InjectedDependencies)
|
|
user := deps.UserService.GetAuthenicatedUser(ctx)
|
|
recipes, err := deps.RecipeService.GetUserRecipes(user.Id)
|
|
if err != nil {
|
|
fmt.Printf("Error getting recipes. %s\n", err.Error())
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{
|
|
"status": http.StatusInternalServerError,
|
|
"message": fmt.Sprintf("Error getting recipes. %s\n", err.Error()),
|
|
})
|
|
return
|
|
}
|
|
|
|
title := "Potion - Profile"
|
|
page := pages.ProfilePage(user, recipes)
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|
|
|
|
func ListPage(ctx *gin.Context) {
|
|
title := "Potion - Shopping List"
|
|
page := pages.ListPage()
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|
|
|
|
// TODO: Figure out how to handle errors, think we just need a simple display.
|
|
func RecipePage(ctx *gin.Context) {
|
|
// Call recipe service to get via ID
|
|
deps := ctx.MustGet("deps").(*domainServer.InjectedDependencies)
|
|
id := ctx.Param("id")
|
|
|
|
// Parse ID
|
|
parsed, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
ctx.JSON(400, err.Error())
|
|
return
|
|
}
|
|
|
|
// Get recipe
|
|
recipe, err := deps.RecipeService.GetRecipe(parsed)
|
|
if err != nil {
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
ctx.JSON(400, err.Error())
|
|
return
|
|
}
|
|
|
|
// Get user
|
|
user, err := deps.UserService.GetUser(recipe.UserId)
|
|
if err != nil {
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
ctx.JSON(400, err.Error())
|
|
return
|
|
}
|
|
|
|
title := "Potion - View Recipe"
|
|
page := pages.RecipePage(*recipe, *user)
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|
|
|
|
func SearchPage(ctx *gin.Context) {
|
|
var page templ.Component
|
|
// Get filters from cookies
|
|
if bytes, err := ctx.Cookie("search-filters"); err != nil {
|
|
fmt.Printf("ERROR: Failed to get search-filter cookie. %s\n", err.Error())
|
|
page = pages.SearchPage(domainRecipe.SearchFilters{}, false)
|
|
} else {
|
|
var filters domainRecipe.SearchFilters
|
|
if err := json.Unmarshal([]byte(bytes), &filters); err != nil {
|
|
fmt.Printf("ERROR: Failed to unmarshal search-filter cookie. %s\n", err.Error())
|
|
page = pages.SearchPage(domainRecipe.SearchFilters{}, false)
|
|
} else {
|
|
page = pages.SearchPage(filters, true)
|
|
}
|
|
}
|
|
|
|
title := "Potion - Recipe Search"
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|
|
|
|
func NotFoundPage(ctx *gin.Context) {
|
|
title := "Potion - Not Found"
|
|
page := pages.NotFoundPage()
|
|
|
|
ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page))
|
|
}
|