本文整理汇总了Golang中periwinkle.Tx.Model方法的典型用法代码示例。如果您正苦于以下问题:Golang Tx.Model方法的具体用法?Golang Tx.Model怎么用?Golang Tx.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类periwinkle.Tx
的用法示例。
在下文中一共展示了Tx.Model方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Save
func (o *Group) Save(db *periwinkle.Tx) {
if o.Subscriptions != nil {
var oldSubscriptions []Subscription
db.Model(o).Related(&oldSubscriptions)
for _, oldsub := range oldSubscriptions {
match := false
for _, newsub := range o.Subscriptions {
if newsub.AddressID == oldsub.AddressID {
match = true
break
}
}
if !match {
var o UserAddress
db.First(&o, "id = ?", oldsub.AddressID)
if o.Medium != "noop" && o.Medium != "admin" {
if err := db.Where("address_id = ? AND group_id = ?", oldsub.AddressID, oldsub.GroupID).Delete(Subscription{}).Error; err != nil {
dbError(err)
}
}
}
}
}
if err := db.Save(o).Error; err != nil {
dbError(err)
}
}
示例2: GetGroupByID
func GetGroupByID(db *periwinkle.Tx, id string) *Group {
id = strings.ToLower(id)
var o Group
if result := db.First(&o, "id = ?", id); result.Error != nil {
if result.RecordNotFound() {
return nil
}
dbError(result.Error)
}
db.Model(&o).Related(&o.Subscriptions)
return &o
}
示例3: GetSubscriptions
func (u *User) GetSubscriptions(db *periwinkle.Tx) []Subscription {
db.Model(u).Related(&u.Addresses)
addressIDs := make([]int64, len(u.Addresses))
for i, address := range u.Addresses {
addressIDs[i] = address.ID
}
var subscriptions []Subscription
if len(addressIDs) > 0 {
if result := db.Where("address_id IN (?)", addressIDs).Find(&subscriptions); result.Error != nil {
if !result.RecordNotFound() {
dbError(result.Error)
}
}
} else {
subscriptions = make([]Subscription, 0)
}
return subscriptions
}
示例4: Save
func (usr *User) Save(db *periwinkle.Tx) {
usr.ID = strings.ToLower(usr.ID)
if usr.Addresses != nil {
var oldAddresses []UserAddress
db.Model(usr).Related(&oldAddresses)
deleteAddressIDs := []int64{}
for o := range oldAddresses {
oldAddr := &oldAddresses[o]
match := false
for n := range usr.Addresses {
newAddr := &usr.Addresses[n]
if newAddr.Medium == oldAddr.Medium && newAddr.Address == oldAddr.Address {
newAddr.ID = oldAddr.ID
match = true
}
}
if !match && oldAddr.Medium != "noop" && oldAddr.Medium != "admin" {
deleteAddressIDs = append(deleteAddressIDs, oldAddr.ID)
}
}
if err := db.Save(usr).Error; err != nil {
dbError(err)
}
if len(deleteAddressIDs) > 0 {
if err := db.Where("id IN (?)", deleteAddressIDs).Delete(UserAddress{}).Error; err != nil {
dbError(err)
}
}
} else {
if err := db.Save(usr).Error; err != nil {
dbError(err)
}
}
}