Skip to content

Commit

Permalink
Service endpoint error handling and automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Freund committed Oct 31, 2024
1 parent 52d55bf commit eccd75f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
7 changes: 7 additions & 0 deletions cmd/sheltertech-go/services_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func TestGetServiceByID(t *testing.T) {
assert.Equal(t, serviceResponse.Service.Id, serviceId, "Service Id is a match")
}

func TestGetServiceByIDWithInvalidID(t *testing.T) {
serviceId := "foo"

res, _ := http.Get(serviceUrl + "/" + serviceId)
assert.Equal(t, res.StatusCode, http.StatusBadRequest, "Invalid service ID returns bad request")
}

func TestPostServicesChangeRequest(t *testing.T) {
url := "http://localhost:3001/api/services/1/change_request"

Expand Down
2 changes: 1 addition & 1 deletion internal/changerequest/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (m *Manager) Submit(w http.ResponseWriter, r *http.Request) {
var service *db.Service
switch changeRequest.Type {
case "ServiceChangeRequest":
service = m.DbClient.GetServiceById(changeRequest.ObjectID)
service, err = m.DbClient.GetServiceById(changeRequest.ObjectID)
break
}
if err != nil || service == nil {
Expand Down
15 changes: 3 additions & 12 deletions internal/db/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ FROM public.services
WHERE resource_id = $1 and status = 1
`

func (m *Manager) GetServiceById(serviceId int) *Service {
func (m *Manager) GetServiceById(serviceId int) (*Service, error) {
row := m.DB.QueryRow(serviceByIDSql, serviceId)
return scanService(row)
}
Expand Down Expand Up @@ -78,17 +78,8 @@ func scanServices(rows *sql.Rows) []*Service {
return services
}

func scanService(row *sql.Row) *Service {
func scanService(row *sql.Row) (*Service, error) {
var service Service
err := row.Scan(&service.Id, &service.CreatedAt, &service.UpdatedAt, &service.Name, &service.LongDescription, &service.Eligibility, &service.RequiredDocuments, &service.Fee, &service.ApplicationProcess, &service.ResourceId, &service.VerifiedAt, &service.Email, &service.Status, &service.Certified, &service.ProgramId, &service.InterpretationServices, &service.Url, &service.WaitTime, &service.ContactId, &service.FundingId, &service.AlternateName, &service.CertifiedAt, &service.Featured, &service.SourceAttribution, &service.InternalNote, &service.ShortDescription)
if err != nil {
switch err {
case sql.ErrNoRows:
fmt.Println("No rows were returned!")
return nil
default:
panic(err)
}
}
return &service
return &service, err
}
7 changes: 6 additions & 1 deletion internal/services/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"log"
"net/http"
"strconv"
"github.com/sheltertechsf/sheltertech-go/internal/common"
)

type Manager struct {
Expand All @@ -44,8 +45,10 @@ func (m *Manager) GetByID(w http.ResponseWriter, r *http.Request) {
serviceId, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
log.Printf("%v", err)
common.WriteErrorJson(w, http.StatusBadRequest, err.Error())
return
}
dbService := m.DbClient.GetServiceById(serviceId)
dbService, err := m.DbClient.GetServiceById(serviceId)
response := FromDBType(dbService)
response.Categories = categories.FromDBTypeArray(m.DbClient.GetCategoriesByServiceID(serviceId))
response.Notes = notes.FromNoteDBTypeArray(m.DbClient.GetNotesByServiceID(serviceId))
Expand Down Expand Up @@ -77,6 +80,8 @@ func writeJson(w http.ResponseWriter, object interface{}) {
output, err := json.Marshal(object)
if err != nil {
fmt.Println("error:", err)
common.WriteErrorJson(w, http.StatusInternalServerError, common.InternalServerErrorMessage)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit eccd75f

Please sign in to comment.