Exemple #1
0
// Handler for get stats
func GetStatsHandler(response http.ResponseWriter, request *http.Request, user_id int) string {
	stats := models.GetUserStats(user_id)

	if json_stats, err := json.Marshal(stats); err != nil {
		return "error"
	} else {
		return string(json_stats)
	}
}
Exemple #2
0
// Ajax handlers for logged in users, functions return a string that's returned to the client
// Saves a user's selected words
func SaveConfigHandler(response http.ResponseWriter, request *http.Request, user_id int) string {
	// POST params are fetched
	type_id := request.FormValue("type")
	enabled := request.FormValue("enabled")

	if enabled == "false" {
		models.DisableUsersWordType(user_id, type_id)
	} else {
		// Fetch word_ids
		word_ids := ""
		is_first := true
		for _, word_id := range models.GetWordIdsByType(type_id) {
			if is_first {
				word_ids += word_id
				is_first = false
			} else {
				word_ids += "," + word_id
			}
		}

		// Fetch ids of users_words that are not in the db for the user and insert them
		missing_user_words := models.GetMissingUserWordsForUser(word_ids, user_id)
		user_word := models.NewUserWord()
		user_word.Fields = make(map[string]string)
		user_word.Fields["user_id"] = strconv.Itoa(user_id)
		for _, word_id := range missing_user_words {
			user_word.Fields["word_id"] = word_id
			user_word.Insert()
		}

		// Enable selected type for user
		models.EnableUsersWordType(user_id, type_id)
	}

	stats := models.GetUserStats(user_id)
	if json_stats, err := json.Marshal(stats); err != nil {
		return "error"
	} else {
		return string(json_stats)
	}
}