Exemple #1
0
func GetUser(w http.ResponseWriter, r *http.Request) (interface{}, *pc.RestError) {
	vars := mux.Vars(r)
	username := vars["username"]

	query := bson.M{"username": username}

	var user pc.User
	if err := userC.Find(query).One(&user); err != nil {
		if err == mgo.ErrNotFound {

			return nil, &pc.RestError{
				WrappedException: err,
				Message:          "No such user",
				HttpStatus:       http.StatusNotFound,
			}
		}

		return nil, &pc.RestError{
			WrappedException: err,
			Message:          "Database error",
			HttpStatus:       http.StatusInternalServerError,
		}
	}

	return user, nil

}
Exemple #2
0
func GetChaptersForUser(w http.ResponseWriter, r *http.Request) (interface{}, *pc.RestError) {
	vars := mux.Vars(r)

	userId := vars["userid"]

	limit, err := strconv.Atoi(vars["limit"])

	if err != nil {
		return nil, &pc.RestError{
			WrappedException: err,
			Message:          "Error parsing limit parameter",
			HttpStatus:       http.StatusBadRequest,
		}
	}

	offset, err := strconv.Atoi(vars["offset"])

	if err != nil {
		return nil, &pc.RestError{
			WrappedException: err,
			Message:          "Error parsing offset parameter",
			HttpStatus:       http.StatusBadRequest,
		}
	}

	query := bson.M{
		"userid":        userId,
		"userpublished": bson.M{"$ne": false},
	}

	iter := chapterC.Find(query).Sort("-startts").Skip(offset).Limit(limit).Iter()

	var results []pc.Chapter

	if err := iter.All(&results); err != nil {
		return nil, &pc.RestError{
			WrappedException: err,
			Message:          "Error parsing database results",
			HttpStatus:       http.StatusInternalServerError,
		}
	}

	return results, nil

}