本文整理匯總了Golang中github.com/haystackapp/haystack-api/db.Connection.C方法的典型用法代碼示例。如果您正苦於以下問題:Golang Connection.C方法的具體用法?Golang Connection.C怎麽用?Golang Connection.C使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/haystackapp/haystack-api/db.Connection
的用法示例。
在下文中一共展示了Connection.C方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Login
func Login(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var rU models.RequestUser
err := json.NewDecoder(req.Body).Decode(&rU)
if err != nil {
renderJSON(res, 400, Error{"Error parsing JSON"})
return
}
var user models.User
err = conn.C("users").Find(bson.M{"email": rU.Email}).One(&user)
if err != nil {
renderJSON(res, 401, Error{"User not found"})
return
}
err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(rU.Password))
if err != nil {
renderJSON(res, 401, Error{"Invalid password"})
return
}
renderJSON(res, 200, user)
})
}
示例2: Me
func Me(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var user models.User
err := conn.C("users").Find(bson.M{"accesstoken": req.FormValue("access_token")}).One(&user)
if err != nil {
renderJSON(res, 401, Error{"Bad access token"})
return
}
renderJSON(res, 200, user)
})
}
示例3: Listing
func Listing(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var listing models.Listing
err := conn.C("listings").FindId(bson.ObjectIdHex(mux.Vars(req)["id"])).One(&listing)
if err != nil {
renderJSON(res, 404, Error{"Listing not found"})
return
}
renderJSON(res, 200, listing)
})
}
示例4: Rental
func Rental(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var rental models.Rental
err := conn.C("rentals").FindId(bson.ObjectIdHex(mux.Vars(req)["id"])).One(&rental)
if err != nil {
renderJSON(res, 404, Error{"Rental not found"})
return
}
renderJSON(res, 200, rental)
})
}
示例5: ConfirmRental
func ConfirmRental(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var seller models.User
err := conn.C("users").Find(bson.M{"accesstoken": req.FormValue("access_token")}).One(&seller)
if err != nil {
renderJSON(res, 401, Error{"Bad access token"})
return
}
var rental models.Rental
err = conn.C("rentals").FindId(bson.ObjectIdHex(mux.Vars(req)["id"])).One(&rental)
if err != nil {
renderJSON(res, 404, Error{"Rental not found"})
return
}
var listing models.Listing
err = conn.C("listings").FindId(rental.ListingId).One(&listing)
if err != nil {
renderJSON(res, 404, Error{"Listing not found"})
return
}
if seller.Id == rental.SellerId {
var buyer models.User
err = conn.C("users").FindId(rental.BuyerId).One(&buyer)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
confirmed, err := models.ConfirmRental(&buyer, &seller, &listing, &rental)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
err = conn.C("rentals").UpdateId(rental.Id, confirmed)
if err != nil {
renderJSON(res, 500, Error{"Error confirming rental"})
return
}
} else {
renderJSON(res, 401, Error{"Listing not owned by user"})
return
}
renderJSON(res, 204, nil)
})
}
示例6: Register
func Register(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
user, err := models.NewUser(req.Body)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
err = conn.C("users").Insert(user)
if err != nil {
renderJSON(res, 500, Error{"Error saving user"})
return
}
renderJSON(res, 201, user)
})
}
示例7: Rent
func Rent(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var buyer models.User
err := conn.C("users").Find(bson.M{"accesstoken": req.FormValue("access_token")}).One(&buyer)
if err != nil {
renderJSON(res, 401, Error{"Bad access token"})
return
}
var listing models.Listing
err = conn.C("listings").FindId(bson.ObjectIdHex(mux.Vars(req)["id"])).One(&listing)
if err != nil {
renderJSON(res, 404, Error{"Listing not found"})
return
}
var seller models.User
err = conn.C("users").FindId(listing.UserId).One(&seller)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
rental, err := models.NewRental(&buyer, &seller, &listing, req.Body)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
err = conn.C("rentals").Insert(rental)
if err != nil {
renderJSON(res, 400, Error{"Error saving rental"})
return
}
pending, err := conn.C("rentals").Find(bson.M{"sellerid": seller.Id, "status": "pending"}).Count()
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
go sendNotif(&listing, &seller, pending)
renderJSON(res, 201, rental)
})
}
示例8: Listings
func Listings(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if accessToken := req.FormValue("access_token"); accessToken != "" {
var user models.User
err := conn.C("users").Find(bson.M{"accesstoken": accessToken}).One(&user)
if err != nil {
renderJSON(res, 401, Error{"Bad access token"})
return
}
listings := []models.Listing{}
err = conn.C("listings").Find(bson.M{"userid": user.Id}).All(&listings)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
renderJSON(res, 200, listings)
} else {
lat, err := strconv.ParseFloat(req.FormValue("lat"), 64)
if err != nil {
renderJSON(res, 400, Error{err.Error()})
return
}
lng, err := strconv.ParseFloat(req.FormValue("lng"), 64)
if err != nil {
renderJSON(res, 400, Error{err.Error()})
return
}
radius, err := strconv.ParseFloat(req.FormValue("radius"), 64)
if err != nil {
renderJSON(res, 400, Error{err.Error()})
return
}
listings, err := conn.FindListingsByLocation(lat, lng, radius)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
renderJSON(res, 200, listings)
}
})
}
示例9: Rentals
func Rentals(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var user models.User
err := conn.C("users").Find(bson.M{"accesstoken": req.FormValue("access_token")}).One(&user)
if err != nil {
renderJSON(res, 401, Error{"Bad access token"})
return
}
rentals := []models.Rental{}
if status := req.FormValue("status"); status != "" {
err = conn.C("rentals").Find(bson.M{"sellerid": user.Id, "status": status}).All(&rentals)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
} else {
err = conn.C("rentals").Find(bson.M{"buyerid": user.Id}).All(&rentals)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
}
renderJSON(res, 200, rentals)
})
}
示例10: DenyRental
func DenyRental(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var seller models.User
err := conn.C("users").Find(bson.M{"accesstoken": req.FormValue("access_token")}).One(&seller)
if err != nil {
renderJSON(res, 401, Error{"Bad access token"})
return
}
var rental models.Rental
err = conn.C("rentals").FindId(bson.ObjectIdHex(mux.Vars(req)["id"])).One(&rental)
if err != nil {
renderJSON(res, 404, Error{"Rental not found"})
return
}
if seller.Id == rental.SellerId {
rental.Status = "denied"
err = conn.C("rentals").UpdateId(rental.Id, rental)
if err != nil {
renderJSON(res, 500, Error{"Error denying rental"})
return
}
} else {
renderJSON(res, 401, Error{"Listing not owned by user"})
return
}
renderJSON(res, 204, nil)
})
}
示例11: CreateListing
func CreateListing(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var user models.User
err := conn.C("users").Find(bson.M{"accesstoken": req.FormValue("access_token")}).One(&user)
if err != nil {
renderJSON(res, 401, Error{"Bad access token"})
return
}
listing, err := models.NewListing(&user, req.Body)
if err != nil {
renderJSON(res, 400, Error{err.Error()})
return
}
err = conn.C("listings").EnsureIndex(mgo.Index{Key: []string{"$2dsphere:location.geometry"}, Bits: 26})
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
err = conn.C("listings").Insert(listing)
if err != nil {
renderJSON(res, 500, Error{"Error saving listing"})
return
}
esConn := elastigo.NewConn()
err = esConn.PutMapping("haystack", "listing", models.Listing{}, elastigo.MappingOptions{})
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
_, err = esConn.Index("haystack", "listing", "", nil, listing)
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
renderJSON(res, 201, listing)
})
}
示例12: RegisterDevice
func RegisterDevice(conn *db.Connection) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var user models.User
err := conn.C("users").Find(bson.M{"accesstoken": req.FormValue("access_token")}).One(&user)
if err != nil {
renderJSON(res, 401, Error{"Bad access token"})
return
}
var d models.Device
err = json.NewDecoder(req.Body).Decode(&d)
if err != nil {
renderJSON(res, 400, Error{"Error parsing JSON"})
return
}
count, err := conn.C("users").Find(bson.M{"devices": d.Device}).Count()
if err != nil {
renderJSON(res, 500, Error{err.Error()})
return
}
if count == 0 {
err = conn.C("users").UpdateId(user.Id, bson.M{"$push": bson.M{"devices": d.Device}})
if err != nil {
renderJSON(res, 500, Error{"Error registering device"})
return
}
} else {
renderJSON(res, 400, Error{"Device already registered"})
return
}
renderJSON(res, 204, nil)
})
}