|
|
@ -4,16 +4,32 @@ import ( |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"net/http" |
|
|
|
"time" |
|
|
|
|
|
|
|
"github.com/gorilla/mux" |
|
|
|
"gopkg.in/mgo.v2/bson" |
|
|
|
) |
|
|
|
|
|
|
|
type Post struct { |
|
|
|
ID bson.ObjectId `json:"id" bson:"_id,omitempty"` |
|
|
|
Title string `json:"title"` |
|
|
|
Summary string `json:"summary"` |
|
|
|
Date time.Time `json:"date"` |
|
|
|
ThumbImg string `json:"thumbimg"` |
|
|
|
Content string `json:"content"` |
|
|
|
User User `json:"user"` |
|
|
|
} |
|
|
|
type User struct { |
|
|
|
Id bson.ObjectId `json:"id" bson:"_id,omitempty"` |
|
|
|
Username string `json:"username"` |
|
|
|
Email string `json:"email"` |
|
|
|
Password string `json:"password"` |
|
|
|
Token string `json:"token"` |
|
|
|
ID bson.ObjectId `json:"id" bson:"_id,omitempty"` |
|
|
|
Username string `json:"username"` |
|
|
|
Name string `json:"name"` |
|
|
|
LastName string `json:"lastname"` |
|
|
|
Twitter string `json:"twitter" bson:",omitempty"` |
|
|
|
Description string `json:"description" bson:",omitempty"` |
|
|
|
Email string `json:"email" bson:",omitempty"` |
|
|
|
Password string `json:"password" bson:",omitempty"` |
|
|
|
Token string `json:"token" bson:",omitempty"` |
|
|
|
Posts []Post `json:"posts" bson:",omitempty"` |
|
|
|
} |
|
|
|
|
|
|
|
func Index(w http.ResponseWriter, r *http.Request) { |
|
|
@ -85,7 +101,7 @@ func Login(w http.ResponseWriter, r *http.Request) { |
|
|
|
rUser.Token = token |
|
|
|
|
|
|
|
//update with the token
|
|
|
|
err = userCollection.Update(bson.M{"_id": rUser.Id}, rUser) |
|
|
|
err = userCollection.Update(bson.M{"_id": rUser.ID}, rUser) |
|
|
|
check(err) |
|
|
|
|
|
|
|
jResp, err := json.Marshal(rUser) |
|
|
@ -94,3 +110,91 @@ func Login(w http.ResponseWriter, r *http.Request) { |
|
|
|
} |
|
|
|
fmt.Fprintln(w, string(jResp)) |
|
|
|
} |
|
|
|
|
|
|
|
func GetUsers(w http.ResponseWriter, r *http.Request) { |
|
|
|
//get projects from mongodb
|
|
|
|
users := []User{} |
|
|
|
err := userCollection.Find(bson.M{}).Limit(50).All(&users) |
|
|
|
check(err) |
|
|
|
|
|
|
|
jResp, err := json.Marshal(users) |
|
|
|
check(err) |
|
|
|
fmt.Fprintln(w, string(jResp)) |
|
|
|
} |
|
|
|
func GetUser(w http.ResponseWriter, r *http.Request) { |
|
|
|
vars := mux.Vars(r) |
|
|
|
userid := vars["userid"] |
|
|
|
|
|
|
|
//get projects from mongodb
|
|
|
|
user := User{} |
|
|
|
err := userCollection.Find(bson.M{"_id": bson.ObjectIdHex(userid)}).One(&user) |
|
|
|
check(err) |
|
|
|
|
|
|
|
err = postCollection.Find(bson.M{"user._id": bson.ObjectIdHex(userid)}).Limit(50).All(&user.Posts) |
|
|
|
check(err) |
|
|
|
|
|
|
|
jResp, err := json.Marshal(user) |
|
|
|
check(err) |
|
|
|
fmt.Fprintln(w, string(jResp)) |
|
|
|
} |
|
|
|
func NewPost(w http.ResponseWriter, r *http.Request) { |
|
|
|
decoder := json.NewDecoder(r.Body) |
|
|
|
var post Post |
|
|
|
err := decoder.Decode(&post) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
defer r.Body.Close() |
|
|
|
|
|
|
|
//get user by token
|
|
|
|
usertoken := r.Header.Get("Authorization") |
|
|
|
user := User{} |
|
|
|
err = userCollection.Find(bson.M{"token": usertoken}).One(&user) |
|
|
|
check(err) |
|
|
|
|
|
|
|
//add date to post
|
|
|
|
post.Date = time.Now() |
|
|
|
|
|
|
|
//add user.id to post.user
|
|
|
|
post.User.ID = user.ID |
|
|
|
err = postCollection.Insert(post) |
|
|
|
check(err) |
|
|
|
|
|
|
|
//get user
|
|
|
|
//user = User{}
|
|
|
|
err = userCollection.Find(bson.M{"token": usertoken}).One(&user) |
|
|
|
check(err) |
|
|
|
|
|
|
|
jResp, err := json.Marshal(user) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
fmt.Fprintln(w, string(jResp)) |
|
|
|
} |
|
|
|
|
|
|
|
func GetPosts(w http.ResponseWriter, r *http.Request) { |
|
|
|
//get projects from mongodb
|
|
|
|
posts := []Post{} |
|
|
|
err := postCollection.Find(bson.M{}).Limit(50).All(&posts) |
|
|
|
check(err) |
|
|
|
|
|
|
|
jResp, err := json.Marshal(posts) |
|
|
|
check(err) |
|
|
|
fmt.Fprintln(w, string(jResp)) |
|
|
|
} |
|
|
|
func GetPost(w http.ResponseWriter, r *http.Request) { |
|
|
|
vars := mux.Vars(r) |
|
|
|
postid := vars["postid"] |
|
|
|
|
|
|
|
//get projects from mongodb
|
|
|
|
post := Post{} |
|
|
|
err := postCollection.Find(bson.M{"_id": bson.ObjectIdHex(postid)}).One(&post) |
|
|
|
check(err) |
|
|
|
|
|
|
|
err = userCollection.Find(bson.M{"_id": post.User.ID}).One(&post.User) |
|
|
|
check(err) |
|
|
|
|
|
|
|
jResp, err := json.Marshal(post) |
|
|
|
check(err) |
|
|
|
fmt.Fprintln(w, string(jResp)) |
|
|
|
} |