Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #38 from adamdecaf/moovhttp
Browse files Browse the repository at this point in the history
all: use github.com/moov-io/base
  • Loading branch information
adamdecaf authored Dec 13, 2018
2 parents 00065c3 + 0acfc56 commit fa9030f
Show file tree
Hide file tree
Showing 25 changed files with 470 additions and 263 deletions.
42 changes: 22 additions & 20 deletions customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"time"

moovhttp "github.com/moov-io/base/http"

"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -128,18 +130,18 @@ func getUserCustomers(customerRepo customerRepository) http.HandlerFunc {
return
}

userId := getUserId(r)
userId := moovhttp.GetUserId(r)
customers, err := customerRepo.getUserCustomers(userId)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(customers); err != nil {
internalError(w, err, "getUserCustomers")
internalError(w, err)
return
}
}
Expand Down Expand Up @@ -169,13 +171,13 @@ func createUserCustomer(customerRepo customerRepository, depositoryRepo deposito

req, err := readCustomerRequest(r)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

userId := getUserId(r)
userId := moovhttp.GetUserId(r)
if !depositoryIdExists(userId, req.DefaultDepository, depositoryRepo) {
encodeError(w, fmt.Errorf("Depository %s does not exist", req.DefaultDepository))
moovhttp.Problem(w, fmt.Errorf("Depository %s does not exist", req.DefaultDepository))
return
}

Expand All @@ -189,19 +191,19 @@ func createUserCustomer(customerRepo customerRepository, depositoryRepo deposito
Created: time.Now(),
}
if err := customer.validate(); err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}
if err := customerRepo.upsertUserCustomer(userId, customer); err != nil {
internalError(w, fmt.Errorf("creating customer=%q, user_id=%q", customer.ID, userId), "customers")
internalError(w, fmt.Errorf("creating customer=%q, user_id=%q", customer.ID, userId))
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(customer); err != nil {
internalError(w, err, "createUserCustomer")
internalError(w, err)
return
}
}
Expand All @@ -214,23 +216,23 @@ func getUserCustomer(customerRepo customerRepository) http.HandlerFunc {
return
}

id, userId := getCustomerId(r), getUserId(r)
id, userId := getCustomerId(r), moovhttp.GetUserId(r)
if id == "" {
w.WriteHeader(http.StatusNotFound)
return
}

customer, err := customerRepo.getUserCustomer(id, userId)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(customer); err != nil {
internalError(w, err, "getUserCustomer")
internalError(w, err)
return
}
}
Expand All @@ -245,19 +247,19 @@ func updateUserCustomer(customerRepo customerRepository) http.HandlerFunc {

req, err := readCustomerRequest(r)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

id, userId := getCustomerId(r), getUserId(r)
id, userId := getCustomerId(r), moovhttp.GetUserId(r)
if id == "" {
w.WriteHeader(http.StatusNotFound)
return
}

customer, err := customerRepo.getUserCustomer(id, userId)
if err != nil {
internalError(w, fmt.Errorf("problem getting customer=%q, user_id=%q", id, userId), "customers")
internalError(w, fmt.Errorf("problem getting customer=%q, user_id=%q", id, userId))
return
}
if req.DefaultDepository != "" {
Expand All @@ -269,21 +271,21 @@ func updateUserCustomer(customerRepo customerRepository) http.HandlerFunc {
customer.Updated = time.Now()

if err := customer.validate(); err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

// Perform update
if err := customerRepo.upsertUserCustomer(userId, customer); err != nil {
internalError(w, fmt.Errorf("updating customer=%q, user_id=%q", id, userId), "customers")
internalError(w, fmt.Errorf("updating customer=%q, user_id=%q", id, userId))
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(customer); err != nil {
internalError(w, err, "updateUserCustomer")
internalError(w, err)
return
}
}
Expand All @@ -296,14 +298,14 @@ func deleteUserCustomer(customerRepo customerRepository) http.HandlerFunc {
return
}

id, userId := getCustomerId(r), getUserId(r)
id, userId := getCustomerId(r), moovhttp.GetUserId(r)
if id == "" {
w.WriteHeader(http.StatusNotFound)
return
}

if err := customerRepo.deleteUserCustomer(id, userId); err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

Expand Down
40 changes: 21 additions & 19 deletions depositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strings"
"time"

moovhttp "github.com/moov-io/base/http"

"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -189,18 +191,18 @@ func getUserDepositories(depositoryRepo depositoryRepository) http.HandlerFunc {
return
}

userId := getUserId(r)
userId := moovhttp.GetUserId(r)
deposits, err := depositoryRepo.getUserDepositories(userId)
if err != nil {
internalError(w, err, "getUserDepositories")
internalError(w, err)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(deposits); err != nil {
internalError(w, err, "getUserDepositories")
internalError(w, err)
return
}
}
Expand Down Expand Up @@ -233,11 +235,11 @@ func createUserDepository(depositoryRepo depositoryRepository) http.HandlerFunc

req, err := readDepositoryRequest(r)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

userId, now := getUserId(r), time.Now()
userId, now := moovhttp.GetUserId(r), time.Now()
depository := &Depository{
ID: DepositoryID(nextID()),
BankName: req.BankName,
Expand All @@ -254,20 +256,20 @@ func createUserDepository(depositoryRepo depositoryRepository) http.HandlerFunc
}

if err := depository.validate(); err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

if err := depositoryRepo.upsertUserDepository(userId, depository); err != nil {
internalError(w, err, "createUserDepository")
internalError(w, err)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusCreated)

if err := json.NewEncoder(w).Encode(depository); err != nil {
internalError(w, err, "createUserDepository")
internalError(w, err)
return
}
}
Expand All @@ -280,22 +282,22 @@ func getUserDepository(depositoryRepo depositoryRepository) http.HandlerFunc {
return
}

id, userId := getDepositoryId(r), getUserId(r)
id, userId := getDepositoryId(r), moovhttp.GetUserId(r)
if id == "" {
w.WriteHeader(http.StatusNotFound)
return
}
depository, err := depositoryRepo.getUserDepository(id, userId)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(depository); err != nil {
internalError(w, err, "getUserDepository")
internalError(w, err)
return
}
}
Expand All @@ -310,19 +312,19 @@ func updateUserDepository(depositoryRepo depositoryRepository) http.HandlerFunc

req, err := readDepositoryRequest(r)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

id, userId := getDepositoryId(r), getUserId(r)
id, userId := getDepositoryId(r), moovhttp.GetUserId(r)
if id == "" {
w.WriteHeader(http.StatusNotFound)
return
}

depository, err := depositoryRepo.getUserDepository(id, userId)
if err != nil {
internalError(w, err, "depositories")
internalError(w, err)
return
}
if depository == nil {
Expand Down Expand Up @@ -358,20 +360,20 @@ func updateUserDepository(depositoryRepo depositoryRepository) http.HandlerFunc
depository.Updated = time.Now()

if err := depository.validate(); err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

if err := depositoryRepo.upsertUserDepository(userId, depository); err != nil {
internalError(w, err, "updateUserDepository")
internalError(w, err)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(depository); err != nil {
internalError(w, err, "updateUserDepository")
internalError(w, err)
return
}
}
Expand All @@ -384,14 +386,14 @@ func deleteUserDepository(depositoryRepo depositoryRepository) http.HandlerFunc
return
}

id, userId := getDepositoryId(r), getUserId(r)
id, userId := getDepositoryId(r), moovhttp.GetUserId(r)
if id == "" {
w.WriteHeader(http.StatusNotFound)
return
}

if err := depositoryRepo.deleteUserDepository(id, userId); err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

Expand Down
14 changes: 8 additions & 6 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"time"

moovhttp "github.com/moov-io/base/http"

"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -48,18 +50,18 @@ func getUserEvents(eventRepo eventRepository) http.HandlerFunc {
return
}

userId := getUserId(r)
userId := moovhttp.GetUserId(r)
events, err := eventRepo.getUserEvents(userId)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(events); err != nil {
internalError(w, err, "events")
internalError(w, err)
return
}
}
Expand All @@ -72,7 +74,7 @@ func getEventHandler(eventRepo eventRepository) http.HandlerFunc {
return
}

eventId, userId := getEventId(r), getUserId(r)
eventId, userId := getEventId(r), moovhttp.GetUserId(r)
if eventId == "" {
w.WriteHeader(http.StatusBadRequest)
return
Expand All @@ -81,15 +83,15 @@ func getEventHandler(eventRepo eventRepository) http.HandlerFunc {
// grab event
event, err := eventRepo.getEvent(eventId, userId)
if err != nil {
encodeError(w, err)
moovhttp.Problem(w, err)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(event); err != nil {
internalError(w, err, "events")
internalError(w, err)
return
}
}
Expand Down
Loading

0 comments on commit fa9030f

Please sign in to comment.