示例#1
0
func (c *TopicController) All(res http.ResponseWriter, req *http.Request) {
	topics, err := c.Topics.All(0, 15)
	if err != nil {
		msg := "Could not retrieve topics"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
		return
	}

	util.Send(res, util.Payload{Result: topics}, http.StatusOK)
}
示例#2
0
func (c *TopicController) List(res http.ResponseWriter, req *http.Request) {
	list, err := c.Topics.List(0, 1000)
	if err != nil {
		msg := "Could not retrieve list of topics"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
		return
	}

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

	if err := c.Topics.Purge(vars["id"]); err != nil {
		msg := "Unable to delete topic"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
		return
	}

	util.Send(res, util.Payload{Success: "Deleted topic"}, http.StatusOK)
}
示例#4
0
func (c *PostController) Purge(res http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	if err := c.Posts.Purge(vars["id"]); err != nil {
		msg := "Could not purge post"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
		return
	}

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

	topic, err := c.Topics.One(vars["id"])
	if err != nil {
		msg := "Could not retrieve topic"
		util.LogError(msg, err)
		util.SendError(res, msg, http.StatusInternalServerError)
		return
	}

	util.Send(res, util.Payload{Result: topic}, http.StatusFound)
}
示例#6
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)
}
示例#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 *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)
}
示例#9
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)
}
示例#10
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)
}
示例#11
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)
}