Exemplo n.º 1
0
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)
	})
}
Exemplo n.º 2
0
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)
	})
}
Exemplo n.º 3
0
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)
	})
}
Exemplo n.º 4
0
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)
	})
}
Exemplo n.º 5
0
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)
	})
}
Exemplo n.º 6
0
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)
	})
}
Exemplo n.º 7
0
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)
	})
}
Exemplo n.º 8
0
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)
	})
}
Exemplo n.º 9
0
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)
	})
}
Exemplo n.º 10
0
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)
		}
	})
}
Exemplo n.º 11
0
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)
	})
}
Exemplo n.º 12
0
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)
	})
}