Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: refactor notification page list

LinkinStar863a9d91ca14
diff --git a/internal/controller/notification_controller.go b/internal/controller/notification_controller.go
index aa173be3..7c3afdae 100644
--- a/internal/controller/notification_controller.go
+++ b/internal/controller/notification_controller.go
@@ -111,6 +111,6 @@ func (nc *NotificationController) GetList(ctx *gin.Context) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
- resp, err := nc.notificationService.GetList(ctx, req)
+ resp, err := nc.notificationService.GetNotificationPage(ctx, req)
handler.HandleResponse(ctx, err, resp)
}
diff --git a/internal/repo/notification/notification_repo.go b/internal/repo/notification/notification_repo.go
index deaf431a..37c9337a 100644
--- a/internal/repo/notification/notification_repo.go
+++ b/internal/repo/notification/notification_repo.go
@@ -4,8 +4,8 @@ import (
"context"
"time"
- "github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
+ "github.com/answerdev/answer/internal/base/pager"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
@@ -29,8 +29,7 @@ func NewNotificationRepo(data *data.Data) notficationcommon.NotificationRepo {
func (nr *notificationRepo) AddNotification(ctx context.Context, notification *entity.Notification) (err error) {
_, err = nr.data.DB.Insert(notification)
if err != nil {
- err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
- return
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
@@ -40,8 +39,7 @@ func (nr *notificationRepo) UpdateNotificationContent(ctx context.Context, notif
notification.UpdatedAt = now
_, err = nr.data.DB.Where("id =?", notification.ID).Cols("content", "updated_at").Update(notification)
if err != nil {
- err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
- return
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
@@ -51,8 +49,7 @@ func (nr *notificationRepo) ClearUnRead(ctx context.Context, userID string, noti
info.IsRead = schema.NotificationRead
_, err = nr.data.DB.Where("user_id =?", userID).And("type =?", notificationType).Cols("is_read").Update(info)
if err != nil {
- err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
- return
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
@@ -62,8 +59,7 @@ func (nr *notificationRepo) ClearIDUnRead(ctx context.Context, userID string, id
info.IsRead = schema.NotificationRead
_, err = nr.data.DB.Where("user_id =?", userID).And("id =?", id).Cols("is_read").Update(info)
if err != nil {
- err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
- return
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
@@ -88,32 +84,22 @@ func (nr *notificationRepo) GetByUserIdObjectIdTypeId(ctx context.Context, userI
return info, exist, nil
}
-func (nr *notificationRepo) SearchList(ctx context.Context, search *schema.NotificationSearch) ([]*entity.Notification, int64, error) {
- var count int64
- var err error
-
- rows := make([]*entity.Notification, 0)
- if search.UserID == "" {
- return rows, 0, nil
- }
- if search.Page > 0 {
- search.Page = search.Page - 1
- } else {
- search.Page = 0
+func (nr *notificationRepo) GetNotificationPage(ctx context.Context, searchCond *schema.NotificationSearch) (
+ notificationList []*entity.Notification, total int64, err error) {
+ notificationList = make([]*entity.Notification, 0)
+ if searchCond.UserID == "" {
+ return notificationList, 0, nil
}
- if search.PageSize == 0 {
- search.PageSize = constant.Default_PageSize
+
+ session := nr.data.DB.NewSession()
+ session = session.Desc("updated_at")
+ cond := &entity.Notification{
+ UserID: searchCond.UserID,
+ Type: searchCond.Type,
}
- offset := search.Page * search.PageSize
- session := nr.data.DB.Where("")
- session = session.And("user_id = ?", search.UserID)
- session = session.And("type = ?", search.Type)
- session = session.OrderBy("updated_at desc")
- session = session.Limit(search.PageSize, offset)
- count, err = session.FindAndCount(&rows)
+ total, err = pager.Help(searchCond.Page, searchCond.PageSize, &notificationList, cond, session)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
- return rows, count, err
}
- return rows, count, nil
+ return
}
diff --git a/internal/service/notification/notification_service.go b/internal/service/notification/notification_service.go
index b8a09976..f0a9cc1f 100644
--- a/internal/service/notification/notification_service.go
+++ b/internal/service/notification/notification_service.go
@@ -95,15 +95,15 @@ func (ns *NotificationService) ClearIDUnRead(ctx context.Context, userID string,
return nil
}
-func (ns *NotificationService) GetList(ctx context.Context, search *schema.NotificationSearch) (
+func (ns *NotificationService) GetNotificationPage(ctx context.Context, searchCond *schema.NotificationSearch) (
pageModel *pager.PageModel, err error) {
resp := make([]*schema.NotificationContent, 0)
- searchType, ok := schema.NotificationType[search.TypeStr]
+ searchType, ok := schema.NotificationType[searchCond.TypeStr]
if !ok {
return pager.NewPageModel(0, resp), nil
}
- search.Type = searchType
- notifications, count, err := ns.notificationRepo.SearchList(ctx, search)
+ searchCond.Type = searchType
+ notifications, total, err := ns.notificationRepo.GetNotificationPage(ctx, searchCond)
if err != nil {
return nil, err
}
@@ -123,5 +123,5 @@ func (ns *NotificationService) GetList(ctx context.Context, search *schema.Notif
}
resp = append(resp, item)
}
- return pager.NewPageModel(count, resp), nil
+ return pager.NewPageModel(total, resp), nil
}
diff --git a/internal/service/notification_common/notification.go b/internal/service/notification_common/notification.go
index 3513d0c7..bf5ac725 100644
--- a/internal/service/notification_common/notification.go
+++ b/internal/service/notification_common/notification.go
@@ -22,7 +22,7 @@ import (
type NotificationRepo interface {
AddNotification(ctx context.Context, notification *entity.Notification) (err error)
- SearchList(ctx context.Context, search *schema.NotificationSearch) ([]*entity.Notification, int64, error)
+ GetNotificationPage(ctx context.Context, search *schema.NotificationSearch) ([]*entity.Notification, int64, error)
ClearUnRead(ctx context.Context, userID string, notificationType int) (err error)
ClearIDUnRead(ctx context.Context, userID string, id string) (err error)
GetByUserIdObjectIdTypeId(ctx context.Context, userID, objectID string, notificationType int) (*entity.Notification, bool, error)