Beispiel #1
0
func (c *AccountController) Purge(res http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	if err := c.Accounts.Purge(vars["id"]); err == nil {
		util.Send(res, util.Payload{Success: "Deleted " + vars["id"]}, http.StatusOK)
	}
}
Beispiel #2
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)
	}
}
Beispiel #3
0
func (c *AccountController) One(res http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	if account, err := c.Accounts.One(vars["id"]); err == nil {
		util.Send(res, util.Payload{Result: account}, http.StatusOK)
	}
}
Beispiel #4
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)
	}
}
Beispiel #5
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)
}
Beispiel #6
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)
}
Beispiel #7
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)
}
Beispiel #8
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)
}
Beispiel #9
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)
}
Beispiel #10
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)
}
Beispiel #11
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)
}
Beispiel #12
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)
}
Beispiel #13
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)
}
Beispiel #14
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)
}
Beispiel #15
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)
}
Beispiel #16
0
func (c *AccountController) All(res http.ResponseWriter, req *http.Request) {
	if accounts, err := c.Accounts.All(0, 15); err == nil {
		util.Send(res, util.Payload{Result: accounts}, http.StatusOK)
	}
}