当前位置: 首页>>代码示例>>Golang>>正文


Golang db.Connection类代码示例

本文整理汇总了Golang中github.com/haystackapp/haystack-api/db.Connection的典型用法代码示例。如果您正苦于以下问题:Golang Connection类的具体用法?Golang Connection怎么用?Golang Connection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Connection类的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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:25,代码来源:user.go

示例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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:12,代码来源:user.go

示例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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:12,代码来源:listing.go

示例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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:12,代码来源:rental.go

示例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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:50,代码来源:rental.go

示例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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:17,代码来源:user.go

示例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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:46,代码来源:listing.go

示例8: 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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:28,代码来源:rental.go

示例9: 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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:31,代码来源:rental.go

示例10: 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)
		}
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:45,代码来源:listing.go

示例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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:42,代码来源:listing.go

示例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)
	})
}
开发者ID:haystackapp,项目名称:haystack-api,代码行数:36,代码来源:user.go


注:本文中的github.com/haystackapp/haystack-api/db.Connection类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。