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) } }
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 }
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 }
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) } } }