示例#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)
	}
}
示例#2
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)
	}

}
示例#3
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)
	}
}
示例#4
0
func ServeSubmitQuestion(store datastores.QuestionStoreServices) m.HandlerFunc {
	return func(c *m.Context, w http.ResponseWriter, r *http.Request) {

		newQuestion := c.ParsedModel.(*models.Question)
		category := mux.Vars(r)["category"]

		err := c.RepStore.UpdateRep(category, c.UserID, QUESTION_ASKING_FEE)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
		}

		err, statusCode := store.StoreQuestion(newQuestion.UserID, category, newQuestion.Title, newQuestion.Content)
		if err != nil {
			http.Error(w, err.Error(), statusCode)
			return
		}

		w.WriteHeader(http.StatusCreated)

	}
}