diff --git a/Dockerfile b/Dockerfile index b13ec72..7769ffe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM debian:latest # Update the package list and install MySQL client RUN apt-get update && apt-get install -y \ - mariadb-client \ + mariadb-client ca-certificates \ && rm -rf /var/lib/apt/lists/* # Copy the preorder binary from the local directory into the container diff --git a/orders/controllers.go b/orders/controllers.go index 9f77fef..f1ca16b 100644 --- a/orders/controllers.go +++ b/orders/controllers.go @@ -24,6 +24,21 @@ func FindOrder(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"data": order}) } +func FindOrderCover(c *gin.Context) { + var order Order + + if err := config.DB.Where("id = ?", c.Param("id")).First(&order).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + } + cover, err := order.fetchCoverImageURL() + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"cover_url": cover}) +} + func CreateOrder(c *gin.Context) { var input CreateOrderInput if err := c.ShouldBindJSON(&input); err != nil { diff --git a/orders/models.go b/orders/models.go index d44190c..4b6df0b 100644 --- a/orders/models.go +++ b/orders/models.go @@ -1,11 +1,17 @@ package orders import ( + "encoding/json" + "io" + "net/http" "preorder/authors" "preorder/formats" + "strconv" "time" ) +const googleBooksAPI = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + type Order struct { ID uint `json:"id" gorm:"primary_key"` Title string `json:"title"` @@ -30,14 +36,38 @@ type UpdateOrderInput struct { Title string `json:"title"` } -func NewOrder(id uint, title string, author uint, format uint, isbn_13 uint, release_date time.Time) Order { +func NewOrder(id uint, title string, author uint, format uint, isbn13 uint, releaseDate time.Time) Order { order := Order{ ID: id, Title: title, AuthorID: author, FormatID: format, - ISBN13: isbn_13, - ReleaseDate: release_date, + ISBN13: isbn13, + ReleaseDate: releaseDate, } return order } + +func (o *Order) fetchCoverImageURL() (string, error) { + resp, err := http.Get(googleBooksAPI + strconv.Itoa(int(o.ISBN13))) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var googleBooksResponse GoogleBooksResponse + if err := json.Unmarshal(body, &googleBooksResponse); err != nil { + return "", err + } + + if len(googleBooksResponse.Items) > 0 { + return googleBooksResponse.Items[0].VolumeInfo.ImageLinks.Thumbnail, nil + } + + return "", nil +} diff --git a/orders/router.go b/orders/router.go index 29c5c99..6d1d457 100644 --- a/orders/router.go +++ b/orders/router.go @@ -4,6 +4,7 @@ import "github.com/gin-gonic/gin" func ApplyOrderRouter(router *gin.Engine) *gin.Engine { router.GET("/orders", FindOrders) + router.GET("/orders/:id/cover", FindOrderCover) router.POST("/orders", CreateOrder) router.GET("/orders/:id", FindOrder) router.PATCH("/orders/:id", UpdateOrder) diff --git a/orders/types.go b/orders/types.go new file mode 100644 index 0000000..f722c82 --- /dev/null +++ b/orders/types.go @@ -0,0 +1,11 @@ +package orders + +type GoogleBooksResponse struct { + Items []struct { + VolumeInfo struct { + ImageLinks struct { + Thumbnail string `json:"thumbnail"` + } `json:"imageLinks"` + } `json:"volumeInfo"` + } `json:"items"` +} diff --git a/test.http b/test.http index 68c755e..ba0c99c 100644 --- a/test.http +++ b/test.http @@ -1,3 +1,7 @@ ### GET http://localhost:8123/orders Content-Type: application/json + +### +GET http://localhost:8123/orders/1/cover +Content-Type: application/json