50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/luxsin/app-api/internal/repository"
|
|
"github.com/luxsin/app-api/internal/response"
|
|
"github.com/luxsin/app-api/pkg/encode"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type BrandHandler struct {
|
|
repo *repository.BrandRepository
|
|
log *zap.Logger
|
|
}
|
|
|
|
func NewBrandHandler(db *sql.DB, log *zap.Logger) *BrandHandler {
|
|
return &BrandHandler{
|
|
repo: repository.NewBrandRepository(db),
|
|
log: log,
|
|
}
|
|
}
|
|
|
|
func (h *BrandHandler) GetBrand(c *gin.Context) {
|
|
brandName := c.Query("brandName")
|
|
base64Resp := encode.ParseBase64Param(c)
|
|
|
|
list, err := h.repo.List(c.Request.Context(), brandName)
|
|
if err != nil {
|
|
h.log.Error("get brand list failed", zap.Error(err))
|
|
response.InternalError(c, "failed to get brand list")
|
|
return
|
|
}
|
|
|
|
if base64Resp {
|
|
encoded, err := encode.EncodeJSON(list)
|
|
if err != nil {
|
|
h.log.Error("encode response failed", zap.Error(err))
|
|
response.InternalError(c, "failed to encode response")
|
|
return
|
|
}
|
|
c.String(http.StatusOK, encoded)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, list)
|
|
}
|