当前位置: 首页>>代码示例>>Golang>>正文


Golang datastore.DataStore类代码示例

本文整理汇总了Golang中github.com/raphaelm/backupd/backupd/datastore.DataStore的典型用法代码示例。如果您正苦于以下问题:Golang DataStore类的具体用法?Golang DataStore怎么用?Golang DataStore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DataStore类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: JobAdd

func JobAdd(store datastore.DataStore, w http.ResponseWriter, r *http.Request) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	var obj model.Job
	err = json.Unmarshal(body, &obj)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		return
	}

	if obj.ID != 0 {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("Error: You cannot pass an ID"))
		return
	}
	created, err := store.SaveJob(&obj)

	if !created || err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		return
	}
	w.WriteHeader(http.StatusCreated)
}
开发者ID:raphaelm,项目名称:backupd,代码行数:31,代码来源:jobs.go

示例2: JobDelete

func JobDelete(store datastore.DataStore, w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	obj, err := store.Job(int64(id))
	if obj.ID == 0 || err != nil {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		return
	}

	deleted, err := store.DeleteJob(&obj)

	if !deleted || err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		return
	}

	w.WriteHeader(http.StatusOK)
}
开发者ID:raphaelm,项目名称:backupd,代码行数:26,代码来源:jobs.go

示例3: BackupIndex

func BackupIndex(store datastore.DataStore, w http.ResponseWriter, r *http.Request) {
	jobs, err := store.Backups()
	if err != nil {
		panic(err)
	}

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(jobs); err != nil {
		panic(err)
	}
}
开发者ID:raphaelm,项目名称:backupd,代码行数:12,代码来源:backups.go

示例4: JobUpdate

func JobUpdate(store datastore.DataStore, w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	obj, err := store.Job(int64(id))
	if obj.ID == 0 || err != nil {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		return
	}

	oldid := obj.ID
	err = json.Unmarshal(body, &obj)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		return
	}

	if obj.ID != oldid {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("Error: You cannot change an ID"))
		return
	}
	_, err = store.SaveJob(&obj)

	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(fmt.Sprintf("Error: %s", err)))
		return
	}
	w.WriteHeader(http.StatusOK)
}
开发者ID:raphaelm,项目名称:backupd,代码行数:44,代码来源:jobs.go

示例5: testJobs

func testJobs(s datastore.DataStore, t *testing.T) {
	// Test only valid RemoteIDs are accepted
	j := model.Job{JobName: "foo", RemoteID: 0}
	created, err := s.SaveJob(&j)
	assert.NotNil(t, err)

	r := model.Remote{Driver: "ssh", Location: "foo"}
	created, err = s.SaveRemote(&r)
	r2 := model.Remote{Driver: "ssh", Location: "bar"}
	created, err = s.SaveRemote(&r2)

	j = model.Job{JobName: "foo", RemoteID: r.ID}
	created, err = s.SaveJob(&j)
	assert.Equal(t, true, created)
	assert.Nil(t, err)

	j2, err := s.Job(j.ID)
	assert.Nil(t, err)
	assert.Equal(t, j.JobName, j2.JobName)
	assert.Equal(t, j.ID, j2.ID)

	j2.JobName = "bar"
	created, err = s.SaveJob(&j2)
	assert.Equal(t, false, created)
	assert.Nil(t, err)

	jslice, err := s.Jobs()
	assert.Nil(t, err)
	assert.Equal(t, 1, len(jslice))
	assert.Equal(t, j2.JobName, jslice[0].JobName)
	assert.Equal(t, j2.ID, jslice[0].ID)

	jslice, err = s.JobsForRemote(&r)
	assert.Nil(t, err)
	assert.Equal(t, 1, len(jslice))

	jslice, err = s.JobsForRemote(&r2)
	assert.Nil(t, err)
	assert.Equal(t, 0, len(jslice))

	s.DeleteJob(&j2)

	jslice, err = s.Jobs()
	assert.Nil(t, err)
	assert.Equal(t, 0, len(jslice))

	_, err = s.Job(j.ID)
	assert.NotNil(t, err)

	s.DeleteRemote(&r)
	s.DeleteRemote(&r2)
}
开发者ID:raphaelm,项目名称:backupd,代码行数:52,代码来源:datastore_test.go

示例6: testRemotes

func testRemotes(s datastore.DataStore, t *testing.T) {
	r := model.Remote{Driver: "ssh", Location: "foo"}
	created, err := s.SaveRemote(&r)
	assert.Equal(t, true, created)
	assert.Nil(t, err)

	r2, err := s.Remote(r.ID)
	assert.Nil(t, err)
	assert.Equal(t, r.Location, r2.Location)
	assert.Equal(t, r.ID, r2.ID)

	r2.Location = "bar"
	created, err = s.SaveRemote(&r2)
	assert.Equal(t, false, created)
	assert.Nil(t, err)

	rslice, err := s.Remotes()
	assert.Nil(t, err)
	assert.Equal(t, 1, len(rslice))
	assert.Equal(t, r2.Location, rslice[0].Location)
	assert.Equal(t, r2.ID, rslice[0].ID)

	s.DeleteRemote(&r2)

	rslice, err = s.Remotes()
	assert.Nil(t, err)
	assert.Equal(t, 0, len(rslice))

	_, err = s.Remote(r.ID)
	assert.NotNil(t, err)
}
开发者ID:raphaelm,项目名称:backupd,代码行数:31,代码来源:datastore_test.go

示例7: testDeleteCascade

func testDeleteCascade(s datastore.DataStore, t *testing.T) {
	r := model.Remote{Driver: "ssh", Location: "foo"}
	created, err := s.SaveRemote(&r)
	assert.Equal(t, true, created)
	assert.Nil(t, err)

	j := model.Job{JobName: "foo", RemoteID: r.ID}
	created, err = s.SaveJob(&j)
	assert.Equal(t, true, created)
	assert.Nil(t, err)

	b := model.Backup{Result: model.BackupCompleted, JobID: j.ID}
	created, err = s.SaveBackup(&b)
	assert.Equal(t, true, created)
	assert.Nil(t, err)

	jslice, err := s.Jobs()
	assert.Nil(t, err)
	assert.Equal(t, 1, len(jslice))

	bslice, err := s.Backups()
	assert.Nil(t, err)
	assert.Equal(t, 1, len(bslice))

	s.DeleteRemote(&r)

	jslice, err = s.Jobs()
	assert.Nil(t, err)
	assert.Equal(t, 0, len(jslice))

	bslice, err = s.Backups()
	assert.Nil(t, err)
	assert.Equal(t, 0, len(bslice))
}
开发者ID:raphaelm,项目名称:backupd,代码行数:34,代码来源:datastore_test.go

示例8: testBackups

func testBackups(s datastore.DataStore, t *testing.T) {
	// Test only valid JobIDs are accepted
	b := model.Backup{Result: model.BackupCompleted, JobID: 0}
	created, err := s.SaveBackup(&b)
	assert.NotNil(t, err)

	r := model.Remote{Driver: "ssh", Location: "foo"}
	created, err = s.SaveRemote(&r)
	j := model.Job{RemoteID: r.ID}
	created, err = s.SaveJob(&j)
	j2 := model.Job{RemoteID: r.ID}
	created, err = s.SaveJob(&j2)

	b = model.Backup{Result: model.BackupCompleted, JobID: j.ID}
	created, err = s.SaveBackup(&b)
	assert.Equal(t, true, created)
	assert.Nil(t, err)

	b2, err := s.Backup(b.ID)
	assert.Nil(t, err)
	assert.Equal(t, b.Result, b2.Result)
	assert.Equal(t, b.ID, b2.ID)

	b2.Result = model.BackupFailed
	created, err = s.SaveBackup(&b2)
	assert.Equal(t, false, created)
	assert.Nil(t, err)

	bslice, err := s.Backups()
	assert.Nil(t, err)
	assert.Equal(t, 1, len(bslice))
	assert.Equal(t, b2.Result, bslice[0].Result)
	assert.Equal(t, b2.ID, bslice[0].ID)

	bslice, err = s.BackupsForJob(&j)
	assert.Nil(t, err)
	assert.Equal(t, 1, len(bslice))

	bslice, err = s.BackupsForJob(&j2)
	assert.Nil(t, err)
	assert.Equal(t, 0, len(bslice))

	s.DeleteBackup(&b2)

	bslice, err = s.Backups()
	assert.Nil(t, err)
	assert.Equal(t, 0, len(bslice))

	_, err = s.Backup(b.ID)
	assert.NotNil(t, err)

	s.DeleteRemote(&r)
}
开发者ID:raphaelm,项目名称:backupd,代码行数:53,代码来源:datastore_test.go


注:本文中的github.com/raphaelm/backupd/backupd/datastore.DataStore类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。