Exemplo n.º 1
0
// 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
}
Exemplo n.º 2
0
// 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},
	})
}
Exemplo n.º 3
0
// 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
}