コード例 #1
0
ファイル: content.go プロジェクト: ninnemana/API
func GetContentRevisions(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var c site.Content
	var err error
	idStr := params["id"]
	c.Id, err = strconv.Atoi(idStr)

	err = c.GetContentRevisions()
	if err != nil {
		apierror.GenerateError("Trouble getting site content revisions", err, rw, req)
	}

	return encoding.Must(enc.Encode(c))
}
コード例 #2
0
ファイル: content.go プロジェクト: ninnemana/API
func SaveContent(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var c site.Content
	var err error
	idStr := params["id"]
	if idStr != "" {
		c.Id, err = strconv.Atoi(idStr)
		err = c.Get(dtx)
		if err != nil {
			apierror.GenerateError("Trouble getting site content", err, rw, req)
		}
	}

	//json
	requestBody, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError("Trouble reading request body for saving site content", err, rw, req)
	}
	err = json.Unmarshal(requestBody, &c)
	if err != nil {
		apierror.GenerateError("Trouble unmarshalling request body for saving site content", err, rw, req)
	}
	//create or update
	if c.Id > 0 {
		err = c.Update()
	} else {
		err = c.Create()
	}

	if err != nil {
		apierror.GenerateError("Trouble saving site content", err, rw, req)
	}
	return encoding.Must(enc.Encode(c))
}
コード例 #3
0
ファイル: content.go プロジェクト: ninnemana/API
func GetContent(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var c site.Content
	var err error
	idStr := params["id"]
	id, err := strconv.Atoi(idStr)

	if err == nil {
		//Thar be an Id int
		c.Id = id
		err = c.Get(dtx)
		if err != nil {
			apierror.GenerateError("Trouble getting site content by Id.", err, rw, req)
			return ""
		}
	}

	//Thar be a slug
	c.Slug = idStr
	err = c.GetBySlug(dtx)
	if err != nil {
		apierror.GenerateError("Trouble getting site content by slug.", err, rw, req)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
コード例 #4
0
ファイル: content.go プロジェクト: ninnemana/API
func DeleteContent(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var err error
	var c site.Content

	idStr := params["id"]

	id, err := strconv.Atoi(idStr)
	if err != nil {
		apierror.GenerateError("Trouble getting site content ID", err, rw, req)
	}
	c.Id = id
	err = c.Delete()
	if err != nil {
		apierror.GenerateError("Trouble deleting site content", err, rw, req)
	}

	return encoding.Must(enc.Encode(c))
}
コード例 #5
0
ファイル: site_test.go プロジェクト: ninnemana/API
func TestSite(t *testing.T) {

	dtx, err := apicontextmock.Mock()
	if err != nil {
		t.Log(err)
	}

	Convey("Site Contents", t, func() {
		var c site.Content
		var contents site.Contents
		var cr site.ContentRevision
		c.WebsiteId = dtx.WebsiteID
		qs := make(url.Values, 0)
		qs.Add("key", dtx.APIKey)

		c.Title = "test content - controller test"
		cr.Text = "test revision - controller test"
		c.ContentRevisions = append(c.ContentRevisions, cr)

		response := httprunner.ParameterizedJsonRequest("POST", "/site/content", "/site/content", &qs, c, SaveContent)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

		c.Active = true
		response = httprunner.ParameterizedJsonRequest("PUT", "/site/content/:id", "/site/content/"+strconv.Itoa(c.Id), &qs, c, SaveContent)
		So(response.Code, ShouldEqual, 200)

		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/content/:id", "/site/content/"+strconv.Itoa(c.Id), &qs, nil, GetContent)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/content", "/site/content", &qs, nil, GetAllContents)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &contents), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/content/:id/revisions", "/site/content/"+strconv.Itoa(c.Id)+"/revisions", &qs, nil, GetContentRevisions)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("DELETE", "/site/content/:id", "/site/content/"+strconv.Itoa(c.Id), &qs, nil, DeleteContent)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

	})

	Convey("Site Menu", t, func() {
		var m site.Menu
		var menus site.Menus
		m.WebsiteId = dtx.WebsiteID
		qs := make(url.Values, 0)
		qs.Add("key", dtx.APIKey)

		m.Name = "test menu - controller test"

		response := httprunner.ParameterizedJsonRequest("POST", "/site/menu", "/site/menu", &qs, m, SaveMenu)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

		m.Active = true
		response = httprunner.ParameterizedJsonRequest("PUT", "/site/menu/:id", "/site/menu/"+strconv.Itoa(m.Id), &qs, m, SaveMenu)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/menu/:id", "/site/menu/"+strconv.Itoa(m.Id), &qs, nil, GetMenu)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/menu", "/site/menu", &qs, nil, GetAllMenus)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &menus), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/menu/contents/:id", "/site/menu/contents/"+strconv.Itoa(m.Id), &qs, nil, GetMenuWithContents)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("DELETE", "/site/menu/:id", "/site/menu/"+strconv.Itoa(m.Id), &qs, nil, DeleteMenu)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

	})

	Convey("Site Menu", t, func() {
		var s site.Website
		qs := make(url.Values, 0)
		qs.Add("key", dtx.APIKey)

		s.Description = "test site - controller test"

		response := httprunner.ParameterizedJsonRequest("POST", "/site", "/site", &qs, s, SaveSite)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &s), ShouldEqual, nil)

		s.Url = "www.controllertest.com"

		response = httprunner.ParameterizedJsonRequest("PUT", "/site/:id", "/site/"+strconv.Itoa(s.ID), &qs, s, SaveSite)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &s), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/:id/details", "/site/"+strconv.Itoa(s.ID)+"/details", &qs, nil, GetSiteDetails)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &s), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("DELETE", "/site/:id", "/site/"+strconv.Itoa(s.ID), &qs, nil, DeleteSite)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &s), ShouldEqual, nil)

	})

	_ = apicontextmock.DeMock(dtx)
}