func addStory(newStories []*story.Story, rs *rss.Story) []*story.Story { id, seen := db.SeenStory(rs.Id) if seen { return newStories } else { id = db.AddStory(rs) } // Convert into a story struct s := story.FromRSS(id, rs) return append(newStories, s) }
// Get all the stories read by a session func AllRead(sessionid string) []*story.Story { rr := new(readReq) rr.stmt = allRead rr.replyCh = make(chan interface{}) rr.readRows = func(stmt *sql.Stmt) interface{} { // Run the query rows, err := stmt.Query(sessionid) if err != nil { log.Fatal("Cannot execute allRead stmt: ", err) } defer rows.Close() var id int64 r := rss.Story{} stories := make([]*story.Story, 0, 8) for rows.Next() { rows.Scan(&id, &r.Id, &r.Title, &r.Summary, &r.Link, &r.Comments) story := story.FromRSS(id, &r) stories = append(stories, story) } return stories } readCh <- rr // Wait for a reply res := <-rr.replyCh ret, ok := res.([]*story.Story) if !ok { log.Fatal("Returned []*story.Story failed type assertion") } return ret }
// Get the story with the given id func GetStory(storyid int64) *story.Story { rr := new(readReq) rr.stmt = getStory rr.replyCh = make(chan interface{}) rr.readRows = func(stmt *sql.Stmt) interface{} { // Run the query rows, err := stmt.Query(storyid) if err != nil { log.Fatal("Cannot execute getStory stmt: ", err) } defer rows.Close() var id int64 var s *story.Story r := rss.Story{} for rows.Next() { rows.Scan(&id, &r.Id, &r.Title, &r.Summary, &r.Link, &r.Comments) s = story.FromRSS(id, &r) } return s } readCh <- rr // Wait for a reply res := <-rr.replyCh ret, ok := res.(*story.Story) if !ok { log.Fatal("Returned *story.Story failed type assertion") } return ret }