作者 linmadan

消息读取按类型

... ... @@ -9,6 +9,8 @@ import (
type ReadAllUnReadSentNotificationCommand struct {
// 通知接收者Uid
ReceiverId int64 `json:"receiverId" valid:"Required"`
// 通知类型
NotificationType int `json:"notificationType"`
}
func (readAllUnReadSentNotificationCommand *ReadAllUnReadSentNotificationCommand) ValidateCommand() error {
... ...
... ... @@ -222,7 +222,7 @@ func (notificationService *NotificationService) ReadAllUnReadSentNotification(re
} else {
readAllUnReadSentNotificationService = value
}
if readCount, err := readAllUnReadSentNotificationService.ReadAll(readAllUnReadSentNotificationCommand.ReceiverId); err != nil {
if readCount, err := readAllUnReadSentNotificationService.ReadAll(readAllUnReadSentNotificationCommand.ReceiverId, readAllUnReadSentNotificationCommand.NotificationType); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
... ...
... ... @@ -6,5 +6,5 @@ import (
type ReadAllUnReadSentNotificationService interface {
coreDomain.DomainEventPublisher
ReadAll(receiverUid int64) (int, error)
ReadAll(receiverUid int64, notificationType int) (int, error)
}
... ...
... ... @@ -11,16 +11,28 @@ type NotificationDao struct {
transactionContext *pgTransaction.TransactionContext
}
func (dao *NotificationDao) ReadAllUnReadSentNotification(receiverId int64) (int, error) {
func (dao *NotificationDao) ReadAllUnReadSentNotification(receiverId int64, notificationType int) (int, error) {
tx := dao.transactionContext.PgTx
result, err := tx.Query(
pg.Scan(),
`UPDATE sent_notifications SET is_read=?, read_time=? WHERE receiver @> '{"uid":?}' AND is_read=false`,
true, time.Now(), receiverId)
if err != nil {
return 0, err
if notificationType == 0 {
result, err := tx.Query(
pg.Scan(),
`UPDATE sent_notifications SET is_read=?, read_time=? WHERE receiver @> '{"uid":?}' AND is_read=false`,
true, time.Now(), receiverId)
if err != nil {
return 0, err
} else {
return result.RowsAffected(), nil
}
} else {
return result.RowsAffected(), nil
result, err := tx.Query(
pg.Scan(),
`UPDATE sent_notifications SET is_read=?, read_time=? WHERE notification_id IN ( SELECT sent_notification.notification_id FROM sent_notifications AS sent_notification LEFT JOIN notifications AS notification ON notification.id = sent_notification.notification_id WHERE sent_notification.receiver @> '{"uid":?}' AND sent_notification.is_read=false AND notification.notification_type=?)`,
true, time.Now(), receiverId, notificationType)
if err != nil {
return 0, err
} else {
return result.RowsAffected(), nil
}
}
}
... ...
... ... @@ -14,7 +14,7 @@ type ReadAllUnReadSentNotificationService struct {
transactionContext *pgTransaction.TransactionContext
}
func (service *ReadAllUnReadSentNotificationService) ReadAll(receiverUid int64) (int, error) {
func (service *ReadAllUnReadSentNotificationService) ReadAll(receiverUid int64, notificationType int) (int, error) {
var employeeRepository domain.EmployeeRepository
var notificationDao *dao.NotificationDao
if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
... ... @@ -36,7 +36,7 @@ func (service *ReadAllUnReadSentNotificationService) ReadAll(receiverUid int64)
if receiver == nil {
return 0, fmt.Errorf("无效的通知接收者")
}
readCount, err := notificationDao.ReadAllUnReadSentNotification(receiverUid)
readCount, err := notificationDao.ReadAllUnReadSentNotification(receiverUid, notificationType)
if err != nil {
return 0, err
} else {
... ...
... ... @@ -39,7 +39,8 @@ var _ = Describe("读取全部未读取的发送出的通知", func() {
It("读取成功", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"receiverId": 2499036607974745088,
"receiverId": 2499036607974745088,
"notificationType": 1,
}
httpExpect.POST("/notifications/read-all").
WithJSON(body).
... ...