Exemplo n.º 1
0
// CreateUser is an http endpoint handler
// Path: POST /user
// Param: HTTP Body is a UserParam in JSON format
func CreateUser(w http.ResponseWriter, r *http.Request, c *server.Context) {
	var u UserParam
	if c.MustDecodeBody(w, &u) == false {
		return
	}

	user, err := models.InsertUser(c.DB.DB, u.Name, u.Email, u.Password)
	if err != nil {
		c.Render.BadRequest(w, err)
	} else {
		c.Render.ResultOK(w, user)
	}
}
Exemplo n.º 2
0
func CreateQuestion(w http.ResponseWriter, r *http.Request, c *server.Context) {
	userId := c.Session.UserId
	var q QuestionParam
	if c.MustDecodeBody(w, &q) == false {
		return
	}

	question, err := models.InsertQuestion(c.DB.DB, userId, q.Title, q.Question)
	if err != nil {
		c.Render.BadRequest(w, err)
	} else {
		c.Render.ResultOK(w, question)
	}
}
Exemplo n.º 3
0
func CreateAnswer(w http.ResponseWriter, r *http.Request, c *server.Context) {
	userId := c.Session.UserId
	var a AnswerParam
	if c.MustDecodeBody(w, &a) == false {
		return
	}
	questionId := int(a.QuestionId)

	if models.GetQuestionById(c.DB.DB, questionId) == nil {
		c.Render.BadRequest(w, errors.New("Question does not exist"))
		return
	}

	answer, err := models.InsertAnswer(c.DB.DB, userId, questionId, a.Message)
	if err != nil {
		c.Render.BadRequest(w, err)
	} else {
		c.Render.ResultOK(w, answer)
	}
}
Exemplo n.º 4
0
func RestoreSession(w http.ResponseWriter, r *http.Request, c *server.Context, next server.Handler) {
	session := server.FetchSession(r)
	c.Session = session
	next(w, r, c)
}
Exemplo n.º 5
0
// InitContext is the first middleware to be called. It will initialize the context with
// request-specific data and objects that might be helpful in handling the request
func InitContext(w http.ResponseWriter, r *http.Request, c *server.Context, next server.Handler) {
	c.Decoder = json.NewDecoder(r.Body)
	c.PathParams = mux.Vars(r)
	next(w, r, c)
}