Ejemplo n.º 1
0
func (r *AccountRepo) UpdateByID(id int, account *domain.Account,
	context usecases.QueryContext) (*domain.Account, error) {

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

	dbName := utils.ToDBName("accounts")
	oldAccount := &domain.Account{}

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

		return nil, internalerrors.DatabaseError
	}

	account.ID = oldAccount.ID
	account.CreatedAt = oldAccount.CreatedAt

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

		return nil, internalerrors.DatabaseError
	}

	return account, nil
}
Ejemplo n.º 2
0
func (i *AccountInter) scopeModel(account *domain.Account) error {
	account.CreatedAt = time.Time{}
	account.UpdatedAt = time.Time{}
	account.Users = []domain.User{}
	account.Sessions = []domain.Session{}
	account.RoleMappings = []domain.RoleMapping{}

	return nil
}