Beispiel #1
0
func (r *RoleMappingRepo) UpdateByID(id int, roleMapping *domain.RoleMapping,
	context usecases.QueryContext) (*domain.RoleMapping, error) {

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

	dbName := utils.ToDBName("roleMappings")
	oldRoleMapping := &domain.RoleMapping{}

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

		return nil, internalerrors.DatabaseError
	}

	roleMapping.ID = oldRoleMapping.ID
	roleMapping.CreatedAt = oldRoleMapping.CreatedAt

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

		return nil, internalerrors.DatabaseError
	}

	return roleMapping, nil
}
Beispiel #2
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
}
Beispiel #3
0
func (r *UserRepo) UpdateAttributesByID(id int, attributes map[string]interface{},
	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")
	user := &domain.User{}

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

		return nil, internalerrors.DatabaseError
	}

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

		return nil, internalerrors.DatabaseError
	}

	return user, nil
}
Beispiel #4
0
func (r *SessionRepo) UpdateByID(id int, session *domain.Session,
	context usecases.QueryContext) (*domain.Session, error) {

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

	dbName := utils.ToDBName("sessions")

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

		return nil, internalerrors.DatabaseError
	}

	err = r.store.GetDB().Where(dbName+".id = ?", id).Model(&domain.Session{}).Updates(&session).Error
	if err != nil {
		if strings.Contains(err.Error(), "constraint") {
			return nil, internalerrors.NewViolatedConstraint(err.Error())
		}

		return nil, internalerrors.DatabaseError
	}

	return session, nil
}
Beispiel #5
0
func (r *AccountRepo) Raw(query string, values ...interface{}) (*sql.Rows, error) {
	db := r.store.GetDB()

	rows, err := db.Raw(query, values...).Rows()
	if err != nil {
		if strings.Contains(err.Error(), "constraint") {
			return nil, internalerrors.NewViolatedConstraint(err.Error())
		}

		return nil, internalerrors.DatabaseError
	}

	return rows, nil
}
Beispiel #6
0
func (r *AccountRepo) Update(accounts []domain.Account, context usecases.QueryContext) ([]domain.Account, error) {
	db := r.store.GetDB()
	transaction := db.Begin()

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

	for _, account := range accounts {
		queryCopy := *query

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

		err = queryCopy.Where(dbName+".id = ?", account.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
		}
	}

	transaction.Commit()
	return accounts, nil
}
Beispiel #7
0
func (r *RoleMappingRepo) Update(roleMappings []domain.RoleMapping, context usecases.QueryContext) ([]domain.RoleMapping, error) {
	db := r.store.GetDB()
	transaction := db.Begin()

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

	for _, roleMapping := range roleMappings {
		queryCopy := *query

		dbName := utils.ToDBName("roleMappings")
		oldRoleMapping := &domain.RoleMapping{}

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

			return nil, internalerrors.DatabaseError
		}

		roleMapping.ID = oldRoleMapping.ID
		roleMapping.CreatedAt = oldRoleMapping.CreatedAt

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

			return nil, internalerrors.DatabaseError
		}
	}

	transaction.Commit()
	return roleMappings, nil
}
Beispiel #8
0
func (r *AccountRepo) Create(accounts []domain.Account) ([]domain.Account, error) {
	db := r.store.GetDB()
	transaction := db.Begin()

	for i, account := range accounts {
		err := db.Create(&account).Error
		if err != nil {
			transaction.Rollback()

			if strings.Contains(err.Error(), "constraint") {
				return nil, internalerrors.NewViolatedConstraint(err.Error())
			}

			return nil, internalerrors.DatabaseError
		}

		accounts[i] = account
	}

	transaction.Commit()
	return accounts, nil
}
Beispiel #9
0
func (r *UserRepo) Create(users []domain.User) ([]domain.User, error) {
	db := r.store.GetDB()
	transaction := db.Begin()

	for i, user := range users {
		err := db.Create(&user).Error
		if err != nil {
			transaction.Rollback()

			if strings.Contains(err.Error(), "constraint") {
				return nil, internalerrors.NewViolatedConstraint(err.Error())
			}

			return nil, internalerrors.DatabaseError
		}

		users[i] = user
	}

	transaction.Commit()
	return users, nil
}
Beispiel #10
0
func (r *RoleRepo) Create(roles []domain.Role) ([]domain.Role, error) {
	db := r.store.GetDB()
	transaction := db.Begin()

	for i, role := range roles {
		err := db.Create(&role).Error
		if err != nil {
			transaction.Rollback()

			if strings.Contains(err.Error(), "constraint") {
				return nil, internalerrors.NewViolatedConstraint(err.Error())
			}

			return nil, internalerrors.DatabaseError
		}

		roles[i] = role
	}

	transaction.Commit()
	return roles, nil
}
Beispiel #11
0
func (r *SessionRepo) Create(sessions []domain.Session) ([]domain.Session, error) {
	db := r.store.GetDB()
	transaction := db.Begin()

	for i, session := range sessions {
		err := db.Create(&session).Error
		if err != nil {
			transaction.Rollback()

			if strings.Contains(err.Error(), "constraint") {
				return nil, internalerrors.NewViolatedConstraint(err.Error())
			}

			return nil, internalerrors.DatabaseError
		}

		sessions[i] = session
	}

	transaction.Commit()
	return sessions, nil
}
Beispiel #12
0
func (r *SessionRepo) Update(sessions []domain.Session, context usecases.QueryContext) ([]domain.Session, error) {
	db := r.store.GetDB()
	transaction := db.Begin()

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

	for _, session := range sessions {
		queryCopy := *query

		dbName := utils.ToDBName("sessions")

		err = queryCopy.Where(dbName+".id = ?", session.ID).First(&domain.Session{}).Error
		if err != nil {
			if strings.Contains(err.Error(), "record not found") {
				return nil, internalerrors.NotFound
			}

			return nil, internalerrors.DatabaseError
		}

		err = r.store.GetDB().Where(dbName+".id = ?", session.ID).Model(&domain.Session{}).Updates(&session).Error
		if err != nil {
			if strings.Contains(err.Error(), "constraint") {
				return nil, internalerrors.NewViolatedConstraint(err.Error())
			}

			return nil, internalerrors.DatabaseError
		}
	}

	transaction.Commit()
	return sessions, nil
}