Exemple #1
0
// Printer
func (b Backend) Print(article interface{}) error {
	s, col := b.Col()
	defer s.Close()

	if err := col.Insert(article); err != nil {
		return backends.NewError(backends.StatusDatastoreError, "Failed to create article", err)
	}

	return nil
}
Exemple #2
0
func (b Backend) BySession(key string) (users.User, error) {
	s, c := b.Col()
	defer s.Close()

	u := users.Model{b, new(users.Data)}
	if err := c.Find(bson.M{"session": key}).One(&u.Data); err != nil {
		return u, backends.NewError(backends.StatusDatastoreError, "user cache", err)
	}

	return u, nil
}
Exemple #3
0
// Reader
func (b Backend) ByUsername(username string) (users.User, error) {
	s, c := b.Col()
	defer s.Close()

	u := users.Model{b, new(users.Data)}
	if err := c.Find(bson.M{"username": username}).One(u.Data); err != nil {
		return u, backends.NewError(backends.StatusDatastoreError, "user authentication", err)
	}

	return u, nil
}
Exemple #4
0
func (b Backend) WriteImgs(titlePath string, imgs interface{}) error {
	session, col := b.Col()
	defer session.Close()

	selector := bson.M{"titlePath": titlePath}
	change := bson.M{"$set": bson.M{"imgs": imgs}} //&a.Imgs}}
	if err := col.Update(selector, change); err != nil {
		return backends.NewError(backends.StatusDatastoreError, "Failed to update images", err)
	}
	return nil
}
Exemple #5
0
func (b Backend) Delete(titlePath string) error {
	s, col := b.Col()
	defer s.Close()

	// update the article's content
	selector := bson.M{"titlePath": titlePath}
	if err := col.Remove(selector); err != nil {
		return backends.NewError(backends.StatusDatastoreError, "Failed to remove article", err)
	}
	return nil
}
Exemple #6
0
func (b Backend) UpdateLastSignin(username string, on time.Time) error {
	s, c := b.Col()
	defer s.Close()

	selector := bson.M{"username": username}
	change := bson.M{"$set": bson.M{"lastSignin": on}}
	if err := c.Update(selector, change); err != nil {
		log.Println("Failed to update 'lastSignin' for: ", username, " : ", err)
		return backends.NewError(backends.StatusDatastoreError, "user update", err)
	}
	return nil
}
Exemple #7
0
// Writer
func (b Backend) UpdateSession(username, key string) error {
	s, c := b.Col()
	defer s.Close()

	selector := bson.M{"username": username}
	change := bson.M{"$set": bson.M{"session": key}}
	if err := c.Update(selector, change); err != nil {
		log.Println("Failed to update 'session' for: ", username, " : ", err)
		return backends.NewError(backends.StatusDatastoreError, "user update", err)
	}
	return nil
}
Exemple #8
0
func (b Backend) Publish(titlePath string, publish bool) error {
	session, col := b.Col()
	defer session.Close()

	selector := bson.M{"titlePath": titlePath}
	change := bson.M{"$set": bson.M{"isPublished": publish}}
	if err := col.Update(selector, change); err != nil {
		log.Println(err)
		return backends.NewError(backends.StatusDatastoreError, "Failed to update published status", err)
	}
	return nil
}
Exemple #9
0
func (b Backend) UpdateContent(titlePath, content string, modified time.Time) error {
	s, col := b.Col()
	defer s.Close()

	// update the article's content
	selector := bson.M{"titlePath": titlePath}
	change := bson.M{"$set": bson.M{"content": &content, "modified": modified}}
	if err := col.Update(selector, change); err != nil {
		return backends.NewError(backends.StatusDatastoreError, "Failed to update article's content", err)
	}
	return nil
}
Exemple #10
0
// Reader
func (b Backend) ByTitlePath(titlePath string, unPublished bool) (interface{}, error) {
	s, col := b.Col()
	defer s.Close()

	c := b.NewArticle()
	query := bson.M{"titlePath": titlePath}
	if !unPublished {
		query["isPublished"] = true
	}
	if err := col.Find(query).One(c); err != nil {
		return c, backends.NewError(backends.StatusNotFound, "Article not found", err)
	}
	b.SetPrinter(c, b)
	return c, nil
}
Exemple #11
0
func (b Backend) Recent(limit, page int, unPublished bool) (interface{}, error) {
	s, col := b.Col()
	defer s.Close()

	c := b.NewArticles(limit)
	var q bson.M = nil
	if !unPublished {
		q = bson.M{"isPublished": true}
	}

	/*iter := col.Find(q).
		Sort("created").
		Skip(page * limit).
		Iter()

	i := new(articles.Article)
	for iter.Next(&i) {
		i.Printer = b
		c = append(c, *i)
		i = new(articles.Article)
	}
	if iter.Err() != nil {
		return c, backends.NewError(backends.StatusDatastoreError, "Failed to query article list", iter.Err())
	}*/

	/* Using the below, instead of the above because:
	 *  according to `ab -c 35 -rn 1000 http://127.0.0.1:9991/articles/`
	 *  the the below is about 450 req/s faster
	 */
	if err := col.Find(q).
		Sort("-created").
		Skip(page * limit).
		Limit(limit).
		All(c); err != nil {
		return c, backends.NewError(backends.StatusDatastoreError, "Failed to query article list", err)
	}
	b.SetPrinter(c, b)

	return c, nil
}