Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "github.com/gorilla/mux"
- "encoding/json"
- "net/http"
- "log"
- "structs"
- "database/sql"
- _ "github.com/go-sql-driver/mysql"
- "io/ioutil"
- "strconv"
- // "net/url"
- "fmt"
- //"os"
- //"strings"
- //
- //"io"
- )
- var categs, recipes = structs.InitAll()
- var conf = structs.GetConfig()
- var db, err = sql.Open("mysql", conf["login"] + ":" + conf["password"] + "@/" + conf["dbname"])
- func main() {
- if err != nil {
- panic(err)
- }
- defer db.Close()
- r := mux.NewRouter()
- r.HandleFunc("/", MainHandler)
- r.HandleFunc("/filter", RecipesGetFiltredHandler)
- r.HandleFunc("/categories", CategoriesGetHandler)
- r.HandleFunc("/category/{id}", CategRecipeGetHandler)
- r.HandleFunc("/recipe/{id}", RecipeGetHandler)
- r.HandleFunc("/recipes", RecipesGetHandler)
- r.HandleFunc("/recipe/{id}/comment", CommentPostHandler)
- log.Println("Listen port " + conf["port"])
- http.ListenAndServe(":" + conf["port"], r)
- }
- func RecipesGetFiltredHandler(iWrt http.ResponseWriter, iReq *http.Request) {
- keys := iReq.URL.Query()
- fmt.Println(keys["fats_max"][0])
- var result []structs.Frecipe
- 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 ?",
- keys["fats_min"][0],
- keys["fats_max"][0], keys["proteins_min"][0], keys["proteins_max"][0], keys["carbohyd_min"][0], keys["carbohyd_max"][0],
- keys["calories_min"][0], keys["calories_max"][0])
- if err != nil {
- fmt.Println("60")
- fmt.Println(err)
- }
- for rows.Next() {
- rec := structs.Frecipe{}
- err = rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
- &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
- &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
- if err != nil {
- fmt.Println("72")
- fmt.Println(err)
- }
- result = append(result, rec)
- }
- if len(result) == 0 {
- iWrt.WriteHeader(http.StatusNotFound)
- } else {
- outJson, err := json.Marshal(result)
- if err != nil {
- fmt.Println("86")
- fmt.Println(err)
- }
- iWrt.Write(outJson)
- }
- }
- func CommentPostHandler(iWrt http.ResponseWriter, iReq *http.Request) {
- var resp structs.PostForm
- vars := mux.Vars(iReq)
- id, err := strconv.Atoi(vars["id"])
- if err != nil {
- panic(err)
- }
- body, err := ioutil.ReadAll(iReq.Body)
- if err != nil {
- panic(err)
- } else {
- err = json.Unmarshal(body, &resp)
- if err != nil {
- panic(err)
- }
- }
- _, err = db.Exec("insert into " + conf["dbname"] + "." + conf["comments"] + " (id, text) values (?, ?)", id, resp.Text)
- if err != nil {
- panic(err)
- } else {
- iWrt.WriteHeader(http.StatusCreated)
- }
- }
- func RecipesGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
- var result []structs.Frecipe
- rows, error := db.Query("select * from " + conf["dbname"] + "." + conf["recipes"])
- if error != nil {
- panic(error)
- }
- for rows.Next() {
- rec := structs.Frecipe{}
- err := rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
- &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
- &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
- if err != nil {
- panic(err)
- continue
- }
- result = append(result, rec)
- }
- if len(result) == 0 {
- iWrt.WriteHeader(http.StatusNotFound)
- } else {
- outJson, err := json.Marshal(result)
- if err != nil {
- panic(err)
- }
- iWrt.Write(outJson)
- }
- }
- func MainHandler(iWrt http.ResponseWriter, iReq *http.Request) {
- iWrt.WriteHeader(http.StatusNotFound)
- }
- func RecipeGetHandler(iWrt http.ResponseWriter, iReq *http.Request){
- vars := mux.Vars(iReq)
- recid, err := strconv.Atoi(vars["id"])
- if err != nil {
- fmt.Println(err)
- }
- row := db.QueryRow("select * from " + conf["dbname"] + "." + conf["recipes"] + " where id = ?", recid)
- rec := structs.Frecipe{}
- err = row.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
- &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
- &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
- if err != nil{
- fmt.Println(err)
- iWrt.WriteHeader(http.StatusNotFound)
- } else {
- outJson, _ := json.Marshal(rec)
- iWrt.Write(outJson)
- }
- }
- func CategoriesGetHandler(iWrt http.ResponseWriter, iReq *http.Request){
- var result []structs.Categ
- rows, err := db.Query("select * from " + conf["dbname"] + "." + conf["recipes_types"])
- if err != nil {
- fmt.Println(err)
- }
- for rows.Next() {
- var categ = structs.Categ{}
- err = rows.Scan(&categ.Recipe_type, &categ.Type_name, &categ.Type_image)
- if err != nil {
- fmt.Println(err)
- }
- result = append(result, categ)
- }
- if len(result) != 0 {
- outJson, _ := json.Marshal(result)
- iWrt.Write(outJson)
- } else {
- iWrt.WriteHeader(http.StatusNotFound)
- }
- }
- func CategRecipeGetHandler(iWrt http.ResponseWriter, iReq *http.Request){
- vars := mux.Vars(iReq)
- category, err := strconv.Atoi(vars["id"])
- if err != nil {
- panic(err)
- }
- var result []structs.Frecipe
- rows, err := db.Query("select * from " + conf["dbname"] + "." + conf["recipes"] + " where type_id = ?", category)
- if err != nil {
- panic(err)
- }
- for rows.Next(){
- rec := structs.Frecipe{}
- err = rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
- &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
- &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
- if err != nil {
- panic(err)
- }
- result = append(result, rec)
- }
- if len(result) == 0 {
- iWrt.WriteHeader(http.StatusNotFound)
- } else {
- outJson, err := json.Marshal(result)
- if err != nil {
- panic(err)
- }
- iWrt.Write(outJson)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement