Skip to content

Commit

Permalink
Errors are handled properly, additional logging and starting on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David Smith committed Jun 26, 2017
1 parent 4194147 commit 8676c0c
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 427 deletions.
38 changes: 33 additions & 5 deletions concept/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"encoding/json"

"github.com/Financial-Times/aggregate-concept-transformer/sqs"
fthealth "github.com/Financial-Times/go-fthealth/v1_1"
"github.com/Financial-Times/http-handlers-go/httphandlers"
status "github.com/Financial-Times/service-status-go/httphandlers"
Expand All @@ -28,27 +29,54 @@ func NewHandler(svc Service) AggregateConceptHandler {
return AggregateConceptHandler{svc: svc}
}

func (h *AggregateConceptHandler) GetHandler(rw http.ResponseWriter, r *http.Request) {
func (h *AggregateConceptHandler) GetHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
UUID := vars["uuid"]

concordedConcept, transactionID, err := h.svc.GetConcordedConcept(UUID)
if err != nil {
//Do error stuff
log.WithError(err).Error("An error getting the concept occurred. Naughty system.")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Could not load the full concept"))
return
}

rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("X-Request-Id", transactionID)
rw.WriteHeader(http.StatusOK)
json.NewEncoder(rw).Encode(concordedConcept)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Request-Id", transactionID)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(concordedConcept)
}

func (h *AggregateConceptHandler) SendHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
UUID := vars["uuid"]

err := h.svc.ProcessMessage(sqs.Notification{
UUID: UUID,
ReceiptHandle: nil,
})

if err != nil {
log.WithError(err).Error("An error processing the concept occurred. Naughty system.")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Could not process the concept"))
return
}
}

func (h *AggregateConceptHandler) RegisterHandlers(router *mux.Router) {
log.Info("Registering handlers")
mh := handlers.MethodHandler{
"GET": http.HandlerFunc(h.GetHandler),
}
sh := handlers.MethodHandler{
"POST": http.HandlerFunc(h.SendHandler),
}
router.Handle("/concept/{uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}", mh)
router.Handle("/concept/{uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}/send", sh)
}

func (h *AggregateConceptHandler) RegisterAdminHandlers(router *mux.Router, healthService *HealthService) {
Expand Down

0 comments on commit 8676c0c

Please sign in to comment.