Пример #1
0
// userId in this case is passengerId because this is for a user removing themself from the reservation list
// or messenging the driver of the ride.
func CheckReservePost(db *sql.DB, userId int, r *http.Request, listingId int) (string, error) {
	if r.FormValue("r") != "" {
		// Handle deleting this reservation
		driverId, err := strconv.Atoi(r.FormValue("r"))
		if err != nil {
			return "", util.NewError(nil, "Usuario invalido", 400)
		}

		// Check if reservation is in the past
		listing, err := ReturnIndividualListing(db, listingId)
		if err != nil {
			return "", err
		}

		pastTime, err := util.TimeStringInPast(listing.Timestamp)
		if err != nil {
			return "", err
		}

		if pastTime {
			return "", util.NewError(nil, "No se pueden modificar reservaciones pasadas", 400)
		}

		seats, err := findSeats(db, listingId, userId)
		if err != nil {
			return "", err
		}
		deleted, err := deleteFromReservations(db, driverId, listingId, userId)
		if err != nil {
			return "", err
		}
		if deleted {
			err = updateSeats(db, driverId, listingId, seats)
			if err != nil {
				return "", err
			}
			err = CreateAlert(db, driverId, "dropped", listingId)
			if err != nil {
				return "", err
			}
		}
		return "https://5sur.com/dashboard/reservations", nil
	}
	if r.FormValue("m") != "" {
		// We are messenging the user with id equal to the post request data.
		return "https://5sur.com/dashboard/messages", nil
	}
	return "", nil
}
Пример #2
0
func DeleteListing(db *sql.DB, userId int, listingId int) ([]RegisteredUser, error) {
	registeredUsers, err := getRegisteredUsers(db, listingId)
	if err != nil {
		return registeredUsers, err
	}

	// Check if listing is in the past
	listing, err := ReturnIndividualListing(db, listingId)
	if err != nil {
		return registeredUsers, err
	}

	pastTime, err := util.TimeStringInPast(listing.Timestamp)
	if err != nil {
		return registeredUsers, err
	}

	if pastTime {
		return registeredUsers, util.NewError(nil, "No se pueden modificar viajes realizados", 400)
	}

	stmt, err := db.Prepare(`
		DELETE l, r, rq
			FROM listings AS l
			LEFT JOIN reservations AS r ON r.listing_id = l.id
			LEFT JOIN reservation_queue AS rq ON rq.listing_id = l.id
			WHERE l.driver = ?
				AND l.id = ?
	`)
	if err != nil {
		return registeredUsers, util.NewError(err, "Error de la base de datos", 500)
	}
	defer stmt.Close()

	// db.Query() prepares, executes, and closes a prepared statement - three round
	// trips to the databse. Call it infrequently as possible; use efficient SQL statments
	_, err = stmt.Exec(userId, listingId)
	if err != nil {
		return registeredUsers, util.NewError(err, "Error de la base de datos", 500)
	}
	return registeredUsers, nil
}
Пример #3
0
func CheckPost(db *sql.DB, userId int, r *http.Request, listingId int) error {
	if r.FormValue("a") != "" {
		passengerId, err := strconv.Atoi(r.FormValue("a"))
		if err != nil {
			return util.NewError(nil, "Pasajero invalido", 400)
		}

		// Check if listing is in the past
		listing, err := ReturnIndividualListing(db, listingId)
		if err != nil {
			return err
		}

		pastTime, err := util.TimeStringInPast(listing.Timestamp)
		if err != nil {
			return err
		}

		if pastTime {
			return util.NewError(nil, "No se pueden modificar viajes realizados", 400)
		}

		pendingUser, err := getPendingUser(db, listingId, passengerId)
		if err != nil {
			return err
		}

		if pendingUser.Seats > listing.Seats {
			return util.NewError(nil, "No hay cupos", 400)
		}

		deleted, err := deleteFromQueue(db, userId, listingId, passengerId)
		if err != nil {
			return err
		}
		if deleted {
			err := addToReservation(db, userId, listingId, passengerId, pendingUser.Seats)
			if err != nil {
				return err
			}
			err = updateSeats(db, userId, listingId, (pendingUser.Seats * -1))
			if err != nil {
				return err
			}
			pendingUsers, err := getPendingUsers(db, listingId)
			if err != nil {
				return err
			}
			if len(pendingUsers) == 0 {
				err = DeleteAlert(db, userId, "pending", listingId)
				if err != nil {
					return err
				}
			}
			err = CreateAlert(db, passengerId, "accepted", listingId)
			if err != nil {
				return err
			}
		}
		return nil
	}
	if r.FormValue("r") != "" {
		passengerId, err := strconv.Atoi(r.FormValue("r"))
		if err != nil {
			return util.NewError(nil, "Pasajero invalido", 400)
		}

		// Check if listing is in the past
		listing, err := ReturnIndividualListing(db, listingId)
		if err != nil {
			return err
		}

		pastTime, err := util.TimeStringInPast(listing.Timestamp)
		if err != nil {
			return err
		}

		if pastTime {
			return util.NewError(nil, "No se pueden modificar viajes realizados", 400)
		}

		deleted, err := deleteFromQueue(db, userId, listingId, passengerId)
		if err != nil {
			return err
		}
		if deleted == false {
			seats, err := findSeats(db, listingId, passengerId)
			if err != nil {
				return err
			}
			err = DeleteAlert(db, passengerId, "accepted", listingId)
			if err != nil {
				return err
			}
			_, err = deleteFromReservations(db, userId, listingId, passengerId)
			if err != nil {
				return err
			}
			err = updateSeats(db, userId, listingId, seats)
			if err != nil {
				return err
			}
			err = CreateAlert(db, passengerId, "removed", listingId)
			if err != nil {
				return err
			}
		} else {
			pendingUsers, err := getPendingUsers(db, listingId)
			if err != nil {
				return err
			}
			if len(pendingUsers) == 0 {
				err = DeleteAlert(db, userId, "pending", listingId)
				if err != nil {
					return err
				}
			}
		}
		return nil
	}
	if r.FormValue("m") != "" {
		_, err := strconv.Atoi(r.FormValue("m"))
		if err != nil {
			return util.NewError(nil, "Pasajero invalido", 400)
		}
		// Deal with messenging
	}
	return nil
}