Example #1
0
// Returns simulations favorited by and comments from the user id passed in the url
func InteractionsHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	userKeyName := vars["userID"]
	ctx := appengine.NewContext(r)
	var simulations []models.SimulationData
	var comments []models.CommentData

	// Check to see if the page user is the same as the logged in user
	userIsOwner := utils.IsOwner(userKeyName, ctx)

	// Only get favorited and commented information if it's the proper user
	// Don't display interaction data to any user except for the user who owns it
	if userIsOwner {
		// Get 50 of the most recent ratings made by the logged in user
		var ratingKeys []*datastore.Key
		var ratingObjs []models.Rating
		q := datastore.NewQuery("Rating").KeysOnly().Filter("AuthorKeyName =", userKeyName).Order("-CreationDate").Limit(50)
		ratingKeys, err := q.GetAll(ctx, ratingObjs)

		// Get the parent keys of the ratings made (these are the keys of the simulations the ratings were for)
		var simulationRateKeys []*datastore.Key
		for _, key := range ratingKeys {
			simulationRateKeys = append(simulationRateKeys, key.Parent())
		}

		// Get all of the simulation objects from the simulation keys
		simulationRateObjs := make([]models.Simulation, len(simulationRateKeys))
		err = datastore.GetMulti(ctx, simulationRateKeys, simulationRateObjs)
		if err != nil {
			controllers.ErrorHandler(w, r, err.Error(), http.StatusInternalServerError)
			return
		}

		// Build the proper simulation data objects from the simulation models
		simulations, err = utils.BuildSimulationDataSlice(ctx, simulationRateObjs, simulationRateKeys)
		if err != nil {
			controllers.ErrorHandler(w, r, "Could not load user simulations: "+err.Error(), http.StatusInternalServerError)
			return
		}

		// Get 50 of the most recent comments made by the logged in user
		q = datastore.NewQuery("Comment").Filter("AuthorKeyName =", userKeyName).Order("-CreationDate").Limit(50)
		comments, err = utils.GetCommentDataSlice(r, q)
		if err != nil {
			controllers.ErrorHandler(w, r, "Error fetching comments: "+err.Error(), http.StatusInternalServerError)
			return
		}
	}

	data := map[string]interface{}{
		"simulations":  simulations,
		"comments":     comments,
		"userIsOwner":  false,
		"userOwnsPage": userIsOwner,
	}

	controllers.BaseHandler(w, r, "user/interactions", data)
}
Example #2
0
// GET returns JSON all comments associated with the simulationID passed in the url
// POST saves the comment to datastore with the simulationID as the ancestor key
func CommentHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	simKeyName := vars["simulationID"]

	ctx := appengine.NewContext(r)
	simulationKey := datastore.NewKey(ctx, "Simulation", simKeyName, 0, nil)

	if r.Method == "GET" {
		q := datastore.NewQuery("Comment").Ancestor(simulationKey).Order("-CreationDate")
		comments, err := utils.GetCommentDataSlice(r, q)

		if err != nil {
			ApiErrorResponse(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// Return comments as json
		json.NewEncoder(w).Encode(comments)
	}

	if r.Method == "POST" {
		formMessage := r.FormValue("Contents")
		if len(formMessage) == 0 || len(formMessage) > 500 {
			ApiErrorResponse(w, "Cannot create empty comments or comments longer than 500 characters", http.StatusBadRequest)
			return
		}

		user, err := utils.GetCurrentUser(ctx)
		if err != nil {
			ApiErrorResponse(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// Get an ID as a simulation descendant
		key, keyName := utils.GenerateUniqueKey(ctx, "Comment", user, simulationKey)

		// Build the comment object
		comment := models.Comment{
			KeyName:       keyName,
			AuthorKeyName: user.KeyName,
			Contents:      formMessage,
			CreationDate:  time.Now(),
		}

		// Put the comment in the datastore
		_, err = datastore.Put(ctx, key, &comment)

		if err != nil {
			ApiErrorResponse(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
}