daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Jan 21st, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.    "github.com/gorilla/mux"
  5.    "encoding/json"
  6.    "net/http"
  7.    "log"
  8.    "structs"
  9.    "database/sql"
  10.    _ "github.com/go-sql-driver/mysql"
  11.    "io/ioutil"
  12.    "strconv"
  13.    // "net/url"
  14.    "fmt"
  15.    //"os"
  16.    //"strings"
  17.    //
  18.    //"io"
  19. )
  20.  
  21. var categs, recipes = structs.InitAll()
  22. var conf = structs.GetConfig()
  23. var db, err = sql.Open("mysql", conf["login"] + ":" + conf["password"] + "@/" + conf["dbname"])
  24.  
  25. func main() {
  26.  
  27.    if err != nil {
  28.      panic(err)
  29.    }
  30.    defer db.Close()
  31.  
  32.    r := mux.NewRouter()
  33.  
  34.    r.HandleFunc("/", MainHandler)
  35.    r.HandleFunc("/filter", RecipesGetFiltredHandler)
  36.    r.HandleFunc("/categories", CategoriesGetHandler)
  37.    r.HandleFunc("/category/{id}", CategRecipeGetHandler)
  38.    r.HandleFunc("/recipe/{id}", RecipeGetHandler)
  39.    r.HandleFunc("/recipes", RecipesGetHandler)
  40.    r.HandleFunc("/recipe/{id}/comment", CommentPostHandler)
  41.  
  42.    log.Println("Listen port " + conf["port"])
  43.    http.ListenAndServe(":" + conf["port"], r)
  44. }
  45.  
  46. func RecipesGetFiltredHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  47.  
  48.   keys := iReq.URL.Query()
  49.  
  50.   fmt.Println(keys["fats_max"][0])
  51.  
  52.   var result []structs.Frecipe
  53.  
  54.   rows, err := db.Query("select * from " + conf["dbname"] + "." + conf["recipes"] + " where fats between ? and ? and proteins between ? and ? and carbohydrates between ? and ? and calories between ? and ?",
  55.                          keys["fats_min"][0],
  56.                          keys["fats_max"][0], keys["proteins_min"][0], keys["proteins_max"][0], keys["carbohyd_min"][0], keys["carbohyd_max"][0],
  57.                          keys["calories_min"][0], keys["calories_max"][0])
  58.  
  59.   if err != nil {
  60.     fmt.Println("60")
  61.     fmt.Println(err)
  62.   }
  63.  
  64.   for rows.Next() {
  65.  
  66.     rec := structs.Frecipe{}
  67.  
  68.     err = rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  69.                      &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  70.                      &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  71.  
  72.     if err != nil {
  73.       fmt.Println("72")
  74.       fmt.Println(err)
  75.     }
  76.  
  77.     result = append(result, rec)
  78.   }
  79.  
  80.   if len(result) == 0 {
  81.     iWrt.WriteHeader(http.StatusNotFound)
  82.   } else {
  83.     outJson, err := json.Marshal(result)
  84.  
  85.     if err != nil {
  86.       fmt.Println("86")
  87.       fmt.Println(err)
  88.     }
  89.  
  90.     iWrt.Write(outJson)
  91.   }
  92. }
  93.  
  94. func CommentPostHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  95.     var resp structs.PostForm
  96.     vars := mux.Vars(iReq)
  97.  
  98.     id, err := strconv.Atoi(vars["id"])
  99.  
  100.     if err != nil {
  101.       panic(err)
  102.     }
  103.  
  104.     body, err := ioutil.ReadAll(iReq.Body)
  105.  
  106.     if err != nil {
  107.       panic(err)
  108.     } else {
  109.       err = json.Unmarshal(body, &resp)
  110.       if err != nil {
  111.         panic(err)
  112.       }
  113.     }
  114.  
  115.     _, err = db.Exec("insert into " + conf["dbname"] + "." + conf["comments"] + " (id, text) values (?, ?)", id, resp.Text)
  116.  
  117.     if err != nil {
  118.       panic(err)
  119.     } else {
  120.       iWrt.WriteHeader(http.StatusCreated)
  121.     }
  122. }
  123.  
  124. func RecipesGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  125.   var result []structs.Frecipe
  126.  
  127.   rows, error := db.Query("select * from " + conf["dbname"] + "." + conf["recipes"])
  128.   if error != nil {
  129.     panic(error)
  130.   }
  131.  
  132.   for rows.Next() {
  133.     rec := structs.Frecipe{}
  134.     err := rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  135.                      &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  136.                      &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  137.     if err != nil {
  138.       panic(err)
  139.       continue
  140.     }
  141.     result = append(result, rec)
  142.  
  143.   }
  144.  
  145.   if len(result) == 0 {
  146.     iWrt.WriteHeader(http.StatusNotFound)
  147.   } else {
  148.     outJson, err := json.Marshal(result)
  149.  
  150.     if err != nil {
  151.       panic(err)
  152.     }
  153.  
  154.     iWrt.Write(outJson)
  155.   }
  156. }
  157.  
  158. func MainHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  159.    iWrt.WriteHeader(http.StatusNotFound)
  160. }
  161.  
  162. func RecipeGetHandler(iWrt http.ResponseWriter, iReq *http.Request){
  163.  vars := mux.Vars(iReq)
  164.  
  165.  recid, err := strconv.Atoi(vars["id"])
  166.  
  167.  if err != nil {
  168.    fmt.Println(err)
  169.  }
  170.  
  171.  row := db.QueryRow("select * from " + conf["dbname"] + "." + conf["recipes"] + " where id = ?", recid)
  172.  
  173.  rec := structs.Frecipe{}
  174.  
  175.  err = row.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  176.                   &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  177.                   &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  178.  
  179.  if err != nil{
  180.    fmt.Println(err)
  181.    iWrt.WriteHeader(http.StatusNotFound)
  182.  } else {
  183.  
  184.    outJson, _ := json.Marshal(rec)
  185.    iWrt.Write(outJson)
  186.  }
  187. }
  188.  
  189. func CategoriesGetHandler(iWrt http.ResponseWriter, iReq *http.Request){
  190.  var result []structs.Categ
  191.  
  192.  rows, err := db.Query("select * from " + conf["dbname"] + "." + conf["recipes_types"])
  193.  
  194.  if err != nil {
  195.    fmt.Println(err)
  196.  }
  197.  
  198.  for rows.Next() {
  199.    var categ = structs.Categ{}
  200.  
  201.    err = rows.Scan(&categ.Recipe_type, &categ.Type_name, &categ.Type_image)
  202.  
  203.    if err != nil {
  204.      fmt.Println(err)
  205.    }
  206.  
  207.    result = append(result, categ)
  208.  }
  209.  
  210.  if len(result) != 0 {
  211.   outJson, _ := json.Marshal(result)
  212.   iWrt.Write(outJson)
  213.  } else {
  214.    iWrt.WriteHeader(http.StatusNotFound)
  215.  }
  216. }
  217.  
  218. func CategRecipeGetHandler(iWrt http.ResponseWriter, iReq *http.Request){
  219.  vars := mux.Vars(iReq)
  220.  
  221.  category, err := strconv.Atoi(vars["id"])
  222.  
  223.  if err != nil {
  224.    panic(err)
  225.  }
  226.  
  227.  var result []structs.Frecipe
  228.  
  229.  rows, err := db.Query("select * from " + conf["dbname"] + "." + conf["recipes"] + " where type_id = ?", category)
  230.  
  231.  if err != nil {
  232.    panic(err)
  233.  }
  234.  
  235.  for rows.Next(){
  236.    rec := structs.Frecipe{}
  237.  
  238.    err = rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  239.                     &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  240.                     &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  241.  
  242.    if err != nil {
  243.      panic(err)
  244.    }
  245.  
  246.    result = append(result, rec)
  247.  }
  248.  
  249.  if len(result) == 0 {
  250.    iWrt.WriteHeader(http.StatusNotFound)
  251.  } else {
  252.    outJson, err := json.Marshal(result)
  253.  
  254.    if err != nil {
  255.      panic(err)
  256.    }
  257.  
  258.    iWrt.Write(outJson)
  259.  }
  260. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top