Example #1
0
func Init(url, dbname string) {
	ok := true
	config := revel.Config

	if url == "" {
		url, ok = config.String("db.url")
		if !ok {
			url, ok = config.String("db.urlEnv")
			if ok {
				utils.Log("get db conf from urlEnv: " + url)
			}
		} else {
			utils.Log("get db conf from db.url: " + url)
		}

		if ok {
			urls := strings.Split(url, "/")
			dbname = urls[len(urls)-1]
		}
	}

	if dbname == "" {
		dbname, _ = config.String("db.dbname")
	}

	if !ok {
		host, _ := revel.Config.String("db.host")
		port, _ := config.String("db.port")
		username, _ := config.String("db.username")
		password, _ := config.String("db.password")

		usernameAndPassword := username + ":" + password + "@"

		if username == "" || password == "" {
			usernameAndPassword = ""
		}

		url = "mongodb://" + usernameAndPassword + host + ":" + port + "/" + dbname
	}

	utils.Log(url)

	var err error
	Session, err = mgo.Dial(url)
	if err != nil {
		panic(err)
	}

	Session.SetMode(mgo.Monotonic, true)

	Users = Session.DB(dbname).C("users")

	Notebooks = Session.DB(dbname).C("notebooks")

}
func (c Notebook) AddNotebook(parentNotebookId string) revel.Result {
	notebook := models.Notebook{
		UserId: c.GetObjectUserId(),
	}

	if parentNotebookId != "" {
		notebook.ParentNotebookId = bson.ObjectIdHex(parentNotebookId)
	}

	ret, notebook := service.NotebookS.AddNotebook(notebook)

	if ret {
		return c.RenderJson(notebook)
	} else {
		utils.Log("false")
		return c.RenderJson(false)
	}
}
Example #3
0
func (this *NotebookService) AddNotebook(notebook Notebook) (bool, Notebook) {
	if notebook.NotebookId == "" {
		notebook.NotebookId = bson.NewObjectId()
	}

	now := time.Now()
	notebook.CreatedTime = now
	notebook.UpdatedTime = now
	notebook.IsDeleted = false

	utils.LogJson(notebook)

	err := database.Notebooks.Insert(notebook)
	if err != nil {
		utils.Log(err)
		return false, notebook
	}
	return true, notebook
}
func (c Notebook) DeleteNotebook(notebookId string) revel.Result {
	ok, msg := service.NotebookS.DeleteNotebook(c.GetUserId(), notebookId)
	utils.Log(ok)
	utils.Log(msg)
	return c.RenderJson(models.Response{Ok: ok, Msg: msg})
}