func (db *Db) PersistNewsletter(newsletter *newsletters.Newsletter) { stmt, err := db.connection.Prepare("UPDATE broccoli_newsletters SET title=?, pubdate=? WHERE id = ?") if err != nil { fmt.Printf("There was an error\n") } result, err := stmt.Exec(newsletter.GetTitle(), newsletter.GetPubDate(), newsletter.GetId()) rows_affected, err := result.RowsAffected() if rows_affected == 0 { // Use INSERT stmt, err = db.connection.Prepare("INSERT INTO broccoli_newsletters(title, pubdate) VALUES (?, ?)") _, err = stmt.Exec(newsletter.GetTitle(), newsletter.GetPubDate()) } // Now, store the newsletter sources sources := newsletter.GetSources() sources_lastchecked := newsletter.GetSourcesLastChecked() newsletter_id := newsletter.GetId() for i, src := range sources { src_id := (*src).GetId() // Use update stmt, err = db.connection.Prepare("UPDATE broccoli_newsletters_sources SET newsletter_id=?, source_id=?, source_lastchecked=?") result, err = stmt.Exec(newsletter_id, src_id, sources_lastchecked[i]) rows_affected, err = result.RowsAffected() if rows_affected == 0 { // Use insert stmt, err = db.connection.Prepare("INSERT broccoli_newsletters_sources(newsletter_id, source_id, source_lastchecked) VALUES (?, ?, ?)") _, err = stmt.Exec(newsletter.GetId(), newsletter_id, src_id, sources_lastchecked[i]) } } }
// Produces a good looking HTML for the given newsletter and stories func Produce(newsletter *newsletters.Newsletter, all_stories []*stories.Story) string { var output bytes.Buffer var everything all_information everything.Title = newsletter.GetTitle() everything.NumStories = len(all_stories) // Group stories based on source type everything.TextStories = make([]*stories.Story, 0, 10) for _, story := range all_stories { everything.TextStories = append(everything.TextStories, story) } tmpl, err := template.ParseFiles("./src/munch/producer/email.html") if err != nil { fmt.Printf("There was an error loading the template file: %s\n", err) } tmpl.Execute(&output, everything) b := output.Bytes() len_total := len(b) minified, err := htmlmin.Minify(b, &htmlmin.Options{MinifyScripts: true, MinifyStyles: true, UnquoteAttrs: true}) len_min := len(minified) if err != nil { fmt.Printf("There was an error in minifying... using the unminified version\n") return output.String() } fmt.Printf("Total saving of %d bytes\n", (len_total - len_min)) return string(minified) }