From 258035833b2541f8c403fe3d5d09da72b352c975 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman Date: Fri, 2 Aug 2019 09:58:30 -0500 Subject: [PATCH] Add GetAddressById --- .../repositories/address_repository.go | 9 +++++++++ .../repositories/address_repository_test.go | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/datastore/postgres/repositories/address_repository.go b/pkg/datastore/postgres/repositories/address_repository.go index 7e9d4724..950fe50c 100644 --- a/pkg/datastore/postgres/repositories/address_repository.go +++ b/pkg/datastore/postgres/repositories/address_repository.go @@ -50,3 +50,12 @@ func (repo AddressRepository) GetOrCreateAddressInTransaction(tx *sqlx.Tx, addre return addressId, getErr } + +func (repo AddressRepository) GetAddressById(db *postgres.DB, id int) (string, error){ + var address string + getErr := db.Get(&address, `SELECT address FROM public.addresses WHERE id = $1`, id) + if getErr != nil { + return "", getErr + } + return address, nil +} diff --git a/pkg/datastore/postgres/repositories/address_repository_test.go b/pkg/datastore/postgres/repositories/address_repository_test.go index b370ea18..c0f3fa82 100644 --- a/pkg/datastore/postgres/repositories/address_repository_test.go +++ b/pkg/datastore/postgres/repositories/address_repository_test.go @@ -146,4 +146,22 @@ var _ = Describe("address lookup", func() { Expect(upperAddressId).To(Equal(mixedCaseAddressId)) }) }) + + Describe("GetAddressById", func() { + It("gets and address by it's id", func() { + addressId, createErr := repo.GetOrCreateAddress(db, address) + Expect(createErr).NotTo(HaveOccurred()) + + actualAddress, getErr := repo.GetAddressById(db, addressId) + Expect(getErr).NotTo(HaveOccurred()) + Expect(actualAddress).To(Equal(address)) + }) + + It("returns an error if the id doesn't exist", func() { + _, getErr := repo.GetAddressById(db, 0) + Expect(getErr).To(HaveOccurred()) + Expect(getErr).To(MatchError("sql: no rows in result set")) + }) + }) }) +