func testUpdatePost(t *testing.T, title string, markdown string) { // create JSON payload var p Post p.Title = title p.Markdown = markdown p.Content = string(blackfriday.MarkdownCommon([]byte(markdown))) apiPayload, _ := json.Marshal(p) // save changes to global object for further testing comparison post.Title = p.Title post.Content = string(blackfriday.MarkdownCommon([]byte(markdown))) post.Markdown = markdown post.Excerpt = excerpt.Make(p.Content, 15) testUpdatePostAPI(t, apiPayload) // creates form-encoded payload p2 := url.Values{} p2.Set("title", title) p2.Add("markdown", markdown) frontendPayload := p2.Encode() // save changes to global object for further testing comparison post.Title = p2.Get("title") post.Markdown = p2.Get("markdown") post.Excerpt = excerpt.Make(post.Content, 15) testUpdatePostFrontend(t, frontendPayload) TestReadPost(t) }
// Update or post.Update updates parameter "entry" with data given in parameter "post". // Requires active session cookie. // Returns updated Post object and an error object. func (post Post) Update(entry Post) (Post, error) { entry.ID = post.ID entry.Content = string(blackfriday.MarkdownCommon([]byte(entry.Markdown))) entry.Excerpt = excerpt.Make(entry.Content, 15) entry.Slug = slug.Create(entry.Title) entry.Updated = time.Now().UTC().Round(time.Second).Unix() _, err := db.NamedExec( "UPDATE posts SET title = :title, content = :content, markdown = :markdown, slug = :slug, excerpt = :excerpt, published = :published, updated = :updated WHERE id = :id", entry) if err != nil { return post, err } entry.Viewcount = post.Viewcount entry.Created = post.Created entry.TimeOffset = post.TimeOffset entry.Author = post.Author return entry, nil }
func testCreatePost(t *testing.T, owner int64, title string, markdown string) { var u User u.ID = owner var p Post p.ID = post.ID + 1 p.Title = title p.Markdown = markdown p.Content = string(blackfriday.MarkdownCommon([]byte(markdown))) p.Slug = slug.Create(p.Title) p.Author = u.ID p.Excerpt = excerpt.Make(p.Content, 15) p.Viewcount = 0 payload, err := json.Marshal(p) if err != nil { panic(err) } testCreatePostRequest(t, payload, p) }
// Insert or post.Insert inserts Post object into database. // Requires active session cookie // Fills post.Author, post.Created, post.Edited, post.Excerpt, post.Slug and post.Published automatically. // Returns Post and error object. func (post Post) Insert(user User) (Post, error) { _, offset, err := timezone.Offset(user.Location) if err != nil { return post, err } post.TimeOffset = offset post.Content = string(blackfriday.MarkdownCommon([]byte(post.Markdown))) post.Author = user.ID post.Created = time.Now().UTC().Round(time.Second).Unix() post.Updated = post.Created post.Excerpt = excerpt.Make(post.Content, 15) post.Slug = slug.Create(post.Title) post.Published = false post.Viewcount = 0 _, err = db.NamedExec(`INSERT INTO posts (title, content, markdown, slug, author, excerpt, viewcount, published, created, updated, timeoffset) VALUES (:title, :content, :markdown, :slug, :author, :excerpt, :viewcount, :published, :created, :updated, :timeoffset)`, post) if err != nil { return post, err } return post, nil }