-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package models | ||
|
||
type Ingredient struct { | ||
Name string `json:"name"` | ||
Unit string `json:"unit"` | ||
Quantity float64 `json:"quantity"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package models | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type Recipe struct { | ||
ID uuid.UUID `json:"id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Ingredients []Ingredient `json:"ingredients"` | ||
} | ||
|
||
func CreateRecipe(db *sql.DB, name string, description string, ingredients []Ingredient) (Recipe, error) { | ||
recipe := Recipe{ | ||
ID: uuid.New(), | ||
Name: name, | ||
Description: description, | ||
Ingredients: ingredients, | ||
} | ||
|
||
jsonIngredients, err := json.Marshal(recipe.Ingredients) | ||
if err != nil { | ||
return Recipe{}, err | ||
} | ||
|
||
_, err = db.Exec("INSERT INTO recipes (id, name, description, ingredients) VALUES (?, ?, ?, ?)", | ||
recipe.ID, recipe.Name, recipe.Description, jsonIngredients) | ||
if err != nil { | ||
return Recipe{}, err | ||
} | ||
|
||
return recipe, nil | ||
} | ||
|
||
func GetUserRecipes(db *sql.DB, token string) ([]Recipe, error) { | ||
user, err := GetUser(db, uuid.MustParse(token)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var recipeIds []string | ||
|
||
rows, err := db.Query("SELECT rid FROM user_recipe WHERE uid = ?", user.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var recipeId string | ||
if err := rows.Scan(&recipeId); err != nil { | ||
return nil, err | ||
} | ||
recipeIds = append(recipeIds, recipeId) | ||
} | ||
|
||
rows, err = db.Query("SELECT id, name, description, ingredients FROM recipes WHERE id IN (" + strings.Join(recipeIds, ",") + ")") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer rows.Close() | ||
|
||
var recipes []Recipe | ||
for rows.Next() { | ||
var recipe Recipe | ||
var jsonIngredients string | ||
if err := rows.Scan(&recipe.ID, &recipe.Name, &recipe.Description, &jsonIngredients); err != nil { | ||
return nil, err | ||
} | ||
err := json.Unmarshal(([]byte)(jsonIngredients), &recipe.Ingredients) | ||
if err != nil { | ||
return nil, err | ||
} | ||
recipes = append(recipes, recipe) | ||
} | ||
|
||
return recipes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package routes | ||
|
||
import ( | ||
"CookingApp/models" | ||
"database/sql" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type createRecipeBody struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Ingredients []models.Ingredient `json:"ingredients"` | ||
} | ||
|
||
func CreateRecipe(c *gin.Context) { | ||
db := c.MustGet("db").(*sql.DB) | ||
|
||
var body createRecipeBody | ||
if err := c.BindJSON(&body); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
} | ||
|
||
recipe, err := models.CreateRecipe(db, body.Name, body.Description, body.Ingredients) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, recipe) | ||
} | ||
|
||
func GetUserRecipes(c *gin.Context) { | ||
db := c.MustGet("db").(*sql.DB) | ||
token := c.MustGet("token").(string) | ||
recipes, err := models.GetUserRecipes(db, token) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, recipes) | ||
} |