package repository import ( "database/sql" "encoding/json" "fmt" domain "github.com/haydenhargreaves/Potion/internal/domain/recipe" "github.com/lib/pq" ) type RecipeRepository struct { db *sql.DB } // Compile-time check to ensure the RecipeRepository implements domain.RecipeRepository var _ domain.RecipeRepository = (*RecipeRepository)(nil) // NewRecipeRepository creates a user repository object which is used by the user service to access // the database. Any recipe related database operations will take place in this repository. func NewRecipeRepository(db *sql.DB) domain.RecipeRepository { return &RecipeRepository{db: db} } // NOTE: This function modified the provided recipe with the new values, such as id and time stamp // CreateRecipe creates a recipe in the database. The recipe provided should contain all data except // time stamps and the ID; the database will fill them when the operation succeeds. Any errors will // be bubbled to the caller. The recipe parameter is passed by reference and will therefore be updated // directly and the new fields (ID, created) can be accessed upon success. func (r *RecipeRepository) CreateRecipe(recipe *domain.Recipe) error { tx, err := r.db.Begin() if err != nil { tx.Rollback() return err } query := `INSERT INTO recipes ( title, description, instructions, serves, difficulty, duration, category, ingredients, userid, modified, created ) VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11 ) RETURNING id;` // NOTE: Data steps // cast duration to JSON // cast ingredients to JSON // cast category to string // use nil for the modified time durationJSON, err := json.Marshal(recipe.Duration) if err != nil { return err } ingredientsJSON, err := json.Marshal(recipe.Ingredients) if err != nil { return err } var id int if err = tx.QueryRow( query, recipe.Title, recipe.Description, pq.Array(recipe.Instructions), recipe.Serves, recipe.Difficulty, durationJSON, string(recipe.Category), ingredientsJSON, recipe.UserId, nil, recipe.Created, ).Scan(&id); err != nil { tx.Rollback() return err } if err := tx.Commit(); err != nil { tx.Rollback() return err } // Set the new ID recipe.Id = id return nil } // GetRecipe gets a recipe from the database via its ID. The operation is wrapped in a transaction // for added safety. The repository will not check for a nil result, instead the service will. Callers // are responsible for protecting against double nil results. Any errors will be bubbled to the caller. func (r *RecipeRepository) GetRecipe(id int) (*domain.Recipe, error) { tx, err := r.db.Begin() if err != nil { tx.Rollback() return nil, err } query := "SELECT * FROM recipes WHERE id = $1" var durationBytes []byte var ingredientBytes []byte var recipe domain.Recipe if err := tx.QueryRow(query, id).Scan( &recipe.Id, &recipe.Title, &recipe.Description, pq.Array(&recipe.Instructions), &recipe.Serves, &recipe.Difficulty, &durationBytes, &recipe.Category, &ingredientBytes, &recipe.UserId, &recipe.Modified, &recipe.Created, ); err != nil { return nil, fmt.Errorf("Failed to location recipe in database: %s", err.Error()) } if err := tx.Commit(); err != nil { tx.Rollback() return nil, err } // Parse duration if len(durationBytes) > 0 { var duration domain.RecipeDuration if err := json.Unmarshal(durationBytes, &duration); err != nil { return nil, fmt.Errorf("Failed to parse duration from database: %s", err.Error()) } recipe.Duration = duration } // Parse ingredient if len(ingredientBytes) > 0 { var ingredients []domain.RecipeIngredient if err := json.Unmarshal(ingredientBytes, &ingredients); err != nil { return nil, fmt.Errorf("Failed to parse ingredients from database: %s", err.Error()) } recipe.Ingredients = ingredients } return &recipe, nil }