Set up project files and add .gitignore to exclude local build/runtime artifacts. Made-with: Cursor
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrot_bbs/internal/model"
|
|
|
|
"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)
|
|
}
|
|
|
|
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
|
|
}
|