作者 linmadan

消息读取按类型

@@ -9,6 +9,8 @@ import ( @@ -9,6 +9,8 @@ import (
9 type ReadAllUnReadSentNotificationCommand struct { 9 type ReadAllUnReadSentNotificationCommand struct {
10 // 通知接收者Uid 10 // 通知接收者Uid
11 ReceiverId int64 `json:"receiverId" valid:"Required"` 11 ReceiverId int64 `json:"receiverId" valid:"Required"`
  12 + // 通知类型
  13 + NotificationType int `json:"notificationType"`
12 } 14 }
13 15
14 func (readAllUnReadSentNotificationCommand *ReadAllUnReadSentNotificationCommand) ValidateCommand() error { 16 func (readAllUnReadSentNotificationCommand *ReadAllUnReadSentNotificationCommand) ValidateCommand() error {
@@ -222,7 +222,7 @@ func (notificationService *NotificationService) ReadAllUnReadSentNotification(re @@ -222,7 +222,7 @@ func (notificationService *NotificationService) ReadAllUnReadSentNotification(re
222 } else { 222 } else {
223 readAllUnReadSentNotificationService = value 223 readAllUnReadSentNotificationService = value
224 } 224 }
225 - if readCount, err := readAllUnReadSentNotificationService.ReadAll(readAllUnReadSentNotificationCommand.ReceiverId); err != nil { 225 + if readCount, err := readAllUnReadSentNotificationService.ReadAll(readAllUnReadSentNotificationCommand.ReceiverId, readAllUnReadSentNotificationCommand.NotificationType); err != nil {
226 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 226 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
227 } else { 227 } else {
228 if err := transactionContext.CommitTransaction(); err != nil { 228 if err := transactionContext.CommitTransaction(); err != nil {
@@ -6,5 +6,5 @@ import ( @@ -6,5 +6,5 @@ import (
6 6
7 type ReadAllUnReadSentNotificationService interface { 7 type ReadAllUnReadSentNotificationService interface {
8 coreDomain.DomainEventPublisher 8 coreDomain.DomainEventPublisher
9 - ReadAll(receiverUid int64) (int, error) 9 + ReadAll(receiverUid int64, notificationType int) (int, error)
10 } 10 }
@@ -11,16 +11,28 @@ type NotificationDao struct { @@ -11,16 +11,28 @@ type NotificationDao struct {
11 transactionContext *pgTransaction.TransactionContext 11 transactionContext *pgTransaction.TransactionContext
12 } 12 }
13 13
14 -func (dao *NotificationDao) ReadAllUnReadSentNotification(receiverId int64) (int, error) { 14 +func (dao *NotificationDao) ReadAllUnReadSentNotification(receiverId int64, notificationType int) (int, error) {
15 tx := dao.transactionContext.PgTx 15 tx := dao.transactionContext.PgTx
16 - result, err := tx.Query(  
17 - pg.Scan(),  
18 - `UPDATE sent_notifications SET is_read=?, read_time=? WHERE receiver @> '{"uid":?}' AND is_read=false`,  
19 - true, time.Now(), receiverId)  
20 - if err != nil {  
21 - return 0, err 16 + if notificationType == 0 {
  17 + result, err := tx.Query(
  18 + pg.Scan(),
  19 + `UPDATE sent_notifications SET is_read=?, read_time=? WHERE receiver @> '{"uid":?}' AND is_read=false`,
  20 + true, time.Now(), receiverId)
  21 + if err != nil {
  22 + return 0, err
  23 + } else {
  24 + return result.RowsAffected(), nil
  25 + }
22 } else { 26 } else {
23 - return result.RowsAffected(), nil 27 + result, err := tx.Query(
  28 + pg.Scan(),
  29 + `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=?)`,
  30 + true, time.Now(), receiverId, notificationType)
  31 + if err != nil {
  32 + return 0, err
  33 + } else {
  34 + return result.RowsAffected(), nil
  35 + }
24 } 36 }
25 } 37 }
26 38
@@ -14,7 +14,7 @@ type ReadAllUnReadSentNotificationService struct { @@ -14,7 +14,7 @@ type ReadAllUnReadSentNotificationService struct {
14 transactionContext *pgTransaction.TransactionContext 14 transactionContext *pgTransaction.TransactionContext
15 } 15 }
16 16
17 -func (service *ReadAllUnReadSentNotificationService) ReadAll(receiverUid int64) (int, error) { 17 +func (service *ReadAllUnReadSentNotificationService) ReadAll(receiverUid int64, notificationType int) (int, error) {
18 var employeeRepository domain.EmployeeRepository 18 var employeeRepository domain.EmployeeRepository
19 var notificationDao *dao.NotificationDao 19 var notificationDao *dao.NotificationDao
20 if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil { 20 if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
@@ -36,7 +36,7 @@ func (service *ReadAllUnReadSentNotificationService) ReadAll(receiverUid int64) @@ -36,7 +36,7 @@ func (service *ReadAllUnReadSentNotificationService) ReadAll(receiverUid int64)
36 if receiver == nil { 36 if receiver == nil {
37 return 0, fmt.Errorf("无效的通知接收者") 37 return 0, fmt.Errorf("无效的通知接收者")
38 } 38 }
39 - readCount, err := notificationDao.ReadAllUnReadSentNotification(receiverUid) 39 + readCount, err := notificationDao.ReadAllUnReadSentNotification(receiverUid, notificationType)
40 if err != nil { 40 if err != nil {
41 return 0, err 41 return 0, err
42 } else { 42 } else {
@@ -39,7 +39,8 @@ var _ = Describe("读取全部未读取的发送出的通知", func() { @@ -39,7 +39,8 @@ var _ = Describe("读取全部未读取的发送出的通知", func() {
39 It("读取成功", func() { 39 It("读取成功", func() {
40 httpExpect := httpexpect.New(GinkgoT(), server.URL) 40 httpExpect := httpexpect.New(GinkgoT(), server.URL)
41 body := map[string]interface{}{ 41 body := map[string]interface{}{
42 - "receiverId": 2499036607974745088, 42 + "receiverId": 2499036607974745088,
  43 + "notificationType": 1,
43 } 44 }
44 httpExpect.POST("/notifications/read-all"). 45 httpExpect.POST("/notifications/read-all").
45 WithJSON(body). 46 WithJSON(body).