예제 #1
0
func GetTestimonial(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, dtx *apicontext.DataContext) string {
	var err error
	var test testimonials.Testimonial

	if test.ID, err = strconv.Atoi(params["id"]); err != nil {
		apierror.GenerateError("Trouble getting testimonial ID", err, rw, req)
	}
	if err := test.Get(dtx); err != nil {
		apierror.GenerateError("Trouble getting testimonial", err, rw, req)
	}
	return encoding.Must(enc.Encode(test))
}
예제 #2
0
func Save(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var a testimonials.Testimonial
	var err error
	idStr := params["id"]
	if idStr != "" {
		a.ID, err = strconv.Atoi(idStr)
		err = a.Get(dtx)
		if err != nil {
			apierror.GenerateError("Trouble getting testimonial", err, rw, req)
		}
	}
	//json
	requestBody, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError("Trouble reading request body for saving testimonial", err, rw, req)
	}
	err = json.Unmarshal(requestBody, &a)
	if err != nil {
		apierror.GenerateError("Trouble unmarshalling request body for saving testimonial", err, rw, req)
	}
	//create or update
	if a.ID > 0 {
		err = a.Update()
	} else {
		err = a.Create()
	}

	if err != nil {
		apierror.GenerateError("Trouble saving testimonial", err, rw, req)
	}
	return encoding.Must(enc.Encode(a))
}
예제 #3
0
func Delete(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var a testimonials.Testimonial

	idStr := params["id"]

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

	return encoding.Must(enc.Encode(a))
}
예제 #4
0
func TestTestTestimonials(t *testing.T) {

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

	Convey("Testimonials", t, func() {
		var test testimonials.Testimonial
		var tests testimonials.Testimonials

		qs := make(url.Values, 0)
		qs.Add("key", dtx.APIKey)

		test.BrandID = dtx.BrandID
		test.Content = "test content - controller test"

		response := httprunner.ParameterizedJsonRequest("POST", "/testimonials", "/testimonials", &qs, test, Save)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &test), ShouldEqual, nil)

		test.FirstName = "test name"
		response = httprunner.ParameterizedJsonRequest("PUT", "/testimonials", "/testimonials", &qs, test, Save)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &test), ShouldEqual, nil)

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

		response = httprunner.ParameterizedRequest("GET", "/testimonials", "/testimonials", &qs, nil, GetAllTestimonials)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &tests), ShouldEqual, nil)

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

	})
	_ = apicontextmock.DeMock(dtx)
}