本文整理匯總了Golang中github.com/ninnemana/API/models/customer.Customer.Create方法的典型用法代碼示例。如果您正苦於以下問題:Golang Customer.Create方法的具體用法?Golang Customer.Create怎麽用?Golang Customer.Create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/ninnemana/API/models/customer.Customer
的用法示例。
在下文中一共展示了Customer.Create方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SaveCustomer
func SaveCustomer(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
var c customer.Customer
var err error
if r.FormValue("id") != "" || params["id"] != "" {
id := r.FormValue("id")
if id == "" {
id = params["id"]
}
if c.Id, err = strconv.Atoi(id); err != nil {
apierror.GenerateError("Trouble getting customer ID", err, rw, r)
return ""
}
if err = c.Basics(dtx.APIKey); err != nil {
apierror.GenerateError("Trouble getting customer", err, rw, r)
return ""
}
}
//json
var requestBody []byte
if requestBody, err = ioutil.ReadAll(r.Body); err != nil {
apierror.GenerateError("Trouble reading request body while saving customer", err, rw, r)
return ""
}
if err = json.Unmarshal(requestBody, &c); err != nil {
apierror.GenerateError("Trouble unmarshalling json request body while saving customer", err, rw, r)
return ""
}
//create or update
if c.Id > 0 {
err = c.Update()
} else {
err = c.Create()
}
if err != nil {
msg := "Trouble creating customer"
if c.Id > 0 {
msg = "Trouble updating customer"
}
apierror.GenerateError(msg, err, rw, r)
return ""
}
return encoding.Must(enc.Encode(c))
}
示例2: TestCustomerPrice
func TestCustomerPrice(t *testing.T) {
var err error
var p customer.Price
var ps customer.Prices
var c customer.Customer
c.Name = "Dog Bountyhunter"
c.Create()
Convey("Testing customer/Price", t, func() {
//test create customer price
form := url.Values{"custID": {strconv.Itoa(c.Id)}, "partID": {"11000"}, "price": {"123456"}}
v := form.Encode()
body := strings.NewReader(v)
thyme := time.Now()
testThatHttp.Request("post", "/customer/prices", "", "", CreateUpdatePrice, body, "application/x-www-form-urlencoded")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, customer.Price{})
So(p.ID, ShouldBeGreaterThan, 0)
//test update customer price
form = url.Values{"isSale": {"true"}, "saleStart": {"01/01/2001"}, "saleEnd": {"01/01/2015"}}
v = form.Encode()
body = strings.NewReader(v)
thyme = time.Now()
testThatHttp.Request("post", "/customer/prices/", ":id", strconv.Itoa(p.ID), CreateUpdatePrice, body, "application/x-www-form-urlencoded")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, customer.Price{})
So(p.IsSale, ShouldEqual, 1)
start, _ := time.Parse(inputTimeFormat, "01/01/2001")
So(p.SaleStart, ShouldResemble, start)
//test get customer price
thyme = time.Now()
testThatHttp.Request("get", "/customer/prices/", ":id", strconv.Itoa(p.ID), GetPrice, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, customer.Price{})
//test get all customer price
thyme = time.Now()
testThatHttp.Request("get", "/customer/prices", "", "", GetAllPrices, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*8) //Long
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ps)
So(err, ShouldBeNil)
So(ps, ShouldHaveSameTypeAs, customer.Prices{})
//test get customer price by part
thyme = time.Now()
testThatHttp.Request("get", "/customer/prices/part/", ":id", strconv.Itoa(p.ID), GetPricesByPart, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ps)
So(err, ShouldBeNil)
So(ps, ShouldHaveSameTypeAs, customer.Prices{})
//test get customer price by customer
thyme = time.Now()
testThatHttp.Request("get", "/customer/pricesByCustomer/", ":id", strconv.Itoa(c.Id), GetPriceByCustomer, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, customer.Price{})
//test get sales
form = url.Values{"id": {strconv.Itoa(c.Id)}, "start": {"01/01/2000"}, "end": {"01/01/2016"}}
v = form.Encode()
body = strings.NewReader(v)
thyme = time.Now()
testThatHttp.Request("post", "/customer/prices/sale", "", "", GetSales, body, "application/x-www-form-urlencoded")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ps)
So(err, ShouldBeNil)
So(ps, ShouldHaveSameTypeAs, customer.Prices{})
//test delete customer price
thyme = time.Now()
testThatHttp.Request("delete", "/customer/prices/", ":id", strconv.Itoa(p.ID), DeletePrice, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
So(err, ShouldBeNil)
So(p, ShouldHaveSameTypeAs, customer.Price{})
})
//teardown
c.Delete()
}
示例3: BenchmarkCRUDCustomerPrice
func BenchmarkCRUDCustomerPrice(b *testing.B) {
var p customer.Price
var c customer.Customer
c.Name = "Axl Rose"
c.Create()
qs := make(url.Values, 0)
Convey("CustomerPrice", b, func() {
form := url.Values{"custID": {strconv.Itoa(c.Id)}, "partID": {"11000"}, "price": {"123456"}}
//create
(&httprunner.BenchmarkOptions{
Method: "POST",
Route: "/customer/prices",
ParameterizedRoute: "/customer/prices",
Handler: CreateUpdatePrice,
QueryString: &qs,
JsonBody: p,
FormBody: form,
Runs: b.N,
}).RequestBenchmark()
//get
(&httprunner.BenchmarkOptions{
Method: "GET",
Route: "/customer/prices",
ParameterizedRoute: "/customer/prices/" + strconv.Itoa(p.ID),
Handler: GetPrice,
QueryString: &qs,
JsonBody: p,
FormBody: nil,
Runs: b.N,
}).RequestBenchmark()
//get all
(&httprunner.BenchmarkOptions{
Method: "GET",
Route: "/customer/prices",
ParameterizedRoute: "/customer/prices",
Handler: GetAllPrices,
QueryString: &qs,
JsonBody: p,
FormBody: nil,
Runs: b.N,
}).RequestBenchmark()
//get by part
(&httprunner.BenchmarkOptions{
Method: "GET",
Route: "/customer/prices/part",
ParameterizedRoute: "/customer/prices/part/" + strconv.Itoa(p.ID),
Handler: GetPricesByPart,
QueryString: &qs,
JsonBody: p,
FormBody: nil,
Runs: b.N,
}).RequestBenchmark()
//get by
(&httprunner.BenchmarkOptions{
Method: "GET",
Route: "/customer/pricesByCustomer",
ParameterizedRoute: "/customer/pricesByCustomer/" + strconv.Itoa(c.Id),
Handler: GetPriceByCustomer,
QueryString: &qs,
JsonBody: p,
FormBody: nil,
Runs: b.N,
}).RequestBenchmark()
//delete
(&httprunner.BenchmarkOptions{
Method: "DELETE",
Route: "/customer/prices",
ParameterizedRoute: "/customer/prices/" + strconv.Itoa(p.ID),
Handler: DeleteLocation,
QueryString: &qs,
JsonBody: p,
FormBody: nil,
Runs: b.N,
}).RequestBenchmark()
})
//teardown
c.Delete()
}
示例4: TestCustomerContent
func TestCustomerContent(t *testing.T) {
flag.Parse()
//customer - for db setup only
var c customer.Customer
var content custcontent.CustomerContent
var partContent custcontent.PartContent
var categoryContent custcontent.CustomerContent
var ct custcontent.ContentType
var crs custcontent.CustomerContentRevisions
var contents []custcontent.CustomerContent
var catContent custcontent.CategoryContent
var catContents []custcontent.CategoryContent
var err error
var apiKey string
catContent.CategoryId = 1
ct.Type = "test"
ct.Create()
c.Name = "test cust"
c.Create()
Convey("Testing customer/Customer_content", t, func() {
//test create part content
content.Text = "new content"
content.ContentType.Id = ct.Id
bodyBytes, _ := json.Marshal(content)
bodyJson := bytes.NewReader(bodyBytes)
thyme := time.Now()
testThatHttp.Request("post", "/customer/cms/part/", ":id", strconv.Itoa(11000)+"?key="+dtx.APIKey, CreatePartContent, bodyJson, "application/json")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
if testThatHttp.Response.Code == 200 { //returns 500 when ninnemana user doesn't exist
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &content)
So(err, ShouldBeNil)
So(content, ShouldHaveSameTypeAs, custcontent.CustomerContent{})
}
//create category content
categoryContent.Text = "new content"
categoryContent.ContentType.Id = ct.Id
bodyBytes, _ = json.Marshal(categoryContent)
bodyJson = bytes.NewReader(bodyBytes)
thyme = time.Now()
testThatHttp.Request("post", "/customer/cms/category/", ":id", strconv.Itoa(catContent.CategoryId)+"?key="+apiKey, CreateCategoryContent, bodyJson, "application/json")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &categoryContent)
So(err, ShouldBeNil)
So(categoryContent, ShouldHaveSameTypeAs, custcontent.CustomerContent{})
//test update part content
content.Text = "newerer content"
bodyBytes, _ = json.Marshal(content)
bodyJson = bytes.NewReader(bodyBytes)
thyme = time.Now()
testThatHttp.Request("put", "/customer/cms/part/", ":id", strconv.Itoa(11000)+"?key="+apiKey, UpdatePartContent, bodyJson, "application/json")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &content)
So(err, ShouldBeNil)
So(content, ShouldHaveSameTypeAs, custcontent.CustomerContent{})
//test update category content
categoryContent.Text = "newerer content"
bodyBytes, _ = json.Marshal(categoryContent)
bodyJson = bytes.NewReader(bodyBytes)
thyme = time.Now()
testThatHttp.Request("put", "/customer/cms/part/", ":id", strconv.Itoa(catContent.CategoryId)+"?key="+apiKey, UpdateCategoryContent, bodyJson, "application/json")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &categoryContent)
So(err, ShouldBeNil)
So(categoryContent, ShouldHaveSameTypeAs, custcontent.CustomerContent{})
//test get part content (unique)
thyme = time.Now()
testThatHttp.Request("get", "/customer/cms/part/", ":id", strconv.Itoa(11000)+"?key="+apiKey, UniquePartContent, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &contents)
So(err, ShouldBeNil)
So(contents, ShouldHaveSameTypeAs, []custcontent.CustomerContent{})
//test get all part content
thyme = time.Now()
testThatHttp.Request("get", "/customer/cms/part", "", "?key="+apiKey, AllPartContent, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &contents)
So(err, ShouldBeNil)
So(contents, ShouldHaveSameTypeAs, []custcontent.CustomerContent{})
//test get category content (all content)
thyme = time.Now()
testThatHttp.Request("get", "/customer/cms/part", "", "?key="+apiKey, AllCategoryContent, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &contents)
//.........這裏部分代碼省略.........
示例5: TestCustomerUser
func TestCustomerUser(t *testing.T) {
var err error
var cu customer.CustomerUser
var c customer.Customer
c.Name = "Dog Bountyhunter"
c.BrandIDs = append(c.BrandIDs, 1)
c.Create()
var pub, pri, auth apiKeyType.ApiKeyType
if database.GetCleanDBFlag() != "" {
t.Log(database.GetCleanDBFlag())
//setup apiKeyTypes
pub.Type = "Public"
pri.Type = "Private"
auth.Type = "Authentication"
pub.Create()
pri.Create()
auth.Create()
}
Convey("Testing customer/User", t, func() {
//test create customer user
form := url.Values{"name": {"Mitt Romney"}, "email": {"[email protected]"}, "pass": {"robthepoor"}, "customerID": {strconv.Itoa(c.Id)}, "isActive": {"true"}, "locationID": {"1"}, "isSudo": {"true"}, "cust_ID": {strconv.Itoa(c.Id)}}
v := form.Encode()
body := strings.NewReader(v)
thyme := time.Now()
testThatHttp.Request("post", "/customer/user/register", "", "", RegisterUser, body, "application/x-www-form-urlencoded")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
So(err, ShouldBeNil)
So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})
So(cu.Id, ShouldNotBeEmpty)
//key stuff - get apiKey
var apiKey string
for _, k := range cu.Keys {
if strings.ToLower(k.Type) == "public" {
apiKey = k.Key
}
}
//test update customer user
form = url.Values{"name": {"Michelle Bachman"}}
v = form.Encode()
body = strings.NewReader(v)
thyme = time.Now()
testThatHttp.Request("post", "/customer/user/", ":id", cu.Id, UpdateCustomerUser, body, "application/x-www-form-urlencoded")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
So(err, ShouldBeNil)
So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})
So(cu.Name, ShouldNotEqual, "Mitt Romney")
//test authenticateUser
err = c.JoinUser(cu)
So(err, ShouldBeNil)
form = url.Values{"email": {"[email protected]"}, "password": {"robthepoor"}}
v = form.Encode()
body = strings.NewReader(v)
thyme = time.Now()
testThatHttp.Request("post", "/customer/auth", "", "", AuthenticateUser, body, "application/x-www-form-urlencoded")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
So(err, ShouldBeNil)
So(c, ShouldHaveSameTypeAs, customer.Customer{})
//test keyed user authentication
thyme = time.Now()
testThatHttp.Request("get", "/customer/auth", "", "?key="+apiKey, KeyedUserAuthentication, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
So(err, ShouldBeNil)
So(c, ShouldHaveSameTypeAs, customer.Customer{})
//test get user by id
thyme = time.Now()
testThatHttp.Request("get", "/customer/", ":id", cu.Id+"?key="+apiKey, GetUserById, nil, "")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
So(err, ShouldBeNil)
So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})
//test change user password
form = url.Values{"email": {"[email protected]"}, "oldPass": {"robthepoor"}, "newPass": {"prolife"}}
v = form.Encode()
body = strings.NewReader(v)
thyme = time.Now()
testThatHttp.Request("post", "/customer/user/changePassword", "", "?key="+apiKey, ChangePassword, body, "application/x-www-form-urlencoded")
So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*2)
So(testThatHttp.Response.Code, ShouldEqual, 200)
var result string
err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &result)
So(err, ShouldBeNil)
So(result, ShouldHaveSameTypeAs, "Success")
//test reset user password
//.........這裏部分代碼省略.........