예제 #1
0
func (c *TopicController) Save(res http.ResponseWriter, req *http.Request) {
	t := core.NewTopic()
	util.DecodeReqBody(req.Body, t)

	v := validate.NewValidator()
	v.NotEmptyString(t.Name)
	v.NoSpaces(t.Name)
	if v.NotValid() {
		msg := "Invalid topic name"
		util.LogError(msg, v)
		util.SendError(res, msg, http.StatusBadRequest)
		return
	}

	if err := c.Topics.Save(t); err != nil {
		msg := "Could not save topic"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
		return
	}

	util.Send(res, util.Payload{Result: t.ID()}, http.StatusCreated)
}
예제 #2
0
func (c *PostController) Save(res http.ResponseWriter, req *http.Request) {
	p := core.NewPost()
	util.DecodeReqBody(req.Body, p)

	v := validate.NewValidator()
	v.NotEmptyString(p.Title)
	v.NoSpaces(p.Title)
	if v.NotValid() {
		msg := "Invalid post name"
		util.LogError(msg, v)
		util.SendError(res, msg, http.StatusBadRequest)
		return
	}

	if err := c.Posts.Save(p); err == nil {
		msg := "Could not save post"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
		return
	}

	util.Send(res, util.Payload{Result: p.ID()}, http.StatusCreated)
}