Esempio n. 1
0
func (o Model) CreateModel() Model {

	c, session := connection.GetCollection("models")
	defer session.Close()
	// This guarantees that t.Id has the bson _id generated by mongo
	o.CreatedAt = time.Now()

	// This while loop ensures there is duplicate Hash
	for {

		id := helpers.RandomString(9)
		count, _ := c.Find(bson.M{"_id": id}).Count()
		if count > 0 {

		} else {

			o.Id = id
			break
		}
	}

	err := c.Insert(o)
	if err != nil {
		return Model{}
		log.Fatal(err)
	}
	return o
}
Esempio n. 2
0
func Models() []Model {
	var models []Model
	c, session := connection.GetCollection("models")
	defer session.Close()
	err := c.Find(nil).All(&models)
	if err == nil {
		return models
	} else {
		return models
	}
}
Esempio n. 3
0
func FindModelById(id string) Model {

	c, session := connection.GetCollection("models")
	defer session.Close()
	result := Model{}
	err := c.Find(bson.M{"_id": id}).One(&result)
	if err != nil {
		return result
		log.Fatal(err)
	}

	return result
}
Esempio n. 4
0
func DestroyModel(id string) error {

	c, _ := connection.GetCollection("models")

	return c.Remove(bson.M{"_id": id})
}