Ejemplo n.º 1
0
func (r *UserRepo) UpdateByID(id int, user *domain.User,
	context usecases.QueryContext) (*domain.User, error) {

	query, err := r.store.BuildQuery(context.Filter, context.OwnerRelations)
	if err != nil {
		return nil, internalerrors.DatabaseError
	}

	dbName := utils.ToDBName("users")
	oldUser := &domain.User{}

	err = query.Where(dbName+".id = ?", id).First(oldUser).Error
	if err != nil {
		if strings.Contains(err.Error(), "record not found") {
			return nil, internalerrors.NotFound
		}

		return nil, internalerrors.DatabaseError
	}

	user.ID = oldUser.ID
	user.CreatedAt = oldUser.CreatedAt

	err = r.store.GetDB().Save(&user).Error
	if err != nil {
		if strings.Contains(err.Error(), "constraint") {
			return nil, internalerrors.NewViolatedConstraint(err.Error())
		}

		return nil, internalerrors.DatabaseError
	}

	return user, nil
}
Ejemplo n.º 2
0
func (i *UserGuestInter) scopeModel(user *domain.User) error {
	user.CreatedAt = time.Time{}
	user.UpdatedAt = time.Time{}
	user.Account = domain.Account{}

	return nil
}
Ejemplo n.º 3
0
func (i *UserInter) scopeModel(user *domain.User) error {
	user.CreatedAt = time.Time{}
	user.UpdatedAt = time.Time{}
	user.Account = domain.Account{}

	if user.Password != "" {
		hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), 0)
		if err != nil {
			return err
		}

		user.Password = string(hashedPassword)
	}

	return nil
}