add cover url route
This commit is contained in:
parent
231fce5077
commit
4b3467b8b8
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
11
orders/types.go
Normal file
11
orders/types.go
Normal file
@ -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"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user