/* GET: http://localhost:8080/myads/profile // TODO: make it dynamic { status: "OK" data: [ 0:{ title: "test" price: 6468 currency: "SGD" image: "0001_72a53f664db6f415e9e862c607d9c0ba177c20af_655B4C_100_75" } ... ] } */ func Myads(w http.ResponseWriter, r *http.Request) { var profile string lim, sort := conf.ResultLimit, "-_id" // TODO: do not hardcode params := strings.Split(r.URL.Path, "/") if len(params) >= 2 { profile = params[2] //TODO: it should work even Path is not enough } else { w.Write(json.Message3("ERROR", nil, "Wrong URL")) return } p, err := strconv.Atoi(profile) var prof bson.M if err != nil { w.Write(json.Message3("ERROR", nil, "Please, provide profile id")) return } else { prof = bson.M{"profile": p} } session, err := mgo.Dial(conf.Mongodb) if err != nil { log.Fatal("Unable to connect to DB ", err) } defer session.Close() session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior. db := session.DB("sa") var data interface{} var adList []AdList var ad AdList iter := db.C("ad").Find(prof).Limit(lim).Sort(sort).Iter() for iter.Next(&ad) { ad.Image = ad.Image1.Baby // TODO: make it dynamic imgName := strings.Split(ad.Image, "_") ad.Width = imgName[3] ad.Height = imgName[4] ad.Image = conf.IkuraUrl + ad.Image adList = append(adList, ad) } data = adList if err != nil { w.Write(json.Message("ERROR", "Ads not found")) } else { if ad.Image != "" { w.Write(json.Message3("OK", data, "Ads found")) } else { w.Write(json.Message3("ERROR", nil, "Ads not found")) } } log.Printf("err = %s\n", err) }
/* PUT: http://localhost:8080/chat/{ad}/{profile} { status: "OK" "data":"PUTed" } */ func Chat(w http.ResponseWriter, r *http.Request) { var ad, profile string s := strings.Split(r.URL.Path, "/") if len(s) >= 4 { ad, profile = s[2], s[3] } else { w.Write(json.Message3("ERROR", nil, "Wrong URL")) return } session, err := mgo.Dial(conf.Mongodb) if err != nil { log.Fatal("Unable to connect to DB ", err) } defer session.Close() session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior. db := session.DB("sa") err = db.C("ad").Update(bson.M{"_id": bson.ObjectIdHex(ad)}, bson.M{"$addToSet": bson.M{"chat": profile}}) if err != nil { w.Write(json.Message("ERROR", "Could not PUT")) log.Printf("err = %s\n", err) } else { w.Write(json.Message("OK", "PUTed")) } }
/* DELETE: http://localhost:8080/ad/{id}/{token} { status: "OK" "data":"Deleted" } */ func DeleteAd(w http.ResponseWriter, r *http.Request) { var ad, token string s := strings.Split(r.URL.Path, "/") if len(s) >= 4 { ad, token = s[2], s[3] } else { w.Write(json.Message3("ERROR", nil, "Wrong URL")) return } // create a global App var to hold your app id and secret. var globalApp = fb.New("1454662381420609", "9a9d5fd67edc5245f366a1a0fb9bb9bf") // facebook asks for a valid redirect uri when parsing signed request. // it's a new enforced policy starting in late 2013. // it can be omitted in a mobile app server. //globalApp.RedirectUri = "" // here comes a client with a facebook signed request string in query string. // creates a new session with signed request. //session, _ := globalApp.SessionFromSignedRequest(signedRequest) // or, you just get a valid access token in other way. // creates a session directly. session := globalApp.Session(token) // use session to send api request with your access token. //res, _ := session.Get("/me/feed", nil) // validate access token. err is nil if token is valid. err := session.Validate() if err == nil { session, err := mgo.Dial(conf.Mongodb) if err != nil { log.Fatal("Unable to connect to DB ", err) } defer session.Close() session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior. db := session.DB("sa") err = db.C("ad").Remove(bson.M{"_id": bson.ObjectIdHex(ad)}) if err != nil { w.Write(json.Message("ERROR", "Could not DELETE")) log.Printf("err = %s\n", err) } else { w.Write(json.Message("OK", "Deleted")) } } else { w.Write(json.Message("ERROR", "Not valid session")) } }
/* GET: http://localhost:8080/listinig/category/limit/sort { status: "OK" data: [ 0:{ title: "test" price: 6468 currency: "SGD" image: "0001_72a53f664db6f415e9e862c607d9c0ba177c20af_655B4C_100_75" } ... ] } */ func Listing(w http.ResponseWriter, r *http.Request) { var category, limit, sort string sort = "-_id" s := strings.Split(r.URL.Path, "/") if len(s) >= 4 { category, limit, sort = s[2], s[3], s[4] //TODO: it should work even Path is not enough } else { w.Write(json.Message3("ERROR", nil, "Wrong URL")) return } c, err := strconv.Atoi(category) var cat bson.M if err != nil || c == 0 { cat = nil // if not number, then consider All categories } else { cat = bson.M{"category": c} } lim, err := strconv.Atoi(limit) if err != nil { lim = conf.ResultLimit } if sort != "date" { if sort != "price" { w.Write(json.Message3("ERROR", nil, "Wrong URL")) return } } else { sort = "-_id" } session, err := mgo.Dial(conf.Mongodb) if err != nil { log.Fatal("Unable to connect to DB ", err) } defer session.Close() session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior. db := session.DB("sa") var data interface{} var adList []AdList var ad AdList iter := db.C("ad").Find(cat).Limit(lim).Sort(sort).Iter() for iter.Next(&ad) { ad.Image = ad.Image1.Baby // TODO: make it dynamic imgName := strings.Split(ad.Image, "_") ad.Width = imgName[3] ad.Height = imgName[4] ad.Image = conf.IkuraUrl + ad.Image adList = append(adList, ad) } data = adList if err != nil { w.Write(json.Message("ERROR", "Ads not found")) } else { if ad.Image != "" { w.Write(json.Message3("OK", data, "Ads found")) } else { w.Write(json.Message3("ERROR", nil, "Ads not found")) } } log.Printf("err = %s\n", err) }