Пример #1
0
//AddNewAddressToUser ... Add address to a user profile
func (client *ClientController) AddNewAddressToUser(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {

	render := render.New(render.Options{})
	address := models.Addresses{}
	errs := binding.Bind(req, &address)
	userlogic.ServiceList = client.ServiceList
	userlogic.Logger = client.Logger
	userlogic.AuthorizationToken = req.Header.Get("Authorization")
	if errs.Handle(res) {
		client.Logger.Crit(errs.Error())
		render.JSON(res, 422, errs.Error())
		return
	}
	bindingErr := address.Validate(req, errs)
	if bindingErr != nil {
		client.Logger.Crit(bindingErr.Error())
		render.JSON(res, 422, bindingErr.Error())
		return
	}

	savedEntity, bSave, err := userlogic.AddNewAddressToUser(address)
	if !bSave && err != nil {
		client.Logger.Error(err.Error())
		panic(err)
	}
	render.JSON(res, 200, savedEntity)
}
Пример #2
0
// CreateAddress ... add new address to a person
func (c PersonController) CreateAddress(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	r := render.New(render.Options{})
	address := new(models.Addresses)
	errs := binding.Bind(req, address)
	bs, _ := ioutil.ReadAll(req.Body)
	if errs.Handle(res) {
		c.Logger.Error(fmt.Sprintf("%s, %s", errs.Error(), string(bs)))
		r.JSON(res, 422, errs.Error())
		return
	}
	bindingErr := address.Validate(req, errs)
	if bindingErr != nil {
		r.JSON(res, 422, bindingErr.Error())
		return
	}

	p := &models.Addresses{
		BaseModel:         c.BaseModel,
		AddressType:       address.AddressType,
		HouseNo:           address.HouseNo,
		Street:            address.Street,
		Area:              address.Area,
		TownsID:           address.TownsID,
		RegionStateID:     address.RegionStateID,
		CountryID:         address.CountryID,
		PersonID:          address.PersonID,
		CompanyEntitiesID: address.CompanyEntitiesID}

	err := c.DataStore.SaveDatabaseObject(p)
	if err != nil {
		panic(err)
	}
	c.Logger.Info(fmt.Sprintf("%v", &p))
	r.JSON(res, 200, p)
}