示例#1
0
// CreateCountry ... create new country
func (c CommonController) CreateCountry(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	r := render.New(render.Options{})
	country := new(models.Country)
	errs := binding.Bind(req, country)
	if errs.Handle(res) {
		r.JSON(res, 422, errs.Error())
		return
	}

	bindingErr := country.Validate(req, errs)

	if bindingErr != nil {
		r.JSON(res, 422, bindingErr.Error())
		return
	}
	// save to database
	p := models.Country{c.BaseModel, country.ISOCode, country.Name, country.RegionStates, country.Language, country.LanguageID}

	err := c.DataStore.SaveDatabaseObject(&p)
	if err != nil {
		fmt.Println(err.Error())
		panic(err)
	} else {
		r.JSON(res, 200, p)
	}

	// render response

}
示例#2
0
// UpdateCountry ... update a merchant by id
func (c CommonController) UpdateCountry(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	r := render.New(render.Options{})
	vars := mux.Vars(req)
	id := vars["id"]

	country := new(models.Country)
	errs := binding.Bind(req, country)
	if errs.Handle(res) {
		r.JSON(res, 422, errs.Error())
		return
	}

	bindingErr := country.Validate(req, errs)

	if bindingErr != nil {
		r.JSON(res, 422, bindingErr.Error())
		return
	}

	queryDict, _ := c.HTTPUtilDunc.DecodeHTTPBody(req)

	ctryExist := &models.Country{}
	qryparam := map[string]interface{}{"id": id}
	err := c.DataStore.FetchFirstGenericObject(qryparam, ctryExist)
	ctryExist.UpdatedAt = time.Now()

	if err != nil && err.ErrNo == 1001 {
		r.JSON(res, 404, err.Error())
	} else if err == nil {
		ctryExist.Name = country.Name
		ctryExist.UpdatedAt = time.Now()
		err := c.DataStore.UpdateDatabaseObject(ctryExist, queryDict)
		if err != nil {
			panic(err)
		} else {
			r.JSON(res, 200, ctryExist)
		}
	} else {
		fmt.Println(err.Error())
		panic(err)
	}
}