示例#1
0
func (c *AccountController) Save(res http.ResponseWriter, req *http.Request) {
	a := core.NewAccount()
	util.DecodeReqBody(req.Body, a)
	if err := c.Accounts.Save(a); err == nil {
		util.Send(res, util.Payload{Result: a.ID()}, http.StatusCreated)
	}
}
示例#2
0
func (c *AccountController) Update(res http.ResponseWriter, req *http.Request) {
	a := &core.Account{}
	util.DecodeReqBody(req.Body, a)
	a.Modified = time.Now()
	if err := c.Accounts.Update(a); err == nil {
		util.Send(res, util.Payload{Result: a.ID()}, http.StatusAccepted)
	}
}
示例#3
0
func (c *TopicController) Update(res http.ResponseWriter, req *http.Request) {
	t := &core.Topic{}
	util.DecodeReqBody(req.Body, t)
	t.Modified = time.Now()

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

	util.Send(res, util.Payload{Result: t.ID()}, http.StatusOK)
}
示例#4
0
func (c *TopicController) RemoveParents(res http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	data := map[string][]string{}
	util.DecodeReqBody(req.Body, &data)
	err := c.Topics.RemoveParents(vars["id"], data["parents"])
	if err != nil {
		msg := "Error removing topic parents"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
	}

	util.Send(res, util.Payload{Success: "Removed parents"}, http.StatusOK)
}
示例#5
0
func (c *TopicController) AddParents(res http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	data := map[string][]string{}
	util.DecodeReqBody(req.Body, &data)
	if err := c.Topics.AddParents(vars["id"], data["parentIds"]); err != nil {
		msg := "Could not define parents"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
		return
	}

	util.Send(res, util.Payload{Success: "Saved parents"}, http.StatusCreated)
}
示例#6
0
func (c *PostController) Update(res http.ResponseWriter, req *http.Request) {
	p := &core.Post{}
	util.DecodeReqBody(req.Body, p)
	p.Modified = time.Now()

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

	util.Send(res, util.Payload{Result: p.ID()}, http.StatusOK)
}
示例#7
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)
}
示例#8
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)
}