hiling_go/api/image.go
2023-09-19 21:49:48 +07:00

45 lines
1.1 KiB
Go

package api
import (
"net/http"
db "git.nochill.in/nochill/hiling_go/db/sqlc"
"github.com/gin-gonic/gin"
)
type getAllImagesReq struct {
Page int32 `form:"page" binding:"required,min=1"`
PageSize int32 `form:"page_size" binding:"required,min=5"`
LocationId int32 `form:"location_id" binding:"required,numeric"`
}
func (server *Server) getAllImagesByLocation(ctx *gin.Context) {
var req getAllImagesReq
if err := ctx.BindQuery(&req); err != nil {
ctx.JSON(http.StatusBadRequest, ValidationErrorResponse(err))
return
}
arg := db.GetImagesByLocationParams{
Limit: req.PageSize,
Offset: (req.Page - 1) * req.PageSize,
LocationId: req.LocationId,
}
count, err := server.Store.GetCountImageByLocation(ctx, arg.LocationId)
if err != nil {
ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong"))
return
}
images, err := server.Store.GetImagesByLocation(ctx, arg)
if err != nil {
ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong"))
return
}
ctx.JSON(http.StatusOK, gin.H{
"total_image": count,
"images": images,
})
}