예제 #1
0
func UpdateEmail(context context.Context, id types.ID, mobile string) (*Account, error) {
	loginID, _ := context.Value("login_id").(types.ID)
	if loginID <= 0 {
		return nil, ErrAuth
	}

	if !validation.IsMobile(mobile) {
		return nil, errors.New("invalid mobile")
	}

	if loginID != id {
		return nil, ErrForbidden
	}

	a, err := persistence.GetAccount(id)
	if err != nil {
		return nil, ErrServer
	}

	if a == nil {
		return nil, ErrNotFound
	}

	if a.Mobile == mobile {
		acc := &Account{}
		runtime.CopyFields(acc, a)
		return acc, nil
	}

	if persistence.IsDupMobile(mobile, a.ID) {
		return nil, ErrDupNick
	}

	a.Mobile = mobile
	err = persistence.UpdateAccount(a)
	if err != nil {
		return nil, ErrServer
	}

	acc := &Account{}
	runtime.CopyFields(acc, a)
	return acc, nil
}
예제 #2
0
func UpdateNick(context context.Context, id types.ID, nick string) (*Account, error) {
	loginID, _ := context.Value("login_id").(types.ID)
	if loginID <= 0 {
		return nil, ErrAuth
	}

	if len(nick) < 2 {
		return nil, errors.New("invalid nick")
	}

	if loginID != id {
		return nil, ErrForbidden
	}

	a, err := persistence.GetAccount(id)
	if err != nil {
		return nil, ErrServer
	}

	if a == nil {
		return nil, ErrNotFound
	}

	if a.Nick == nick {
		acc := &Account{}
		runtime.CopyFields(acc, a)
		return acc, nil
	}

	if persistence.IsDupNick(nick, a.ID) {
		return nil, ErrDupNick
	}

	a.Nick = nick
	err = persistence.UpdateAccount(a)
	if err != nil {
		return nil, ErrServer
	}

	acc := &Account{}
	runtime.CopyFields(acc, a)
	return acc, nil
}