Advertisement
Guest User

Untitled

a guest
Jan 27th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.21 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "database/sql"
  5. "encoding/json"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "structs"
  12.  
  13. _ "github.com/go-sql-driver/mysql"
  14. "github.com/gorilla/mux"
  15.  
  16. // "net/url"
  17. "fmt"
  18. //"os"
  19. //"io"
  20. )
  21.  
  22. var categs, recipes = structs.InitAll()
  23. var conf = structs.GetConfig()
  24. var db, err = sql.Open("mysql", conf["login"]+":"+conf["password"]+"@/"+conf["dbname"])
  25.  
  26. func main() {
  27.  
  28. if err != nil {
  29. fmt.Println(err)
  30. }
  31. defer db.Close()
  32.  
  33. r := mux.NewRouter()
  34.  
  35. r.HandleFunc("/", MainHandler)
  36. r.HandleFunc("/auth", AuthenticationHandler)
  37. r.HandleFunc("/register", RegisterHandler)
  38. r.HandleFunc("/filter", RecipesGetFiltredHandler)
  39. r.HandleFunc("/category/{id}", CategRecipeGetHandler)
  40. r.HandleFunc("/categories", CategoriesGetHandler)
  41. r.HandleFunc("/food", FoodGetHandler)
  42. r.HandleFunc("/food/{id}/allergen", AddAllergenHandler)
  43. r.HandleFunc("/food/{id}/delallergen", DelAllergenHandler)
  44. r.HandleFunc("/recipe/{id}/addtofav", AddToFavPostHandler)
  45. r.HandleFunc("/recipe/{id}/deletefromfav", DeleteFromFavHandler)
  46. r.HandleFunc("/recipe/{id}/comments", CommentsGetHandler)
  47. r.HandleFunc("/recipe/{id}/comment", CommentPostHandler)
  48. r.HandleFunc("/recipe/{id}", RecipeGetHandler)
  49. r.HandleFunc("/recipes", RecipesGetHandler)
  50. r.HandleFunc("/user/getfavs", FavsGetHandler)
  51. r.HandleFunc("/user/allergens", AllergensHandler)
  52. r.HandleFunc("/user/menu/{meal}", MenuGetHandler)
  53. r.HandleFunc("/user/menuadd/{meal}", MenuPostHandler)
  54. r.HandleFunc("/user/menudel/{meal}", DelMenuHandler)
  55.  
  56. log.Println("Listen port " + conf["port"])
  57. http.ListenAndServe(":"+conf["port"], r)
  58. }
  59.  
  60. func AllergensHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  61. fmt.Println(iReq)
  62.  
  63. body, err := ioutil.ReadAll(iReq.Body)
  64.  
  65. if err != nil {
  66. fmt.Println(err)
  67. }
  68.  
  69. var data map[string]string
  70.  
  71. err = json.Unmarshal(body, &data)
  72.  
  73. if err != nil {
  74. fmt.Println(err)
  75. }
  76.  
  77. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  78.  
  79. var uid int
  80.  
  81. err = row.Scan(&uid)
  82.  
  83. rows, err := db.Query("select ingredient_id from "+conf["dbname"]+"."+conf["allergens"]+" where user_id = ?", uid)
  84.  
  85. if err != nil {
  86. fmt.Println(err)
  87. }
  88.  
  89. var result []structs.ShortFoodForm
  90.  
  91. for rows.Next() {
  92. rec := structs.ShortFoodForm{}
  93.  
  94. var ingid int
  95. err = rows.Scan(&ingid)
  96.  
  97. if err != nil {
  98. fmt.Println(err)
  99. }
  100.  
  101. meal := db.QueryRow("select id, name from "+conf["dbname"]+"."+conf["ingredients"]+" where id = ?", ingid)
  102.  
  103. err = meal.Scan(&rec.Id, &rec.Name)
  104.  
  105. if err != nil {
  106. fmt.Println(err)
  107. }
  108.  
  109. result = append(result, rec)
  110. }
  111.  
  112. if len(result) == 0 {
  113. fmt.Println(err)
  114. iWrt.WriteHeader(http.StatusNotFound)
  115. } else {
  116. outJson, err := json.Marshal(result)
  117.  
  118. if err != nil {
  119. fmt.Println(err)
  120. }
  121.  
  122. iWrt.Write(outJson)
  123. }
  124. }
  125.  
  126. func DelMenuHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  127. fmt.Println(iReq)
  128.  
  129. vars := mux.Vars(iReq)
  130.  
  131. body, err := ioutil.ReadAll(iReq.Body)
  132.  
  133. if err != nil {
  134. fmt.Println(err)
  135. }
  136.  
  137. var data map[string]string
  138. err = json.Unmarshal(body, &data)
  139.  
  140. if err != nil {
  141. fmt.Println(err)
  142. }
  143.  
  144. var uid, recid int
  145.  
  146. mealtime := vars["meal"]
  147. day := data["day"]
  148. recid, err = strconv.Atoi(data["recipe_id"])
  149.  
  150. if err != nil {
  151. fmt.Println(err)
  152. }
  153.  
  154. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  155.  
  156. err = row.Scan(&uid)
  157.  
  158. if err != nil {
  159. fmt.Println(err)
  160. }
  161.  
  162. _, err = db.Exec("delete from "+conf["dbname"]+"."+conf["recday"]+" where user_id = ? and recipe_id = ? and day = ? and mealtime = ?", uid, recid, day, mealtime)
  163.  
  164. if err != nil {
  165. fmt.Println(err)
  166. iWrt.WriteHeader(http.StatusTeapot)
  167. } else {
  168. iWrt.WriteHeader(http.StatusOK)
  169. }
  170.  
  171. }
  172.  
  173. func DelAllergenHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  174. fmt.Println(iReq)
  175.  
  176. vars := mux.Vars(iReq)
  177.  
  178. body, err := ioutil.ReadAll(iReq.Body)
  179.  
  180. if err != nil {
  181. fmt.Println(err)
  182. }
  183.  
  184. var data map[string]string
  185.  
  186. err = json.Unmarshal(body, &data)
  187.  
  188. if err != nil {
  189. fmt.Println(err)
  190. }
  191.  
  192. var uid, allid int
  193.  
  194. allid, err = strconv.Atoi(vars["id"])
  195.  
  196. if err != nil {
  197. fmt.Println(err)
  198. }
  199.  
  200. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  201.  
  202. err = row.Scan(&uid)
  203.  
  204. if err != nil {
  205. fmt.Println(err)
  206. }
  207.  
  208. deleter, err := db.Prepare("delete from " + conf["dbname"] + "." + conf["allergens"] + " where user_id = ? and ingredient_id = ?")
  209.  
  210. if err != nil {
  211. fmt.Println(err)
  212. }
  213.  
  214. _, err = deleter.Exec(uid, allid)
  215.  
  216. if err != nil {
  217. fmt.Println(err)
  218. iWrt.WriteHeader(http.StatusTeapot)
  219. } else {
  220. iWrt.WriteHeader(http.StatusOK)
  221. }
  222. }
  223.  
  224. func AddAllergenHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  225. fmt.Println(iReq)
  226.  
  227. vars := mux.Vars(iReq)
  228.  
  229. ingid, err := strconv.Atoi(vars["id"])
  230.  
  231. if err != nil {
  232. fmt.Println(err)
  233. }
  234.  
  235. body, err := ioutil.ReadAll(iReq.Body)
  236.  
  237. if err != nil {
  238. fmt.Println(err)
  239. }
  240.  
  241. var data map[string]string
  242. err = json.Unmarshal(body, &data)
  243.  
  244. if err != nil {
  245. fmt.Println(err)
  246. }
  247.  
  248. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  249.  
  250. var uid int
  251. err = row.Scan(&uid)
  252.  
  253. inserter, err := db.Prepare("insert into " + conf["dbname"] + "." + conf["allergens"] + " (ingredient_id, user_id) values(?, ?)")
  254.  
  255. _, err = inserter.Exec(ingid, uid)
  256.  
  257. if err != nil {
  258. fmt.Println(err)
  259. iWrt.WriteHeader(http.StatusTeapot)
  260. } else {
  261. iWrt.WriteHeader(http.StatusCreated)
  262. }
  263. }
  264.  
  265. func FoodGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  266. fmt.Println(iReq)
  267.  
  268. var result []structs.ShortFoodForm
  269.  
  270. rows, err := db.Query("select id, name from " + conf["dbname"] + "." + conf["ingredients"] + " order by name")
  271.  
  272. if err != nil {
  273. fmt.Println(err)
  274. }
  275.  
  276. for rows.Next() {
  277. rec := structs.ShortFoodForm{}
  278.  
  279. err = rows.Scan(&rec.Id, &rec.Name)
  280.  
  281. if err != nil {
  282. fmt.Println(err)
  283. }
  284.  
  285. result = append(result, rec)
  286. }
  287.  
  288. if len(result) == 0 {
  289. iWrt.WriteHeader(http.StatusNotFound)
  290. } else {
  291. outJson, err := json.Marshal(result)
  292.  
  293. if err != nil {
  294. fmt.Println(err)
  295. }
  296.  
  297. iWrt.Write(outJson)
  298. }
  299. }
  300.  
  301. func DeleteFromFavHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  302. fmt.Println(iReq)
  303. vars := mux.Vars(iReq)
  304. recid, err := strconv.Atoi(vars["id"])
  305.  
  306. if err != nil {
  307. fmt.Println(err)
  308. }
  309.  
  310. body, err := ioutil.ReadAll(iReq.Body)
  311.  
  312. if err != nil {
  313. fmt.Println(err)
  314. }
  315.  
  316. var data map[string]string
  317. err = json.Unmarshal(body, &data)
  318.  
  319. if err != nil {
  320. fmt.Println(err)
  321. }
  322.  
  323. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  324.  
  325. var uid int
  326. err = row.Scan(&uid)
  327.  
  328. if err != nil {
  329. fmt.Println(err)
  330. }
  331.  
  332. deleter, err := db.Prepare("delete from " + conf["dbname"] + "." + conf["favs"] + " where recipe_id = ? and user_id = ?")
  333.  
  334. if err != nil {
  335. fmt.Println(err)
  336. }
  337.  
  338. _, err = deleter.Exec(recid, uid)
  339.  
  340. if err != nil {
  341. fmt.Println(err)
  342. iWrt.WriteHeader(http.StatusTeapot)
  343. } else {
  344. iWrt.WriteHeader(http.StatusOK)
  345. }
  346. }
  347.  
  348. func MenuPostHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  349. fmt.Println(iReq)
  350. var data map[string]string
  351.  
  352. vars := mux.Vars(iReq)
  353.  
  354. body, err := ioutil.ReadAll(iReq.Body)
  355.  
  356. if err != nil {
  357. fmt.Println(err)
  358. }
  359.  
  360. err = json.Unmarshal(body, &data)
  361.  
  362. if err != nil {
  363. fmt.Println(err)
  364. }
  365.  
  366. fmt.Println(data)
  367.  
  368. recid, err := strconv.Atoi(data["recipe_id"])
  369.  
  370. if err != nil {
  371. fmt.Println(err)
  372. }
  373.  
  374. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  375.  
  376. var uid int
  377. err = row.Scan(&uid)
  378.  
  379. if err != nil {
  380. fmt.Println(err)
  381. }
  382.  
  383. _, err = db.Exec("insert into "+conf["dbname"]+"."+conf["recday"]+" (recipe_id, user_id, day, mealtime) values(?, ?, ?, ?)", recid, uid, data["day"], vars["meal"])
  384.  
  385. if err != nil {
  386. fmt.Println(err)
  387. iWrt.WriteHeader(http.StatusTeapot)
  388. } else {
  389. iWrt.WriteHeader(http.StatusCreated)
  390. }
  391. }
  392.  
  393. func FavsGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  394. fmt.Println(iReq)
  395. body, err := ioutil.ReadAll(iReq.Body)
  396.  
  397. if err != nil {
  398. fmt.Println(err)
  399. }
  400.  
  401. var data map[string]string
  402.  
  403. err = json.Unmarshal(body, &data)
  404.  
  405. if err != nil {
  406. fmt.Println(err)
  407. }
  408.  
  409. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  410.  
  411. var uid int
  412. err = row.Scan(&uid)
  413.  
  414. if err != nil {
  415. fmt.Println(err)
  416. }
  417.  
  418. var result []structs.Frecipe
  419.  
  420. rows, err := db.Query("select recipe_id from "+conf["dbname"]+"."+conf["favs"]+" where user_id = ?", uid)
  421.  
  422. for rows.Next() {
  423. var rec_id int
  424. rec := structs.Frecipe{}
  425. err = rows.Scan(&rec_id)
  426.  
  427. fmt.Println(rec_id)
  428.  
  429. if err != nil {
  430. fmt.Println(err)
  431. }
  432.  
  433. recipe := db.QueryRow("select * from "+conf["dbname"]+"."+conf["recipes"]+" where id = ?", rec_id)
  434.  
  435. err = recipe.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  436. &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  437. &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  438.  
  439. if err != nil {
  440. fmt.Println(err)
  441. }
  442.  
  443. result = append(result, rec)
  444. }
  445.  
  446. if len(result) == 0 {
  447. iWrt.WriteHeader(http.StatusNotFound)
  448. } else {
  449. outJson, err := json.Marshal(result)
  450.  
  451. if err != nil {
  452. fmt.Println(err)
  453. }
  454.  
  455. iWrt.Write(outJson)
  456. }
  457. }
  458.  
  459. func MenuGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  460. fmt.Println(iReq)
  461.  
  462. var data map[string]string
  463. var result []structs.Frecipe
  464.  
  465. vars := mux.Vars(iReq)
  466.  
  467. body, err := ioutil.ReadAll(iReq.Body)
  468.  
  469. if err != nil {
  470. fmt.Println(err)
  471. }
  472.  
  473. err = json.Unmarshal(body, &data)
  474.  
  475. if err != nil {
  476. fmt.Println(err)
  477. }
  478.  
  479. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  480.  
  481. fmt.Println("users")
  482.  
  483. var uid int
  484. err = row.Scan(&uid)
  485.  
  486. if err != nil {
  487. fmt.Println(err)
  488. }
  489.  
  490. rows, err := db.Query("select recipe_id from "+conf["dbname"]+"."+conf["recday"]+" where user_id = ? and mealtime = ? and day = ?", uid, vars["meal"], data["day"])
  491.  
  492. fmt.Println("recday")
  493.  
  494. if err != nil {
  495. fmt.Println(err)
  496. }
  497.  
  498. for rows.Next() {
  499. rec := structs.Frecipe{}
  500.  
  501. var recid int
  502. err = rows.Scan(&recid)
  503.  
  504. if err != nil {
  505. fmt.Println(err)
  506. }
  507.  
  508. recipe := db.QueryRow("select * from "+conf["dbname"]+"."+conf["recipes"]+" where id = ?", recid)
  509.  
  510. fmt.Println("recipes")
  511.  
  512. err = recipe.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  513. &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  514. &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  515.  
  516. if err != nil {
  517. fmt.Println(err)
  518. }
  519.  
  520. result = append(result, rec)
  521. }
  522.  
  523. if len(result) == 0 {
  524. iWrt.WriteHeader(http.StatusNotFound)
  525. } else {
  526. outJson, err := json.Marshal(result)
  527.  
  528. if err != nil {
  529. fmt.Println(err)
  530. }
  531.  
  532. iWrt.Write(outJson)
  533. }
  534. }
  535.  
  536. func AddToFavPostHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  537. fmt.Println(iReq)
  538.  
  539. vars := mux.Vars(iReq)
  540.  
  541. id, err := strconv.Atoi(vars["id"])
  542.  
  543. if err != nil {
  544. fmt.Println(err)
  545. }
  546.  
  547. var data map[string]string
  548. body, err := ioutil.ReadAll(iReq.Body)
  549.  
  550. if err != nil {
  551. fmt.Println(err)
  552. }
  553.  
  554. err = json.Unmarshal(body, &data)
  555.  
  556. if err != nil {
  557. fmt.Println(err)
  558. }
  559.  
  560. row := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  561.  
  562. var uid int
  563. err = row.Scan(&uid)
  564.  
  565. if err != nil {
  566. fmt.Println(err)
  567. }
  568.  
  569. if uid == 0 {
  570. return
  571. }
  572.  
  573. inserter, err := db.Prepare("insert into " + conf["dbname"] + "." + conf["favs"] + " (recipe_id, user_id) values(?, ?)")
  574.  
  575. if err != nil {
  576. fmt.Println(err)
  577. }
  578.  
  579. _, err = inserter.Exec(id, uid)
  580.  
  581. if err != nil {
  582. fmt.Println(err)
  583. iWrt.WriteHeader(http.StatusTeapot)
  584. } else {
  585. iWrt.WriteHeader(http.StatusCreated)
  586. }
  587. }
  588.  
  589. func AuthenticationHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  590. fmt.Println(iReq)
  591.  
  592. var data map[string]string
  593. body, err := ioutil.ReadAll(iReq.Body)
  594.  
  595. if err != nil {
  596. fmt.Println(err)
  597. }
  598.  
  599. err = json.Unmarshal(body, &data)
  600.  
  601. fmt.Println(data)
  602.  
  603. if err != nil {
  604. fmt.Println(err)
  605. }
  606.  
  607. rows, err := db.Query("select * from "+conf["dbname"]+"."+conf["users"]+" where login = ? and password = ?", data["login"], data["password"])
  608.  
  609. if rows.Next() {
  610. iWrt.WriteHeader(http.StatusOK)
  611. } else {
  612. iWrt.WriteHeader(http.StatusTeapot)
  613. }
  614. }
  615.  
  616. func RegisterHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  617. fmt.Println(iReq)
  618.  
  619. var data map[string]string
  620. body, err := ioutil.ReadAll(iReq.Body)
  621.  
  622. if err != nil {
  623. fmt.Println(err)
  624. }
  625.  
  626. err = json.Unmarshal(body, &data)
  627.  
  628. fmt.Println(data)
  629.  
  630. if err != nil {
  631. fmt.Println(err)
  632. }
  633.  
  634. rows, err := db.Query("select * from "+conf["dbname"]+"."+conf["users"]+" where login = ? or mail = ?",
  635. data["login"], data["mail"])
  636.  
  637. if err != nil {
  638. fmt.Println(err)
  639. }
  640.  
  641. acs := structs.AccessForm{}
  642.  
  643. acs.Access = !rows.Next()
  644.  
  645. if acs.Access {
  646. _, err = db.Exec("insert into "+conf["dbname"]+"."+conf["users"]+" (login, password, mail) values (?, ?, ?)",
  647. data["login"], data["password"], data["mail"])
  648. iWrt.WriteHeader(http.StatusCreated)
  649. } else {
  650. iWrt.WriteHeader(http.StatusTeapot)
  651. }
  652. }
  653.  
  654. func CommentsGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  655. fmt.Println(iReq)
  656.  
  657. vars := mux.Vars(iReq)
  658. recid, err := strconv.Atoi(vars["id"])
  659.  
  660. if err != nil {
  661. fmt.Println(err)
  662. }
  663.  
  664. var result []structs.PostForm
  665.  
  666. rows, err := db.Query("select id, text from "+conf["dbname"]+"."+conf["comments"]+" where recipe_id = ?", recid)
  667.  
  668. if err != nil {
  669. fmt.Println(err)
  670. }
  671.  
  672. for rows.Next() {
  673. rec := structs.PostForm{}
  674.  
  675. err = rows.Scan(&rec.Id, &rec.Text)
  676. rec.Recipe_id = recid
  677.  
  678. result = append(result, rec)
  679. }
  680.  
  681. if len(result) == 0 {
  682. iWrt.WriteHeader(http.StatusNotFound)
  683. } else {
  684. outJson, err := json.Marshal(result)
  685.  
  686. if err != nil {
  687. fmt.Println(err)
  688. }
  689.  
  690. iWrt.Write(outJson)
  691. }
  692. }
  693.  
  694. func RecipesGetFiltredHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  695.  
  696. fmt.Println(iReq)
  697.  
  698. keys := iReq.URL.Query()
  699.  
  700. body, err := ioutil.ReadAll(iReq.Body)
  701.  
  702. if err != nil {
  703. fmt.Println(err)
  704. }
  705.  
  706. var data map[string]string
  707. err = json.Unmarshal(body, &data)
  708.  
  709. if err != nil {
  710. fmt.Println(err)
  711. }
  712.  
  713. var result []structs.Frecipe
  714. user := db.QueryRow("select id from "+conf["dbname"]+"."+conf["users"]+" where login = ?", data["login"])
  715. var uid int
  716.  
  717. err = user.Scan(&uid)
  718.  
  719. if err != nil {
  720. fmt.Println(err)
  721. }
  722.  
  723. allergenRecipes, err := db.Query("select ingredient_id from "+conf["dbname"]+"."+conf["allergens"]+" where user_id = ?", uid)
  724.  
  725. if err != nil {
  726. fmt.Println(err)
  727. }
  728.  
  729. var keywords []string
  730.  
  731. for allergenRecipes.Next() {
  732. keyword := ""
  733. var kwid int
  734.  
  735. err = allergenRecipes.Scan(&kwid)
  736.  
  737. if err != nil {
  738. fmt.Println(err)
  739. }
  740.  
  741. allergen := db.QueryRow("select name from "+conf["dbname"]+"."+conf["ingredients"]+" where id = ?", kwid)
  742. err = allergen.Scan(&keyword)
  743.  
  744. if err != nil {
  745. fmt.Println(err)
  746. }
  747.  
  748. keyl := 6
  749.  
  750. fmt.Println(keyl)
  751.  
  752. keyword = keyword[:keyl]
  753.  
  754. fmt.Println("OK")
  755.  
  756. keywords = append(keywords, keyword)
  757. }
  758.  
  759. 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 ?",
  760. keys["fats_min"][0],
  761. keys["fats_max"][0], keys["proteins_min"][0], keys["proteins_max"][0], keys["carbohyd_min"][0], keys["carbohyd_max"][0],
  762. keys["calories_min"][0], keys["calories_max"][0])
  763.  
  764. if err != nil {
  765. fmt.Println("60")
  766. fmt.Println(err)
  767. }
  768.  
  769. for rows.Next() {
  770. rec := structs.Frecipe{}
  771.  
  772. err = rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  773. &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  774. &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  775.  
  776. if err != nil {
  777. fmt.Println(err)
  778. }
  779.  
  780. if strings.Contains(rec.Recipe_title, strings.ToLower(keys["name"][0])) {
  781. if keys["allergens"][0] == "true" {
  782. for i := 0; i < len(keywords); i++ {
  783. fmt.Println(keywords[i])
  784.  
  785. if strings.Contains(strings.ToLower(rec.Recipe_ingredients), strings.ToLower(keywords[i])) {
  786. break
  787. }
  788.  
  789. if (i + 1) == len(keywords) {
  790. result = append(result, rec)
  791. }
  792. }
  793. } else {
  794. result = append(result, rec)
  795. }
  796. }
  797. }
  798.  
  799. if len(result) == 0 {
  800. iWrt.WriteHeader(http.StatusLocked)
  801. } else {
  802. outJson, err := json.Marshal(result)
  803.  
  804. if err != nil {
  805. fmt.Println("86")
  806. fmt.Println(err)
  807. }
  808.  
  809. iWrt.Write(outJson)
  810. }
  811. }
  812.  
  813. func CommentPostHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  814. fmt.Println(iReq)
  815.  
  816. var resp structs.PostForm
  817. vars := mux.Vars(iReq)
  818.  
  819. id, err := strconv.Atoi(vars["id"])
  820.  
  821. if err != nil {
  822. panic(err)
  823. }
  824.  
  825. body, err := ioutil.ReadAll(iReq.Body)
  826.  
  827. if err != nil {
  828. panic(err)
  829. } else {
  830. err = json.Unmarshal(body, &resp)
  831. if err != nil {
  832. panic(err)
  833. }
  834. }
  835.  
  836. _, err = db.Exec("insert into "+conf["dbname"]+"."+conf["comments"]+" (id, text) values (?, ?)", id, resp.Text)
  837.  
  838. if err != nil {
  839. panic(err)
  840. } else {
  841. iWrt.WriteHeader(http.StatusCreated)
  842. }
  843. }
  844.  
  845. func RecipesGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  846. fmt.Println(iReq)
  847.  
  848. var result []structs.Frecipe
  849.  
  850. rows, error := db.Query("select * from " + conf["dbname"] + "." + conf["recipes"])
  851. if error != nil {
  852. panic(error)
  853. }
  854.  
  855. for rows.Next() {
  856. rec := structs.Frecipe{}
  857. err := rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  858. &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  859. &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  860. if err != nil {
  861. panic(err)
  862. }
  863.  
  864. result = append(result, rec)
  865.  
  866. }
  867.  
  868. if len(result) == 0 {
  869. iWrt.WriteHeader(http.StatusNotFound)
  870. } else {
  871. outJson, err := json.Marshal(result)
  872.  
  873. if err != nil {
  874. panic(err)
  875. }
  876.  
  877. iWrt.Write(outJson)
  878. }
  879. }
  880.  
  881. func MainHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  882. fmt.Println(iReq)
  883. iWrt.WriteHeader(http.StatusNotFound)
  884. }
  885.  
  886. func RecipeGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  887. fmt.Println(iReq)
  888. vars := mux.Vars(iReq)
  889.  
  890. recid, err := strconv.Atoi(vars["id"])
  891.  
  892. if err != nil {
  893. fmt.Println(err)
  894. }
  895.  
  896. row := db.QueryRow("select * from "+conf["dbname"]+"."+conf["recipes"]+" where id = ?", recid)
  897.  
  898. rec := structs.Frecipe{}
  899.  
  900. err = row.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  901. &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  902. &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  903.  
  904. if err != nil {
  905. fmt.Println(err)
  906. iWrt.WriteHeader(http.StatusNotFound)
  907. } else {
  908.  
  909. outJson, _ := json.Marshal(rec)
  910. iWrt.Write(outJson)
  911. }
  912. }
  913.  
  914. func CategoriesGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  915. var result []structs.Categ
  916.  
  917. rows, err := db.Query("select * from " + conf["dbname"] + "." + conf["recipes_types"])
  918.  
  919. if err != nil {
  920. fmt.Println(err)
  921. }
  922.  
  923. for rows.Next() {
  924. var categ = structs.Categ{}
  925.  
  926. err = rows.Scan(&categ.Recipe_type, &categ.Type_name, &categ.Type_image)
  927.  
  928. if err != nil {
  929. fmt.Println(err)
  930. }
  931.  
  932. result = append(result, categ)
  933. }
  934.  
  935. if len(result) != 0 {
  936. outJson, _ := json.Marshal(result)
  937. iWrt.Write(outJson)
  938. } else {
  939. iWrt.WriteHeader(http.StatusNotFound)
  940. }
  941. }
  942.  
  943. func CategRecipeGetHandler(iWrt http.ResponseWriter, iReq *http.Request) {
  944. fmt.Println(iReq)
  945. vars := mux.Vars(iReq)
  946.  
  947. category, err := strconv.Atoi(vars["id"])
  948.  
  949. if err != nil {
  950. panic(err)
  951. }
  952.  
  953. var result []structs.Frecipe
  954.  
  955. rows, err := db.Query("select * from "+conf["dbname"]+"."+conf["recipes"]+" where type_id = ?", category)
  956.  
  957. if err != nil {
  958. panic(err)
  959. }
  960.  
  961. for rows.Next() {
  962. rec := structs.Frecipe{}
  963.  
  964. err = rows.Scan(&rec.Recipe_id, &rec.User_id, &rec.Recipe_title, &rec.Time_of_cooking, &rec.Recipe,
  965. &rec.Recipe_image, &rec.Recipe_type, &rec.Calories, &rec.Proteins, &rec.Carbohydrates,
  966. &rec.Portions, &rec.Fats, &rec.Recipe_ingredients)
  967.  
  968. if err != nil {
  969. panic(err)
  970. }
  971.  
  972. result = append(result, rec)
  973. }
  974.  
  975. if len(result) == 0 {
  976. iWrt.WriteHeader(http.StatusNotFound)
  977. } else {
  978. outJson, err := json.Marshal(result)
  979.  
  980. if err != nil {
  981. panic(err)
  982. }
  983.  
  984. iWrt.Write(outJson)
  985. }
  986. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement