Potion/internal/app/service/recipe_service.go
Hayden Hargreaves b17c5774e9 (UI): Implemented much of the frontend recipe creation wizard.
Most everything is implemented, included a state handler and a pretty
simple (but workable) system for managing state in HTML. Nice and simple
for now.

There is still much work to be done, but the rest is simple backend
creation and error handling. And then input validation...a nightmare.
2025-06-29 22:30:20 -07:00

67 lines
2.0 KiB
Go

package service
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
domain "github.com/haydenhargreaves/Potion/internal/domain/recipe"
)
// RecipeService implements the domain.RecipeService defined in the domain module.
type RecipeService struct {
recipeRepository domain.RecipeRepository
}
// Compile-time check to ensure the RecipeService implements domain.RecipeService
var _ domain.RecipeService = (*RecipeService)(nil)
// NewRecipeService creates a user service object which can be passed into the context. The service
// requires a recipe repository which it will use to hit the database when needed.
func NewRecipeService(recipeRepository domain.RecipeRepository) domain.RecipeService {
return &RecipeService{recipeRepository: recipeRepository}
}
func (s *RecipeService) CreateRecipe(ctx *gin.Context) domain.Recipe {
// TODO: Implement
recipe := domain.Recipe{
Title: "Delicious Go Curry",
Description: "A savory and easy-to-make curry, perfect for weeknights.",
Instructions: []string{
"Chop all vegetables.",
"Sauté onions and garlic until fragrant.",
"Add curry paste and stir for 1 minute.",
"Add coconut milk and vegetables, simmer until cooked.",
"Serve with rice.",
},
Serves: 4,
Difficulty: 3,
Duration: domain.RecipeDuration{
Total: 45,
Prep: 15,
Cook: 30,
},
Category: domain.MealDinner, // Using our EMeal type. Ensure this matches an updated enum value.
Ingredients: []domain.RecipeIngredient{
{Name: "Onion", Quantity: "1 large"},
{Name: "Garlic", Quantity: "3 cloves"},
{Name: "Curry Paste", Quantity: "2 tbsp"},
{Name: "Coconut Milk", Quantity: "400ml can"},
{Name: "Broccoli", Quantity: "1 head"},
{Name: "Bell Pepper", Quantity: "1 red"},
{Name: "Rice", Quantity: "As needed"},
},
UserId: 3,
Created: time.Now(),
}
if err := s.recipeRepository.CreateRecipe(&recipe); err != nil {
ctx.JSON(http.StatusOK, gin.H{"err": err.Error()})
return domain.Recipe{}
}
ctx.JSON(http.StatusCreated, gin.H{"recipe": recipe})
return recipe
}