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.

200 lines
4.7 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/gorilla/mux"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10. type Post struct {
  11. ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
  12. Title string `json:"title"`
  13. Summary string `json:"summary"`
  14. Date time.Time `json:"date"`
  15. ThumbImg string `json:"thumbimg"`
  16. Content string `json:"content"`
  17. User User `json:"user"`
  18. }
  19. type User struct {
  20. ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
  21. Username string `json:"username"`
  22. Name string `json:"name"`
  23. LastName string `json:"lastname"`
  24. Twitter string `json:"twitter" bson:",omitempty"`
  25. Description string `json:"description" bson:",omitempty"`
  26. Email string `json:"email" bson:",omitempty"`
  27. Password string `json:"password" bson:",omitempty"`
  28. Token string `json:"token" bson:",omitempty"`
  29. Posts []Post `json:"posts" bson:",omitempty"`
  30. }
  31. func Index(w http.ResponseWriter, r *http.Request) {
  32. fmt.Fprintln(w, "clientApp")
  33. }
  34. func Signup(w http.ResponseWriter, r *http.Request) {
  35. decoder := json.NewDecoder(r.Body)
  36. var user User
  37. err := decoder.Decode(&user)
  38. if err != nil {
  39. panic(err)
  40. }
  41. defer r.Body.Close()
  42. fmt.Print("user signup: ")
  43. fmt.Println(user.Username)
  44. //save the new project to mongodb
  45. rUser := User{}
  46. err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
  47. if err != nil {
  48. //user not exists
  49. err = userCollection.Insert(user)
  50. err = userCollection.Find(bson.M{"email": user.Email}).One(&user)
  51. } else {
  52. //user exists
  53. http.Error(w, "user already registered", http.StatusConflict)
  54. return
  55. }
  56. fmt.Println(user.Username)
  57. jResp, err := json.Marshal(user)
  58. if err != nil {
  59. panic(err)
  60. }
  61. fmt.Fprintln(w, string(jResp))
  62. }
  63. func Login(w http.ResponseWriter, r *http.Request) {
  64. decoder := json.NewDecoder(r.Body)
  65. var user User
  66. err := decoder.Decode(&user)
  67. if err != nil {
  68. panic(err)
  69. }
  70. defer r.Body.Close()
  71. //TODO check if the user password exists in the database
  72. fmt.Print("user login: ")
  73. fmt.Println(user)
  74. //save the new project to mongodb
  75. rUser := User{}
  76. err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
  77. if err != nil {
  78. http.Error(w, "error login, email not foun", http.StatusConflict)
  79. return
  80. }
  81. //user exists, check password
  82. if user.Password != rUser.Password {
  83. http.Error(w, "error login, password not match", http.StatusConflict)
  84. return
  85. }
  86. token, err := newToken()
  87. check(err)
  88. rUser.Token = token
  89. //update with the token
  90. err = userCollection.Update(bson.M{"_id": rUser.ID}, rUser)
  91. check(err)
  92. jResp, err := json.Marshal(rUser)
  93. if err != nil {
  94. panic(err)
  95. }
  96. fmt.Fprintln(w, string(jResp))
  97. }
  98. func GetUsers(w http.ResponseWriter, r *http.Request) {
  99. //get projects from mongodb
  100. users := []User{}
  101. err := userCollection.Find(bson.M{}).Limit(50).All(&users)
  102. check(err)
  103. jResp, err := json.Marshal(users)
  104. check(err)
  105. fmt.Fprintln(w, string(jResp))
  106. }
  107. func GetUser(w http.ResponseWriter, r *http.Request) {
  108. vars := mux.Vars(r)
  109. userid := vars["userid"]
  110. //get projects from mongodb
  111. user := User{}
  112. err := userCollection.Find(bson.M{"_id": bson.ObjectIdHex(userid)}).One(&user)
  113. check(err)
  114. err = postCollection.Find(bson.M{"user._id": bson.ObjectIdHex(userid)}).Limit(50).All(&user.Posts)
  115. check(err)
  116. jResp, err := json.Marshal(user)
  117. check(err)
  118. fmt.Fprintln(w, string(jResp))
  119. }
  120. func NewPost(w http.ResponseWriter, r *http.Request) {
  121. decoder := json.NewDecoder(r.Body)
  122. var post Post
  123. err := decoder.Decode(&post)
  124. if err != nil {
  125. panic(err)
  126. }
  127. defer r.Body.Close()
  128. //get user by token
  129. usertoken := r.Header.Get("Authorization")
  130. user := User{}
  131. err = userCollection.Find(bson.M{"token": usertoken}).One(&user)
  132. check(err)
  133. //add date to post
  134. post.Date = time.Now()
  135. //add user.id to post.user
  136. post.User.ID = user.ID
  137. err = postCollection.Insert(post)
  138. check(err)
  139. //get user
  140. //user = User{}
  141. err = userCollection.Find(bson.M{"token": usertoken}).One(&user)
  142. check(err)
  143. jResp, err := json.Marshal(user)
  144. if err != nil {
  145. panic(err)
  146. }
  147. fmt.Fprintln(w, string(jResp))
  148. }
  149. func GetPosts(w http.ResponseWriter, r *http.Request) {
  150. //get projects from mongodb
  151. posts := []Post{}
  152. err := postCollection.Find(bson.M{}).Limit(50).All(&posts)
  153. check(err)
  154. jResp, err := json.Marshal(posts)
  155. check(err)
  156. fmt.Fprintln(w, string(jResp))
  157. }
  158. func GetPost(w http.ResponseWriter, r *http.Request) {
  159. vars := mux.Vars(r)
  160. postid := vars["postid"]
  161. //get projects from mongodb
  162. post := Post{}
  163. err := postCollection.Find(bson.M{"_id": bson.ObjectIdHex(postid)}).One(&post)
  164. check(err)
  165. err = userCollection.Find(bson.M{"_id": post.User.ID}).One(&post.User)
  166. check(err)
  167. jResp, err := json.Marshal(post)
  168. check(err)
  169. fmt.Fprintln(w, string(jResp))
  170. }