本文整理汇总了Golang中github.com/pomack/dsocial/go/models/dsocial.User.BeforeSave方法的典型用法代码示例。如果您正苦于以下问题:Golang User.BeforeSave方法的具体用法?Golang User.BeforeSave怎么用?Golang User.BeforeSave使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pomack/dsocial/go/models/dsocial.User
的用法示例。
在下文中一共展示了User.BeforeSave方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CreateUserAccount
func (p *InMemoryDataStore) CreateUserAccount(user *dm.User) (*dm.User, error) {
if user == nil {
return nil, nil
}
if user.Id == "" {
user.Id = p.generateIdForAccount(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME)
}
if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id); ok {
return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_ID
}
if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username); ok {
return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_USERNAME
}
if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email); ok {
return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_EMAIL
}
if err := user.BeforeCreate(); err != nil {
return user, err
}
if err := user.BeforeSave(); err != nil {
return user, err
}
p.store(user.Id, _INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id, user)
p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username, user.Id)
p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email, user.Id)
if err := user.AfterSave(); err != nil {
return user, err
}
if err := user.AfterCreate(); err != nil {
return user, err
}
return user, nil
}
示例2: UpdateUserAccount
func (p *InMemoryDataStore) UpdateUserAccount(user *dm.User) (*dm.User, error) {
if user == nil {
return nil, nil
}
if user.Id == "" {
return user, dm.ERR_MUST_SPECIFY_ID
}
oldUserI, _ := p.retrieve(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id)
var oldUser *dm.User = nil
if oldUserI != nil {
oldUser = oldUserI.(*dm.User)
}
if oldUser == nil || oldUser.Username != user.Username {
if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username); ok {
return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_USERNAME
}
}
if oldUser == nil || oldUser.Email != user.Email {
if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email); ok {
return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_EMAIL
}
}
if err := user.BeforeUpdate(); err != nil {
return user, err
}
if err := user.BeforeSave(); err != nil {
return user, err
}
p.store(user.Id, _INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id, user)
p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username, user.Id)
p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email, user.Id)
if oldUser != nil {
if oldUser.Username != user.Username {
p.delete(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, oldUser.Username)
}
if oldUser.Email != user.Email {
p.delete(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, oldUser.Email)
}
}
if err := user.AfterSave(); err != nil {
return user, err
}
if err := user.AfterUpdate(); err != nil {
return user, err
}
return user, nil
}