migrate from github
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package notification_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type NotificationCreateBatchOperation struct {
|
||||
repo *NotificationRepository
|
||||
items []model.Notification
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *NotificationRepository) CreateBatch(items []model.Notification) *NotificationCreateBatchOperation {
|
||||
return &NotificationCreateBatchOperation{
|
||||
repo: r,
|
||||
items: items,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *NotificationCreateBatchOperation) WithTx(tx *gorm.DB) *NotificationCreateBatchOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *NotificationCreateBatchOperation) Exec() error {
|
||||
if len(op.items) == 0 {
|
||||
return nil
|
||||
}
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
return db.Create(&op.items).Error
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package notification_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type NotificationListOperation struct {
|
||||
repo *NotificationRepository
|
||||
receiverID int64
|
||||
status *string
|
||||
page int
|
||||
pageSize int
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *NotificationRepository) List(receiverID int64) *NotificationListOperation {
|
||||
return &NotificationListOperation{
|
||||
repo: r,
|
||||
receiverID: receiverID,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *NotificationListOperation) WithTx(tx *gorm.DB) *NotificationListOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *NotificationListOperation) WithStatus(status *string) *NotificationListOperation {
|
||||
op.status = status
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *NotificationListOperation) WithPagination(page, pageSize int) *NotificationListOperation {
|
||||
op.page = page
|
||||
op.pageSize = pageSize
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *NotificationListOperation) ExecWithTotal() ([]model.Notification, int64, error) {
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
|
||||
query := db.Model(&model.Notification{}).Where("receiver_id = ?", op.receiverID)
|
||||
if op.status != nil && *op.status != "" {
|
||||
query = query.Where("status = ?", *op.status)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
page := op.page
|
||||
pageSize := op.pageSize
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
if pageSize > 100 {
|
||||
pageSize = 100
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
var list []model.Notification
|
||||
if err := query.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&list).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return list, total, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package notification_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type NotificationMarkReadOperation struct {
|
||||
repo *NotificationRepository
|
||||
receiverID int64
|
||||
externalIDs []string
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *NotificationRepository) MarkRead(receiverID int64, externalIDs []string) *NotificationMarkReadOperation {
|
||||
return &NotificationMarkReadOperation{
|
||||
repo: r,
|
||||
receiverID: receiverID,
|
||||
externalIDs: externalIDs,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *NotificationMarkReadOperation) WithTx(tx *gorm.DB) *NotificationMarkReadOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *NotificationMarkReadOperation) Exec() error {
|
||||
if len(op.externalIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
return db.Model(&model.Notification{}).
|
||||
Where("receiver_id = ? AND external_id IN ?", op.receiverID, op.externalIDs).
|
||||
Updates(map[string]any{"status": "read"}).
|
||||
Error
|
||||
}
|
||||
|
||||
type NotificationMarkAllReadOperation struct {
|
||||
repo *NotificationRepository
|
||||
receiverID int64
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (r *NotificationRepository) MarkAllRead(receiverID int64) *NotificationMarkAllReadOperation {
|
||||
return &NotificationMarkAllReadOperation{
|
||||
repo: r,
|
||||
receiverID: receiverID,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *NotificationMarkAllReadOperation) WithTx(tx *gorm.DB) *NotificationMarkAllReadOperation {
|
||||
op.tx = tx
|
||||
return op
|
||||
}
|
||||
|
||||
func (op *NotificationMarkAllReadOperation) Exec() error {
|
||||
db := op.repo.db
|
||||
if op.tx != nil {
|
||||
db = op.tx
|
||||
}
|
||||
return db.Model(&model.Notification{}).
|
||||
Where("receiver_id = ? AND status != ?", op.receiverID, "read").
|
||||
Updates(map[string]any{"status": "read"}).
|
||||
Error
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package notification_repo
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type NotificationRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewNotificationRepository(db *gorm.DB) *NotificationRepository {
|
||||
return &NotificationRepository{db: db}
|
||||
}
|
||||
|
||||
type NotificationUpdateExternalIDOperation struct {
|
||||
repo *NotificationRepository
|
||||
id int64
|
||||
externalID string
|
||||
}
|
||||
|
||||
func (r *NotificationRepository) UpdateExternalID(id int64, externalID string) *NotificationUpdateExternalIDOperation {
|
||||
return &NotificationUpdateExternalIDOperation{
|
||||
repo: r,
|
||||
id: id,
|
||||
externalID: externalID,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *NotificationUpdateExternalIDOperation) Exec() error {
|
||||
if op.id == 0 || op.externalID == "" {
|
||||
return nil
|
||||
}
|
||||
return op.repo.db.Model(&model.Notification{}).
|
||||
Where("id = ? AND (external_id IS NULL OR external_id = '')", op.id).
|
||||
Updates(map[string]any{"external_id": op.externalID}).
|
||||
Error
|
||||
}
|
||||
Reference in New Issue
Block a user