2026-04-22 16:01:59 +08:00
|
|
|
|
package repository
|
2026-03-09 21:28:58 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-04 13:07:03 +08:00
|
|
|
|
"errors"
|
|
|
|
|
|
|
2026-04-22 16:01:59 +08:00
|
|
|
|
"with_you/internal/model"
|
2026-03-09 21:28:58 +08:00
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type GroupJoinRequestRepository interface {
|
|
|
|
|
|
Create(req *model.GroupJoinRequest) error
|
|
|
|
|
|
GetByFlag(flag string) (*model.GroupJoinRequest, error)
|
|
|
|
|
|
Update(req *model.GroupJoinRequest) error
|
|
|
|
|
|
GetPendingByGroupAndTarget(groupID, targetUserID string, reqType model.GroupJoinRequestType) (*model.GroupJoinRequest, error)
|
2026-05-04 13:07:03 +08:00
|
|
|
|
GetPendingByGroupAndTargets(groupID string, targetUserIDs []string, reqType model.GroupJoinRequestType) (map[string]bool, error)
|
2026-03-09 21:28:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type groupJoinRequestRepository struct {
|
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewGroupJoinRequestRepository(db *gorm.DB) GroupJoinRequestRepository {
|
|
|
|
|
|
return &groupJoinRequestRepository{db: db}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *groupJoinRequestRepository) Create(req *model.GroupJoinRequest) error {
|
|
|
|
|
|
return r.db.Create(req).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *groupJoinRequestRepository) GetByFlag(flag string) (*model.GroupJoinRequest, error) {
|
|
|
|
|
|
var req model.GroupJoinRequest
|
|
|
|
|
|
if err := r.db.Where("flag = ?", flag).First(&req).Error; err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &req, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *groupJoinRequestRepository) Update(req *model.GroupJoinRequest) error {
|
|
|
|
|
|
return r.db.Save(req).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *groupJoinRequestRepository) GetPendingByGroupAndTarget(groupID, targetUserID string, reqType model.GroupJoinRequestType) (*model.GroupJoinRequest, error) {
|
|
|
|
|
|
var req model.GroupJoinRequest
|
|
|
|
|
|
err := r.db.Where("group_id = ? AND target_user_id = ? AND request_type = ? AND status = ?",
|
|
|
|
|
|
groupID, targetUserID, reqType, model.GroupJoinRequestStatusPending).
|
|
|
|
|
|
Order("created_at DESC").
|
|
|
|
|
|
First(&req).Error
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &req, nil
|
|
|
|
|
|
}
|
2026-05-04 13:07:03 +08:00
|
|
|
|
|
|
|
|
|
|
func (r *groupJoinRequestRepository) GetPendingByGroupAndTargets(groupID string, targetUserIDs []string, reqType model.GroupJoinRequestType) (map[string]bool, error) {
|
|
|
|
|
|
result := make(map[string]bool)
|
|
|
|
|
|
if len(targetUserIDs) == 0 {
|
|
|
|
|
|
return result, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
var existingIDs []string
|
|
|
|
|
|
err := r.db.Model(&model.GroupJoinRequest{}).
|
|
|
|
|
|
Where("group_id = ? AND target_user_id IN ? AND request_type = ? AND status = ?",
|
|
|
|
|
|
groupID, targetUserIDs, reqType, model.GroupJoinRequestStatusPending).
|
|
|
|
|
|
Pluck("target_user_id", &existingIDs).Error
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return result, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, id := range existingIDs {
|
|
|
|
|
|
result[id] = true
|
|
|
|
|
|
}
|
|
|
|
|
|
return result, nil
|
|
|
|
|
|
}
|