Example #1
0
func ServeSortedQuestions(store datastores.QuestionStoreServices) m.HandlerFunc {
	return func(c *m.Context, w http.ResponseWriter, r *http.Request) {
		routeVars := mux.Vars(r)
		questions, err, statusCode := store.SortQuestions(routeVars["postComponent"], routeVars["sortedBy"], routeVars["order"], routeVars["offset"])
		if err != nil {
			http.Error(w, err.Error(), statusCode)
			return
		}
		services.PrintJSON(w, questions)
	}
}
Example #2
0
func ServeFindUser(store datastores.UserStoreServices) m.HandlerFunc {
	return func(c *m.Context, w http.ResponseWriter, r *http.Request) {

		user, err, statusCode := store.FindUser(mux.Vars(r)["filter"], mux.Vars(r)["searchVal"])
		if err != nil {
			http.Error(w, err.Error(), statusCode)
			return
		}
		services.PrintJSON(w, user)
	}
}
Example #3
0
func ServeQuestionsByFilter(store datastores.QuestionStoreServices) m.HandlerFunc {
	return func(c *m.Context, w http.ResponseWriter, r *http.Request) {

		questions, err, statusCode := store.FindQuestionsByFilter(mux.Vars(r)["filter"], mux.Vars(r)["val"])
		if err != nil {
			http.Error(w, err.Error(), statusCode)
			return
		}
		services.PrintJSON(w, questions)
	}

}
Example #4
0
func ServePostByID(store datastores.QuestionStoreServices) m.HandlerFunc {
	return func(c *m.Context, w http.ResponseWriter, r *http.Request) {
		var post []models.ModelServices
		question, answer, err, statusCode := store.FindPostByID(mux.Vars(r)["questionId"])
		if err != nil {
			http.Error(w, err.Error(), statusCode)
			return
		}

		post = append(post, question)
		if answer != nil {
			post = append(post, answer)
		}
		services.PrintJSON(w, post)
	}
}
Example #5
0
func RefreshExpiringToken(fn HandlerFunc) HandlerFunc {
	return func(c *Context, w http.ResponseWriter, r *http.Request) {

		//Refreshes token, if the token expires in less than 24 hours
		if (c.Exp != time.Time{}) && (c.Exp.Sub(time.Now()) < (time.Duration(24) * time.Hour)) {

			refreshedToken, err := c.RefreshToken()
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			services.PrintJSON(w, refreshedToken)
		}
		fn(c, w, r)
	}
}
Example #6
0
func ServeLogin(store datastores.UserStoreServices) m.HandlerFunc {
	return func(c *m.Context, w http.ResponseWriter, r *http.Request) {

		credentials := c.ParsedModel.(*models.UnauthUser)

		retrievedUser, err, statusCode := store.FindUser("username", credentials.Username)
		if err != nil {
			http.Error(w, err.Error(), statusCode)
			return
		}

		c.UserID = retrievedUser.ID
		token, err := c.Login(credentials.Password, retrievedUser.HashedPassword)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		services.PrintJSON(w, token)
	}
}