|
- package model
-
- import (
- "errors"
-
- "github.com/QuantumNous/new-api/common"
- "gorm.io/gorm"
- )
-
- // UserAssetGroup binds a platform user to an upstream asset group per channel.
- type UserAssetGroup struct {
- Id int `json:"id" gorm:"primaryKey"`
- UserId int `json:"user_id" gorm:"not null;uniqueIndex:idx_user_asset_group,priority:1"`
- ChannelId int `json:"channel_id" gorm:"not null;uniqueIndex:idx_user_asset_group,priority:2"`
- GroupId string `json:"group_id" gorm:"type:varchar(255);not null"`
- CreatedAt int64 `json:"created_at"`
- UpdatedAt int64 `json:"updated_at"`
- }
-
- func (UserAssetGroup) TableName() string {
- return "user_asset_groups"
- }
-
- func GetUserAssetGroup(userId int, channelId int) (*UserAssetGroup, error) {
- var binding UserAssetGroup
- err := DB.Where("user_id = ? AND channel_id = ?", userId, channelId).First(&binding).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &binding, nil
- }
-
- func CreateUserAssetGroup(userId int, channelId int, groupId string) error {
- now := common.GetTimestamp()
- return DB.Create(&UserAssetGroup{
- UserId: userId,
- ChannelId: channelId,
- GroupId: groupId,
- CreatedAt: now,
- UpdatedAt: now,
- }).Error
- }
|