refactors data access layer to use pop ORM for eager loading, adds book titles rented to renters in response body

This commit is contained in:
David Lick
2020-05-31 00:40:32 -04:00
parent a06b4d70ab
commit 28e52a4e08
29 changed files with 732 additions and 287 deletions
+6 -5
View File
@@ -10,9 +10,10 @@ import (
"time"
"github.com/davidlick/bookish/bookish-server/cmd/http"
"github.com/davidlick/bookish/bookish-server/inventory"
"github.com/davidlick/bookish/bookish-server/mysql"
"github.com/davidlick/bookish/bookish-server/rental"
"github.com/davidlick/bookish/bookish-server/renter"
_ "github.com/go-sql-driver/mysql"
"github.com/sirupsen/logrus"
)
@@ -43,7 +44,7 @@ func init() {
func main() {
// Connect to application database and create store.
appDSN := fmt.Sprintf("%s:%s@tcp(%s:%s)/bookish?parseTime=True&loc=Local",
appDSN := fmt.Sprintf("mysql://%s:%s@tcp(%s:%s)/bookish?parseTime=true&loc=local&multiStatements=true",
cfg.DBUser,
cfg.DBPass,
cfg.DBHost,
@@ -56,15 +57,15 @@ func main() {
// Build domain services.
renterService := renter.NewService(appDB)
inventoryService := inventory.NewService(appDB)
rentalService := rental.NewService(appDB)
// Initialize server.
server := http.Server{
Port: cfg.APIPort,
BooksHost: cfg.BooksHost,
Logger: logger,
Renter: renterService,
Inventory: inventoryService,
Renters: renterService,
Rentals: rentalService,
}
// Create channels to listen for OS signals.
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"time"
bookish "github.com/davidlick/bookish/bookish-server"
"github.com/davidlick/bookish/bookish-server/inventory"
"github.com/davidlick/bookish/bookish-server/rental"
"github.com/davidlick/bookish/bookish-server/renter"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
@@ -21,8 +21,8 @@ type Server struct {
Port string
BooksHost string
Logger *logrus.Logger
Renter renter.Service
Inventory inventory.Service
Renters renter.Service
Rentals rental.Service
// LibraryMap holds a map of values that are returned by the books API. This is used to check
// if a book exists in the library.
LibraryMap struct {
+1 -8
View File
@@ -6,7 +6,6 @@ import (
"errors"
"io/ioutil"
"net/http"
"strconv"
"github.com/davidlick/bookish/bookish-server/internal"
"github.com/davidlick/bookish/bookish-server/mysql"
@@ -36,13 +35,7 @@ func (s *Server) renterCtx(next http.Handler) http.Handler {
ctx := r.Context()
id := chi.URLParam(r, "renterId")
renterId, err := strconv.Atoi(id)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
renter, err := s.Renter.FetchRenter(renterId)
renter, err := s.Renters.FetchRenter(id)
if err != nil && errors.Is(mysql.ErrNotFound, err) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
@@ -2,12 +2,13 @@ package http
import (
"errors"
"fmt"
"net/http"
bookish "github.com/davidlick/bookish/bookish-server"
"github.com/davidlick/bookish/bookish-server/internal"
"github.com/davidlick/bookish/bookish-server/inventory"
"github.com/davidlick/bookish/bookish-server/mysql"
"github.com/davidlick/bookish/bookish-server/rental"
"github.com/gobuffalo/uuid"
)
// checkoutBook is used to checkout a book for a renter.
@@ -25,10 +26,16 @@ func (s *Server) checkoutBook(w http.ResponseWriter, r *http.Request) {
return
}
err := s.Inventory.CheckoutBook(renter.ID, bookTitle)
id, err := uuid.FromString(renter.ID)
if err != nil {
if errors.Is(inventory.ErrUnavailableBook, err) {
http.Error(w, err.Error(), http.StatusForbidden)
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
err = s.Rentals.CheckoutBook(id, bookTitle)
if err != nil {
if errors.Is(rental.ErrUnavailableBook, err) {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
@@ -52,9 +59,17 @@ func (s *Server) returnBook(w http.ResponseWriter, r *http.Request) {
return
}
err := s.Inventory.ReturnBook(renter.ID, bookTitle)
id, err := uuid.FromString(renter.ID)
if err != nil {
fmt.Println(err)
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
err = s.Rentals.ReturnBook(id, bookTitle)
if err != nil && errors.Is(mysql.ErrNotFound, err) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
} else if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
+4 -3
View File
@@ -7,6 +7,7 @@ import (
bookish "github.com/davidlick/bookish/bookish-server"
"github.com/davidlick/bookish/bookish-server/internal"
"github.com/gofrs/uuid"
)
// registerRenter creates a record of a new renter.
@@ -24,14 +25,14 @@ func (s *Server) registerRenter(w http.ResponseWriter, r *http.Request) {
return
}
id, err := s.Renter.RegisterRenter(renter.Name, renter.Address, renter.Email, renter.PhoneNumber)
id, err := s.Renters.RegisterRenter(renter.Name, renter.Address, renter.Email, renter.PhoneNumber)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
resp := struct {
ID int `json:"renterId"`
ID uuid.UUID `json:"renterId"`
}{ID: id}
json.NewEncoder(w).Encode(resp)
@@ -39,7 +40,7 @@ func (s *Server) registerRenter(w http.ResponseWriter, r *http.Request) {
// listRenters lists all renters registered in the API.
func (s *Server) listRenters(w http.ResponseWriter, r *http.Request) {
rr, err := s.Renter.ListRenters()
rr, err := s.Renters.ListRenters()
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return