From 5521f0b7d49c81791acc6e2a897d36b341e70d8e Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 9 Nov 2019 19:16:41 -0500 Subject: Add make Recipe w/ id & database connection --- backend/recipe.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'backend') diff --git a/backend/recipe.go b/backend/recipe.go index 4fb8816..1d6da90 100644 --- a/backend/recipe.go +++ b/backend/recipe.go @@ -1,5 +1,8 @@ package main +import "database/sql" +import "strings" + type Ingredient struct { Name string Amount float64 @@ -43,3 +46,75 @@ func MakeRecipe() *Recipe { } +func RecipeFromId(id int, db *sql.DB) *Recipe { + var rec Recipe + var ingr Ingredient + var step Step + + rows, err := db.Query("SELECT id, title, photo_urls, keywords, description, serving_size, cook_time, rating, num_cooked FROM recipes WHERE id = $1", id) + if err == nil { + var id, rating, num_cooked, servings, cook_time int + var title, photo_urls, keywords, desc string + + defer rows.Close() + rows.Next() + rows.Scan(&id, &title, &photo_urls, &keywords, &desc, &servings, &cook_time, &rating, &num_cooked) + rec = Recipe{ + Id: id, + Title: title, + Desc: desc, + Photos: make([]string, 0), + Serving_size: servings, + Cook_time: cook_time, + Rating: rating, + Keywords: make([]string, 0), + Ingredients: make([]Ingredient, 0), + Steps: make([]Step, 0)} + + if len(photo_urls) > 0 { + photos := strings.Split(photo_urls, "|") + for _, p := range photos { + rec.Photos = append(rec.Photos, p) + } + } + if len(keywords) > 0 { + keyword_split := strings.Split(keywords, "|") + for _, k := range keyword_split { + rec.Keywords = append(rec.Keywords, k) + } + } + + var name, unit string + var amount float64 + rows, err = db.Query("SELECT name, amount, unit FROM ingredients WHERE recipe_id = $1", id) + if err == nil { + for rows.Next() { + rows.Scan(&name, &amount, &unit) + ingr = Ingredient{ + Name: name, + Amount: amount, + Unit: unit, + } + rec.Ingredients = append(rec.Ingredients, ingr) + } + } + + var num, timer int + rows, err = db.Query("SELECT step, description, timer FROM steps WHERE recipe_id = $1", id) + if err == nil { + for rows.Next() { + rows.Scan(&num, &desc, &timer) + step = Step{ + Num: num, + Desc: desc, + Time: timer, + } + rec.Steps = append(rec.Steps, step) + } + } + + return &rec + } + + return nil +} -- cgit v1.1