func GetTopic(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, dtx *apicontext.DataContext) string { var err error var topic forum.Topic if topic.ID, err = strconv.Atoi(params["id"]); err != nil { apierror.GenerateError("Trouble getting forum topic ID", err, rw, req) } if err := topic.Get(dtx); err != nil { apierror.GenerateError("Trouble getting forum topic", err, rw, req) } return encoding.Must(enc.Encode(topic)) }
func AddTopic(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, dtx *apicontext.DataContext) string { var err error var topic forum.Topic if topic.GroupID, err = strconv.Atoi(req.FormValue("groupID")); err != nil { apierror.GenerateError("Trouble getting forum group ID for new topic", err, rw, req) } if req.FormValue("closed") != "" { if topic.Closed, err = strconv.ParseBool(req.FormValue("closed")); err != nil { apierror.GenerateError("Trouble adding forum topic -- boolean closed parameter is invalid", err, rw, req) } } topic.Name = req.FormValue("name") topic.Description = req.FormValue("description") topic.Image = req.FormValue("image") topic.Active = true if err = topic.Add(); err != nil { apierror.GenerateError("Trouble adding forum topic", err, rw, req) } return encoding.Must(enc.Encode(topic)) }
func AddPost(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, dtx *apicontext.DataContext) string { var err error var post forum.Post //we add a post either by topic (new post) or by identifying the parentID (replying to an existing post) if req.FormValue("topicID") == "" && req.FormValue("parentID") == "" { apierror.GenerateError("Trouble adding forum post -- Missing topic ID or parent post ID", err, rw, req) } //we're adding a new post, so we use the topicID in order to create a new thread if req.FormValue("topicID") != "" { var topic forum.Topic if topic.ID, err = strconv.Atoi(req.FormValue("topicID")); err != nil { apierror.GenerateError("Trouble adding forum post -- invalid topic ID", err, rw, req) } //verify that this topic exists if err = topic.Get(dtx); err != nil { apierror.GenerateError("Trouble getting forum post -- topic doesn't exist", err, rw, req) } //create a new thread and save it thread := forum.Thread{ TopicID: topic.ID, Active: true, } if err = thread.Add(); err != nil { apierror.GenerateError("Trouble adding forum post -- failed to add thread", err, rw, req) } post.ThreadID = thread.ID } else { //we're replying to an existing post var parentPost forum.Post if parentPost.ID, err = strconv.Atoi(req.FormValue("parentID")); err != nil { apierror.GenerateError("Trouble adding forum post reply -- parentID is invalid", err, rw, req) } if err = parentPost.Get(dtx); err != nil { apierror.GenerateError("Trouble getting parent forum post", err, rw, req) } post.ParentID = parentPost.ID post.ThreadID = parentPost.ThreadID } if req.FormValue("notify") != "" { if post.Notify, err = strconv.ParseBool(req.FormValue("notify")); err != nil { apierror.GenerateError("Trouble adding post -- boolean notify parameter is invalid", err, rw, req) } } if req.FormValue("sticky") != "" { if post.Sticky, err = strconv.ParseBool(req.FormValue("sticky")); err != nil { apierror.GenerateError("Trouble adding post -- boolean sticky parameter is invalid", err, rw, req) } } post.Approved = true post.Active = true post.Title = req.FormValue("title") post.Post = req.FormValue("post") post.Name = req.FormValue("name") post.Email = req.FormValue("email") post.Company = req.FormValue("company") //post.IPAddress = "" if err = post.Add(); err != nil { apierror.GenerateError("Trouble adding post", err, rw, req) } return encoding.Must(enc.Encode(post)) }