コード例 #1
0
//we should pass in a pointer to a address map so that we can update the same memory location
func GetAddressesByIds(addressMap map[int64]models.Address) (map[int32]models.Address, error) {
	db, err := GetPostgreSqlDB()
	retMap := make(map[int32]models.Address)
	if err != nil {
		return retMap, errors.New("Could not obtain DB connection.")
	}
	defer db.Close()

	//this is bad, need to add comma
	var addressIds string
	for k, _ := range addressMap {
		t := strconv.FormatInt(k, 10)
		addressIds = helpers.StringConcat(t)
	}

	rows, err := db.Query(`SELECT a.address_id, 
							a.address_guid, 
							a.line_1, 
							a.line_2, 
							a.line_3, 
							a.city, 
							a.postal_code, 
					       	a.country_code_iso_3166 as country_code, 
					       	a.state, 
					       	a.latitude, 
					       	a.longitude, 
					       	s.description as address_status, 
					       	a.created_by_user, 
					       	a.created_timestamp, 
					       	a.modified_by_user, 
					       	a.modified_timestamp
					  FROM contact.address a
					  JOIN contact.address_status s on s.address_status_id = a.address_status_id
					  where address_id in ($1)`, addressIds)

	if err == sql.ErrNoRows {
		return retMap, errors.New("No addresses found.")
	}

	if err != nil {
		log.Printf("%v", err.Error())
		return retMap, errors.New("Could not query database.")
	}

	defer rows.Close()

	for rows.Next() {
		a := models.Address{}
		rows.Scan(
			&a.Address_Id,
			&a.Address_Guid,
			&a.Line_1,
			&a.Line_2,
			&a.Line_3,
			&a.City,
			&a.Postal_Code,
			&a.Country_Code,
			&a.State,
			&a.Latitude,
			&a.Longitude,
			&a.Address_Status,
			&a.Created_By_User,
			&a.Created_Timestamp,
			&a.Modified_By_User,
			&a.Modified_Timestamp)

		retMap[a.Address_Id] = a
	}

	return retMap, nil
}
コード例 #2
0
//GetUrl redirects to the longUrl given the passed in shortUrl. Also inserts a visit record.
func GetDealByGuid(responseWriter http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
	deal, err := repos.GetDealByGuid(mux.Vars(request)["dealGuid"])
	if err != nil {
		http.Error(responseWriter, "Record not found", http.StatusNotFound)
		return
	}

	//geturl
	geturl := helpers.StringConcat("http://192.168.99.100:4900/account/", deal.Account_Guid.String)
	fmt.Print(geturl)
	//api call to get account by accountguid
	res, err := http.Get(geturl)
	//if err != nil {
	//	what to do
	//}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)

	acct := models.AccountResponse{}
	err = json.Unmarshal(body, &acct)
	if err != nil {
		fmt.Println("error:", err)
	}

	contacts := []models.ContactResponse{}
	for _, ac := range acct.AccountContacts {
		acr := models.AccountContactResponse{
			Contact_Guid:       ac.Contact_Guid,
			First_Name:         ac.First_Name,
			Middle_Name:        ac.Middle_Name,
			Last_Name:          ac.Last_Name,
			Email_Address:      ac.Email_Address,
			Mobile_Phone:       ac.Mobile_Phone,
			Other_Phone:        ac.Other_Phone,
			Billing_Address_Id: ac.Billing_Address_Id,
			Mailing_Address_Id: ac.Mailing_Address_Id,
		}
		c := models.ContactResponseFromAccountContactResponse(acr)
		contacts = append(contacts, c)
	}

	addresses := []models.AddressResponse{}
	//todo how to check if object is present
	if acct.Billing_Address.Address_Id > 0 {
		a := acct.Billing_Address
		a.Address_Type = "Billing_Address"
		addresses = append(addresses, a)
	}

	if acct.Mailing_Address.Address_Id > 0 {
		a := acct.Mailing_Address
		a.Address_Type = "Mailing_Address"
		addresses = append(addresses, a)
	}

	if acct.System_Address.Address_Id > 0 {
		a := acct.System_Address
		a.Address_Type = "System_Address"
		addresses = append(addresses, a)
	}

	dealresponse := models.DealResponseFromDeal(deal)
	dealresponse.Contacts = contacts
	dealresponse.Addresses = addresses

	dealJson, err := json.Marshal(dealresponse)
	if err != nil {
		http.Error(responseWriter, "Could not marshall json", http.StatusInternalServerError)
		return
	}
	http.Error(responseWriter, string(dealJson), http.StatusOK)
}