pagination
parent
7879f2c1c0
commit
497a952a7a
|
@ -172,12 +172,18 @@ func CreateEquipmentDocumentation(c *fiber.Ctx, body structs.CreateEquipmentDocu
|
||||||
return c.JSON(fiber.Map{"message": "ok"})
|
return c.JSON(fiber.Map{"message": "ok"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetEquipmentDocumentations(stockItemId string, c *fiber.Ctx) error {
|
func GetEquipmentDocumentations(stockItemId string, query structs.PageQuery, c *fiber.Ctx) error {
|
||||||
var documentations []structs.EquipmentDocumentation
|
var documentations []structs.EquipmentDocumentation
|
||||||
|
|
||||||
database.DB.Where("stock_item_id = ?", stockItemId).Find(&documentations)
|
offset := (query.Page - 1) * utils.EquipmentDocumentationsPaginationLimit
|
||||||
|
|
||||||
|
database.DB.Limit(utils.EquipmentDocumentationsPaginationLimit).
|
||||||
|
Offset(offset).
|
||||||
|
Where("stock_item_id = ?", stockItemId).
|
||||||
|
Find(&documentations)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
totalPages := 0
|
||||||
statusCode := 200
|
statusCode := 200
|
||||||
|
|
||||||
if len(documentations) == 0 {
|
if len(documentations) == 0 {
|
||||||
|
@ -189,6 +195,8 @@ func GetEquipmentDocumentations(stockItemId string, c *fiber.Ctx) error {
|
||||||
log.Error().Msgf("Invex api request error: %s", err)
|
log.Error().Msgf("Invex api request error: %s", err)
|
||||||
return c.SendStatus(fiber.StatusInternalServerError)
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
totalPages = utils.GetTotalPages(&documentations, stockItemId, "stock_item_id = ?", stockItemId)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.AddSystemLog(structs.LogMessage{
|
logger.AddSystemLog(structs.LogMessage{
|
||||||
|
@ -203,7 +211,8 @@ func GetEquipmentDocumentations(stockItemId string, c *fiber.Ctx) error {
|
||||||
|
|
||||||
return c.JSON(structs.EquipmentDocumentationResponse{
|
return c.JSON(structs.EquipmentDocumentationResponse{
|
||||||
Status: statusCode,
|
Status: statusCode,
|
||||||
Documentations: documentations})
|
Documentations: documentations,
|
||||||
|
TotalPages: totalPages})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetEquipmentDocumentation(stockItemId string, documentationId string, c *fiber.Ctx) error {
|
func GetEquipmentDocumentation(stockItemId string, documentationId string, c *fiber.Ctx) error {
|
||||||
|
|
|
@ -18,7 +18,7 @@ type EquipmentDocumentation struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type EquipmentRequest struct {
|
type EquipmentRequest struct {
|
||||||
StockItemId string `json:"stockItemId"`
|
StockItemId string
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetDocumentationEquipmentRequest struct {
|
type GetDocumentationEquipmentRequest struct {
|
||||||
|
@ -38,6 +38,7 @@ type CreateEquipmentDocumentationRequest struct {
|
||||||
type EquipmentDocumentationResponse struct {
|
type EquipmentDocumentationResponse struct {
|
||||||
Status int
|
Status int
|
||||||
Documentations []EquipmentDocumentation
|
Documentations []EquipmentDocumentation
|
||||||
|
TotalPages int
|
||||||
}
|
}
|
||||||
|
|
||||||
// swagger:model EditEquipmentDocumentationRequest
|
// swagger:model EditEquipmentDocumentationRequest
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package structs
|
||||||
|
|
||||||
|
type PageQuery struct {
|
||||||
|
Page int
|
||||||
|
}
|
|
@ -35,6 +35,8 @@ const (
|
||||||
|
|
||||||
ConnectionStateOffline = 0
|
ConnectionStateOffline = 0
|
||||||
ConnectionStateOnline = 1
|
ConnectionStateOnline = 1
|
||||||
|
|
||||||
|
EquipmentDocumentationsPaginationLimit = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"jannex/admin-dashboard-backend/modules/database"
|
||||||
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -95,3 +97,29 @@ func BodyParserHelper(c *fiber.Ctx, body interface{}) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func QueryParserHelper(c *fiber.Ctx, query interface{}) error {
|
||||||
|
if err := c.QueryParser(query); err != nil {
|
||||||
|
log.Error().Msgf("Failed to parse query, err: %s", err.Error())
|
||||||
|
return errors.New("Failed to parse query")
|
||||||
|
}
|
||||||
|
|
||||||
|
if errValidation := ValidateStruct(query); errValidation != nil {
|
||||||
|
log.Error().Msgf("Failed to validate query, err: %v", errValidation)
|
||||||
|
return errors.New("Failed to validate query")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTotalPages returns total pages for pagination
|
||||||
|
// Example Where("stock_item_id = ?", stockItemId) -> whereQuery = "stock_item_id = ?" and whereQuery = stockItemId
|
||||||
|
func GetTotalPages(any interface{}, stockItemId string, whereQuery interface{}, args ...interface{}) int {
|
||||||
|
var totalPages int64
|
||||||
|
|
||||||
|
database.DB.Model(any).
|
||||||
|
Where(whereQuery, args).
|
||||||
|
Count(&totalPages)
|
||||||
|
|
||||||
|
return int(math.Ceil(float64(totalPages) / float64(EquipmentDocumentationsPaginationLimit)))
|
||||||
|
}
|
||||||
|
|
|
@ -87,7 +87,13 @@ func GetEquipmentDocumentations(c *fiber.Ctx) error {
|
||||||
return c.SendStatus(fiber.StatusBadRequest)
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
return equipment.GetEquipmentDocumentations(params.StockItemId, c)
|
var query structs.PageQuery
|
||||||
|
|
||||||
|
if err := utils.QueryParserHelper(c, &query); err != nil {
|
||||||
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
return equipment.GetEquipmentDocumentations(params.StockItemId, query, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetEquipmentDocumentation(c *fiber.Ctx) error {
|
func GetEquipmentDocumentation(c *fiber.Ctx) error {
|
||||||
|
|
Loading…
Reference in New Issue