added notification pagination

main
alex 2023-09-03 21:14:30 +02:00
parent 6dbfcec0c5
commit 73e5548e32
8 changed files with 55 additions and 20 deletions

View File

@ -178,7 +178,7 @@ func GetEquipmentDocumentations(stockItemId string, query structs.PageQuery, c *
utils.DbPageQuery(query, utils.DbPageQuery(query,
utils.EquipmentDocumentationsPaginationLimit, utils.EquipmentDocumentationsPaginationLimit,
&documentations, &documentations,
"created_at", "created_at DESC",
"stock_item_id = ?", "stock_item_id = ?",
stockItemId) stockItemId)
@ -196,7 +196,10 @@ func GetEquipmentDocumentations(stockItemId string, query structs.PageQuery, c *
return c.SendStatus(fiber.StatusInternalServerError) return c.SendStatus(fiber.StatusInternalServerError)
} }
} else { } else {
totalPages = utils.GetTotalPages(&documentations, "stock_item_id = ?", stockItemId) totalPages = utils.GetTotalPages(utils.EquipmentDocumentationsPaginationLimit,
&documentations,
"stock_item_id = ?",
stockItemId)
} }
logger.AddSystemLog(structs.LogMessage{ logger.AddSystemLog(structs.LogMessage{

View File

@ -958,8 +958,11 @@ func StartGroupTask(userId string, groupTask structs.GroupTasks) {
GroupTask structs.GroupTasks GroupTask structs.GroupTasks
TotalPages int TotalPages int
}{ }{
GroupTask: groupTask, GroupTask: groupTask,
TotalPages: utils.GetTotalPages([]structs.GroupTasks{}, "category = ?", groupTask.Category), TotalPages: utils.GetTotalPages(utils.GroupTasksPaginationLimit,
[]structs.GroupTasks{},
"category = ?",
groupTask.Category),
}, },
}) })

View File

@ -9,7 +9,6 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/rs/zerolog/log"
) )
func GetTotalNotifications(userId string) int { func GetTotalNotifications(userId string) int {
@ -20,17 +19,20 @@ func GetTotalNotifications(userId string) int {
return int(totalNotifications) return int(totalNotifications)
} }
func GetNotifications(userId string) []structs.Notification { func GetNotifications(query structs.PageQuery, userId string) []structs.Notification {
var notifications []structs.Notification var notifications []structs.Notification
database.DB.Model(&structs.Notification{}).Where("user_id = ?", userId).Find(&notifications) utils.DbPageQuery(query,
utils.NotificationsPaginationLimit,
&notifications,
"created_at DESC",
"user_id = ?",
userId)
return notifications return notifications
} }
func AddNotification(c *fiber.Ctx, body structs.AddNotificationRequest) error { func AddNotification(c *fiber.Ctx, body structs.AddNotificationRequest) error {
log.Info().Msgf("body %v", body)
var userIds []string var userIds []string
if len(body.UserIds) == 0 && body.NeededPermission == "" && len(body.RoleIds) == 0 { if len(body.UserIds) == 0 && body.NeededPermission == "" && len(body.RoleIds) == 0 {
@ -85,13 +87,20 @@ func AddNotification(c *fiber.Ctx, body structs.AddNotificationRequest) error {
// send notification to user // send notification to user
socketclients.SendMessageToUser(userId, "", structs.SendSocketMessage{ socketclients.SendMessageToUser(userId, "", structs.SendSocketMessage{
Cmd: utils.SentCmdNewNotification, Cmd: utils.SentCmdNewNotification,
Body: notify, Body: struct {
Notification structs.Notification
TotalPages int
}{
Notification: notify,
TotalPages: utils.GetTotalPages(utils.NotificationsPaginationLimit,
[]structs.Notification{},
"user_id = ?",
userId),
},
}) })
} }
log.Info().Msgf("ids %v", userIds)
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)
} }

View File

@ -26,4 +26,5 @@ type AddNotificationRequest struct {
// swagger:model NotificationsResponse // swagger:model NotificationsResponse
type NotificationsResponse struct { type NotificationsResponse struct {
Notifications []Notification Notifications []Notification
TotalPages int
} }

View File

@ -38,6 +38,7 @@ const (
EquipmentDocumentationsPaginationLimit = 3 EquipmentDocumentationsPaginationLimit = 3
GroupTasksPaginationLimit = 5 GroupTasksPaginationLimit = 5
NotificationsPaginationLimit = 10
) )
var ( var (

View File

@ -116,23 +116,23 @@ func QueryParserHelper(c *fiber.Ctx, query interface{}) error {
// GetTotalPages returns total pages for pagination // GetTotalPages returns total pages for pagination
// Example whereQuery = "stock_item_id = ?" and args = stockItemId is Where("stock_item_id = ?", stockItemId) // Example whereQuery = "stock_item_id = ?" and args = stockItemId is Where("stock_item_id = ?", stockItemId)
func GetTotalPages(any interface{}, whereQuery interface{}, args ...interface{}) int { func GetTotalPages(paginationLimit int, any interface{}, whereQuery interface{}, args ...interface{}) int {
var totalPages int64 var totalPages int64
database.DB.Model(any). database.DB.Model(any).
Where(whereQuery, args). Where(whereQuery, args).
Count(&totalPages) Count(&totalPages)
return int(math.Ceil(float64(totalPages) / float64(EquipmentDocumentationsPaginationLimit))) return int(math.Ceil(float64(totalPages) / float64(paginationLimit)))
} }
func GetPageOffset(page int) int { func GetPageOffset(page int, paginationLimit int) int {
return (page - 1) * EquipmentDocumentationsPaginationLimit return (page - 1) * paginationLimit
} }
func DbPageQuery(query structs.PageQuery, paginationLimit int, result any, orderBy, whereQuery interface{}, args ...interface{}) *gorm.DB { func DbPageQuery(query structs.PageQuery, paginationLimit int, result any, orderBy, whereQuery interface{}, args ...interface{}) *gorm.DB {
return database.DB.Limit(paginationLimit). return database.DB.Limit(paginationLimit).
Offset(GetPageOffset(query.Page)). Offset(GetPageOffset(query.Page, paginationLimit)).
Where(whereQuery, args). Where(whereQuery, args).
Order(orderBy). Order(orderBy).
Find(result) Find(result)

View File

@ -70,7 +70,10 @@ func GetGroupTasks(c *fiber.Ctx) error {
return c.JSON(structs.GroupTasksResponse{ return c.JSON(structs.GroupTasksResponse{
CategoryGroup: categoryGroup, CategoryGroup: categoryGroup,
GroupTasks: grouptasks.GetAllGroupTasks(params.Category, query), GroupTasks: grouptasks.GetAllGroupTasks(params.Category, query),
TotalPages: utils.GetTotalPages([]structs.GroupTasks{}, "category = ?", params.Category), TotalPages: utils.GetTotalPages(utils.GroupTasksPaginationLimit,
[]structs.GroupTasks{},
"category = ?",
params.Category),
}) })
} }

View File

@ -57,6 +57,9 @@ func GetNotifications(c *fiber.Ctx) error {
// - name: X-Api-Key // - name: X-Api-Key
// in: header // in: header
// description: You can create a new api key in your user profile // description: You can create a new api key in your user profile
// - name: page
// in: query
// description: Page number
// responses: // responses:
// '200': // '200':
// description: Notifications // description: Notifications
@ -67,7 +70,19 @@ func GetNotifications(c *fiber.Ctx) error {
// '500': // '500':
// description: Failed to get notifications // description: Failed to get notifications
var query structs.PageQuery
if err := utils.QueryParserHelper(c, &query); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
userId := c.Locals("userId").(string)
return c.JSON(structs.NotificationsResponse{ return c.JSON(structs.NotificationsResponse{
Notifications: notification.GetNotifications(c.Locals("userId").(string)), Notifications: notification.GetNotifications(query, userId),
TotalPages: utils.GetTotalPages(utils.NotificationsPaginationLimit,
[]structs.Notification{},
"user_id = ?",
userId),
}) })
} }