You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
1.9 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type Routes []Route
  8. var routes = Routes{
  9. Route{
  10. "Index",
  11. "GET",
  12. "/",
  13. Index,
  14. },
  15. /* Route{
  16. "Recommendations",
  17. "GET",
  18. "/r/{userid}/{nrec}",
  19. Recommendations,
  20. },
  21. */
  22. Route{
  23. "NetworkMap",
  24. "Get",
  25. "/map",
  26. NetworkMap,
  27. },
  28. /*
  29. Route{
  30. "SelectItem",
  31. "GET",
  32. "/selectItem/{userid}/{itemid}",
  33. SelectItem,
  34. },
  35. */
  36. }
  37. //ROUTES
  38. func Index(w http.ResponseWriter, r *http.Request) {
  39. fmt.Fprintln(w, "ask for recommendations in /r")
  40. //http.FileServer(http.Dir("./web"))
  41. }
  42. /*
  43. func NewUser(w http.ResponseWriter, r *http.Request) {
  44. ipFilter(w, r)
  45. decoder := json.NewDecoder(r.Body)
  46. var newUser UserModel
  47. err := decoder.Decode(&newUser)
  48. check(err)
  49. defer r.Body.Close()
  50. saveUser(userCollection, newUser)
  51. fmt.Println(newUser)
  52. fmt.Fprintln(w, "new user added: ", newUser.ID)
  53. }
  54. */
  55. func NetworkMap(w http.ResponseWriter, r *http.Request) {
  56. ipFilter(w, r)
  57. nodes, err := getAllNodes()
  58. check(err)
  59. edges, err := getAllEdges()
  60. check(err)
  61. var network NetworkModel
  62. network.Nodes = nodes
  63. network.Edges = edges
  64. //convert []resp struct to json
  65. jNetwork, err := json.Marshal(network)
  66. check(err)
  67. fmt.Fprintln(w, string(jNetwork))
  68. }
  69. /*
  70. func SelectItem(w http.ResponseWriter, r *http.Request) {
  71. ipFilter(w, r)
  72. vars := mux.Vars(r)
  73. userid := vars["userid"]
  74. itemid := vars["itemid"]
  75. //find item
  76. item, err := getItemById(itemid)
  77. if err != nil {
  78. fmt.Fprintln(w, "item "+itemid+" not found")
  79. }
  80. //find user
  81. user, err := getUserById(userid)
  82. if err != nil {
  83. fmt.Fprintln(w, "user "+userid+" not found")
  84. }
  85. //increase TActed in item
  86. item.TActed = item.TActed + 1
  87. //save item
  88. item, err = updateItem(item)
  89. check(err)
  90. fmt.Println(item)
  91. //add item to []Actions of user
  92. user.Actions = append(user.Actions, itemid)
  93. //save user
  94. user, err = updateUser(user)
  95. check(err)
  96. fmt.Println(user)
  97. fmt.Fprintln(w, "user: "+userid+", selects item: "+itemid)
  98. }
  99. */