func GetCategories(req *f.Request) ([]*Category, error) { categories := []*Category{} // I would like to use datastore.DataStore.List() here. query := ds.NewQuery("Category") _, err := query.GetAll(jux.GetNewContext(req), &categories) return categories, err }
// Return a list of articles matching the given category. func GetArticles(req *f.Request, category string, offset int, limit int) ([]*Article, error) { articles := []*Article{} // Create a query for the given interface. query := ds.NewQuery("Article"). Filter("Category =", category). Order("Created"). Offset(offset). Limit(limit) _, err := query.GetAll(jux.GetNewContext(req), &articles) return articles, err }
// Shows a single article for the given id. func read(req *f.Request, res *f.Response, next func()) { // Create a Query. article := &Article{} article.Id = req.Query["id"] // Grab the datastore. ds := datastore.New(jux.GetNewContext(req)) if err := ds.Read(article); err != nil { res.Render("notfound/main.html", map[string]string{ "error": "Article not found.", }) return } // Inflate the Article. article.InflateBody() // Render the Article. res.Locals["pageTitle"] = article.Title res.Render("jux_article/read.html", map[string][]*Article{ "Articles": []*Article{article}, }) }
// Add the listed categories to the DataStore. func LoadCategories(req *f.Request) error { // Grab the data store. ds := datastore.New(jux.GetNewContext(req)) // Get the Category title. categories := map[string]string{ "general": "General", "alice": "Alice's Adventures in Wonderland", } // Walk over the categories and add them to the store. for key, value := range categories { category := &Category{} category.Id = key category.Category = key category.Title = value if err := ds.Create(category); err != nil { return err } } return nil }
// Read all files in the given directory and store them in the DataStore. func LoadJsonArticles(req *f.Request, dirname string) error { ds := datastore.New(jux.GetNewContext(req)) list, err := ioutil.ReadDir(dirname) if err != nil { panic(err) } for _, file := range list { if file.IsDir() != true { filepath := path.Join(dirname, file.Name()) if source, ok := ReadJsonArticle(filepath); ok { article := &Article{} article.Id = file.Name() article.Title = source.Title article.Category = source.Category article.Text = []byte(source.Text) if err := ds.Create(article); err != nil { return err } } } time.Sleep(time.Second) } return nil }