Exemple #1
0
// parseIdError returns a not found ID error when aplicable;
// otherwise returns a internal server error.
func parseIdError(id string, err error) rqhttp.JsonError {
	var jerr rqhttp.JsonError
	if err == mgo.ErrNotFound {
		jerr = rqhttp.NewJsonErrorFromError(
			http.StatusNotFound, NotFoundId(id))
	} else {
		jerr = rqhttp.NewJsonErrorFromError(
			http.StatusInternalServerError, err)
	}

	return jerr
}
Exemple #2
0
func (s *AccountsController) GetAccount(c *gin.Context) {
	sMajor := c.Param("major")
	iMajor, err := strconv.Atoi(sMajor)
	if err != nil || iMajor < 1 || iMajor > 999 {
		jerr := rqhttp.NewJsonErrorFromError(
			http.StatusGone, InvalidId(sMajor))
		c.JSON(jerr.Status, jerr)
		return
	}

	account := models.Account{}

	// db.accounts.find({_id: major})
	err = s.col.
		Find(bson.M{"_id": iMajor}).
		Select(bson.M{"minors": 0}).
		One(&account)
	if err != nil {
		jerr := parseIdError(sMajor, err)
		c.JSON(jerr.Status, jerr)
		return
	}

	c.JSON(http.StatusOK, account)
}
Exemple #3
0
func (s *AccountsController) GetAccountList(c *gin.Context) {
	level, err := s.getQueryLevel(c)
	if err != nil {
		jerr := rqhttp.NewJsonErrorFromError(http.StatusGone, err)
		c.JSON(jerr.Status, jerr)
		return
	}

	var filter bson.M
	switch level {
	case -1:
		filter = nil
	case 1:
		// db.accounts.find({_id: { $lte: 9 } })
		filter = bson.M{"_id": bson.M{"$lte": 9}}
	case 2:
		// db.accounts.find({_id: { $gte: 10, $lte: 99 } })
		filter = bson.M{"_id": bson.M{"$gte": 10, "$lte": 99}}
	case 3:
		// db.accounts.find({_id: { $gte: 100, $lte: 999 } })
		filter = bson.M{"_id": bson.M{"$gte": 100, "$lte": 999}}
	}

	list := make([]models.Account, 0)
	err = s.col.
		Find(filter).
		Select(bson.M{"minors": 0}).
		All(&list)
	if err != nil {
		jerr := rqhttp.NewJsonErrorFromError(
			http.StatusInternalServerError, err)
		c.JSON(jerr.Status, jerr)
		return
	}

	c.JSON(http.StatusOK, list)
}