feat: 添加通知微服务,支持站内通知已读状态

This commit is contained in:
zetaloop
2026-04-24 12:44:59 +08:00
parent 95f2f10f9f
commit b557bfcc2e
52 changed files with 7035 additions and 30 deletions
+31 -30
View File
@@ -1,40 +1,41 @@
syntax = "v1"
import "common.api"
type (
PathId {
Id int64 `path:"id"`
}
Notification {
Id int64 `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
Content string `json:"content"`
Read bool `json:"read"`
Link string `json:"link,optional"`
CreatedAt string `json:"createdAt"`
}
NotificationListResp {
Items []Notification `json:"items"`
Meta PageMeta `json:"meta"`
}
PathId {
Id int64 `path:"id"`
}
Notification {
Id int64 `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
Content string `json:"content"`
Read bool `json:"read"`
Link string `json:"link,optional"`
CreatedAt string `json:"createdAt"`
}
NotificationListResp {
Items []Notification `json:"items"`
Meta PageMeta `json:"meta"`
}
)
@server(
prefix: api/v1
group: notification
@server (
prefix: api/v1
group: notification
)
service notifi-api {
@doc "获取通知列表"
@handler ListNotifications
get /notifications (PageReq) returns (NotificationListResp)
@doc "获取通知列表"
@handler ListNotifications
get /notifications (PageReq) returns (NotificationListResp)
@doc "标记已读"
@handler ReadNotification
put /notifications/:id/read (PathId) returns (EmptyResp)
@doc "标记已读"
@handler ReadNotification
put /notifications/:id/read (PathId) returns (EmptyResp)
@doc "全部已读"
@handler ReadAllNotifications
put /notifications/read-all (EmptyResp) returns (EmptyResp)
}
@doc "全部已读"
@handler ReadAllNotifications
put /notifications/read-all (EmptyResp) returns (EmptyResp)
}
+87
View File
@@ -0,0 +1,87 @@
syntax = "proto3";
option go_package ="./pb";
package pb;
// ------------------------------------
// Messages
// ------------------------------------
//--------------------------------notifications--------------------------------
message Notifications {
int64 id = 1;
int64 userId = 2;
string type = 3;
string title = 4;
string content = 5;
bool read = 6;
string link = 7;
int64 createdAt = 8;
int64 updatedAt = 9;
}
message AddNotificationsReq {
int64 userId = 1;
string type = 2;
string title = 3;
string content = 4;
optional string link = 5;
}
message AddNotificationsResp {
int64 id = 1;
}
message UpdateNotificationsReq {
int64 id = 1;
optional bool read = 2;
optional string type = 3;
optional string title = 4;
optional string content = 5;
optional string link = 6;
optional int64 updatedAt = 7;
}
message UpdateNotificationsResp {
}
message DelNotificationsReq {
int64 id = 1;
}
message DelNotificationsResp {
}
message GetNotificationsByIdReq {
int64 id = 1;
}
message GetNotificationsByIdResp {
Notifications notifications = 1;
}
message SearchNotificationsReq {
int64 offset = 1;
int64 limit = 2;
optional int64 id = 3;
optional int64 userId = 4;
optional string type = 5;
optional bool read = 6;
}
message SearchNotificationsResp {
repeated Notifications notifications = 1;
}
// ------------------------------------
// Rpc Func
// ------------------------------------
service notificationService {
rpc AddNotifications(AddNotificationsReq) returns (AddNotificationsResp);
rpc UpdateNotifications(UpdateNotificationsReq) returns (UpdateNotificationsResp);
rpc DelNotifications(DelNotificationsReq) returns (DelNotificationsResp);
rpc GetNotificationsById(GetNotificationsByIdReq) returns (GetNotificationsByIdResp);
rpc SearchNotifications(SearchNotificationsReq) returns (SearchNotificationsResp);
}
+24
View File
@@ -0,0 +1,24 @@
CREATE TABLE notifications
(
id BIGINT PRIMARY KEY,
user_id BIGINT NOT NULL,
type VARCHAR(20) NOT NULL,
title VARCHAR(200) NOT NULL,
content TEXT NOT NULL,
read BOOLEAN NOT NULL DEFAULT FALSE,
link VARCHAR(500),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chk_notification_type CHECK (type IN ('order', 'community', 'system'))
);
CREATE INDEX idx_notifications_user_created ON notifications (user_id, created_at DESC);
CREATE INDEX idx_notifications_user_read_created ON notifications (user_id, read, created_at DESC);
CREATE INDEX idx_notifications_user_type_created ON notifications (user_id, type, created_at DESC);
CREATE TRIGGER trigger_notifications_updated_at
BEFORE UPDATE
ON notifications
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();