feat: added endpoint for ingredients and ingredeients per recipe
This commit is contained in:
parent
3c98c82f8a
commit
083b2fcaa3
188
server/main.go
188
server/main.go
|
@ -1,32 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"encoding/json"
|
|
||||||
"os"
|
"os"
|
||||||
"github.com/golang-jwt/jwt"
|
|
||||||
"time"
|
|
||||||
"database/sql"
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateJwt(username string) (string, error) {
|
|
||||||
token := jwt.New(jwt.SigningMethodEdDSA)
|
|
||||||
|
|
||||||
claims := token.Claims.(jwt.MapClaims)
|
|
||||||
claims["exp"] = time.Now().Add(10 * time.Minute)
|
|
||||||
claims["authorized"] = true
|
|
||||||
claims["user"] = username
|
|
||||||
|
|
||||||
tokenString, err := token.SignedString(os.Getenv("GREPFOOD_JWT_SECRET"))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return tokenString, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteJson(w http.ResponseWriter, v any) {
|
func WriteJson(w http.ResponseWriter, v any) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
@ -40,7 +23,9 @@ func WriteJson(w http.ResponseWriter, v any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleVersion(w http.ResponseWriter, r *http.Request) {
|
func handleVersion(w http.ResponseWriter, r *http.Request) {
|
||||||
version := struct {Version string `json:version`} {
|
version := struct {
|
||||||
|
Version string `json:version`
|
||||||
|
}{
|
||||||
Version: "0.1.0-demo",
|
Version: "0.1.0-demo",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +92,142 @@ type RecipeIngredient struct {
|
||||||
Amount int `json:amount`
|
Amount int `json:amount`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetRecipes(db *sql.DB) ([]Recipe, error) {
|
||||||
|
rows, err := db.Query("SELECT name, icon FROM recipe")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
recipes := []Recipe{}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
recipe := Recipe{}
|
||||||
|
rows.Scan(&recipe.Name, &recipe.Icon)
|
||||||
|
recipes = append(recipes, recipe)
|
||||||
|
}
|
||||||
|
|
||||||
|
return recipes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIngredients(db *sql.DB) ([]Ingredient, error) {
|
||||||
|
rows, err := db.Query("SELECT name, icon, price FROM ingredient")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ingredients := []Ingredient{}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
ingredient := Ingredient{}
|
||||||
|
rows.Scan(&ingredient.Name, &ingredient.Icon, &ingredient.Price)
|
||||||
|
ingredients = append(ingredients, ingredient)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ingredients, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRecipeIngredients(recipe_name string, db *sql.DB) ([]Ingredient, error) {
|
||||||
|
rows, err := db.Query(fmt.Sprintf("SELECT id FROM recipe WHERE name='%s'", recipe_name))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
id := 0
|
||||||
|
if rows.Next() {
|
||||||
|
rows.Scan(&id)
|
||||||
|
} else {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err = db.Query(fmt.Sprintf("SELECT ingredient.name, ingredient.icon, ingredient.price FROM ingredient INNER JOIN recipe_ingredient ON recipe_ingredient.ingredient_d=ingredient.id WHERE recipe_ingredient.recipe_id=%d", id))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ingredients := []Ingredient{}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
ingredient := Ingredient{}
|
||||||
|
rows.Scan(&ingredient.Name, &ingredient.Icon, &ingredient.Price)
|
||||||
|
ingredients = append(ingredients, ingredient)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ingredients, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleApiRecipes(w http.ResponseWriter, r *http.Request, db *sql.DB) {
|
||||||
|
if r.Method != http.MethodGet && r.Method != "" {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
log.Print("Mismatched method: ", r.Method)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
recipes, err := GetRecipes(db)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||||
|
log.Print("Error fetching recipes: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteJson(w, recipes)
|
||||||
|
|
||||||
|
log.Print("Fetched ", len(recipes), " recipes from database")
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleApiIngredients(w http.ResponseWriter, r *http.Request, db *sql.DB) {
|
||||||
|
if r.Method != http.MethodGet && r.Method != "" {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
log.Print("Mismatched method: ", r.Method)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ingredients, err := GetIngredients(db)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||||
|
log.Print("Error fetching ingredients: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Print("Fetched ", len(ingredients), " ingredients from database")
|
||||||
|
|
||||||
|
WriteJson(w, ingredients)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleApiRecipeIngredients(w http.ResponseWriter, r *http.Request, db *sql.DB) {
|
||||||
|
if r.Method != http.MethodGet && r.Method != "" {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
log.Print("Mismatched method: ", r.Method)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
recipe_name := r.PathValue("name")
|
||||||
|
log.Print("Fetching ingredients for recipe ", recipe_name)
|
||||||
|
|
||||||
|
ingredients, err := GetRecipeIngredients(recipe_name, db)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||||
|
log.Print("Error fetching ingredients: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if ingredients == nil {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteJson(w, ingredients)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleApiIngedientAdd(w http.ResponseWriter, r *http.Request, db *sql.DB) {
|
||||||
|
if r.Method != http.MethodGet && r.Method != "" {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
log.Print("Mismatched method: ", r.Method)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dbconn := os.Getenv("GREPFOOD_DATABASE_CONNECTION")
|
dbconn := os.Getenv("GREPFOOD_DATABASE_CONNECTION")
|
||||||
|
|
||||||
|
@ -119,24 +240,15 @@ func main() {
|
||||||
InitDatabase(db)
|
InitDatabase(db)
|
||||||
|
|
||||||
http.HandleFunc("/version", handleVersion)
|
http.HandleFunc("/version", handleVersion)
|
||||||
|
http.Handle("/", http.FileServer(http.Dir("static")))
|
||||||
http.HandleFunc("/api/recipes", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/api/recipes", func(w http.ResponseWriter, r *http.Request) {
|
||||||
rows, err := db.Query("SELECT name, icon FROM recipe")
|
HandleApiRecipes(w, r, db)
|
||||||
if err != nil {
|
})
|
||||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
http.HandleFunc("/api/ingredients", func(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
HandleApiIngredients(w, r, db)
|
||||||
}
|
})
|
||||||
|
http.HandleFunc("/api/recipe/{name}/ingredients", func(w http.ResponseWriter, r *http.Request) {
|
||||||
recipes := struct { Recipes []Recipe `json:recipes`} {
|
HandleApiRecipeIngredients(w, r, db)
|
||||||
Recipes: []Recipe{},
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
recipe := Recipe{}
|
|
||||||
rows.Scan(&recipe.Name, &recipe.Icon)
|
|
||||||
recipes.Recipes = append(recipes.Recipes, recipe)
|
|
||||||
}
|
|
||||||
|
|
||||||
WriteJson(w, recipes)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(os.Getenv("GREPFOOD_SYNCSERVER_PORT"), nil))
|
log.Fatal(http.ListenAndServe(os.Getenv("GREPFOOD_SYNCSERVER_PORT"), nil))
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
INSERT INTO grepfood.public.ingredient (name, icon, price)
|
||||||
|
VALUES
|
||||||
|
('potato', 'potato.png', 399),
|
||||||
|
('tomato', 'tomato.png', 199),
|
||||||
|
('flour', 'flour.png', 050),
|
||||||
|
('water', 'waterglass.png', 005);
|
||||||
|
|
||||||
|
INSERT INTO grepfood.public.recipe (name, icon)
|
||||||
|
VALUES
|
||||||
|
('bread', 'bread.png'),
|
||||||
|
('pizza', 'pizza.png'),
|
||||||
|
('cookie', 'cookie.png'),
|
||||||
|
('pasta', 'pasta.png');
|
Loading…
Reference in New Issue