Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

2.5 KB
Raw Blame
package repositories

import (
	"context"
	"errors"
	"fmt"

	"github.com/NdoleStudio/httpsms/pkg/entities"
	"github.com/NdoleStudio/httpsms/pkg/telemetry"
	"github.com/NdoleStudio/stacktrace"
	"gorm.io/gorm"
)

// gormIntegration3CxRepository is responsible for persisting entities.Integration3CX
type gormIntegration3CxRepository struct {
	logger telemetry.Logger
	tracer telemetry.Tracer
	db     *gorm.DB
}

// NewGormIntegration3CXRepository creates the GORM version of the Integration3CxRepository
func NewGormIntegration3CXRepository(
	logger telemetry.Logger,
	tracer telemetry.Tracer,
	db *gorm.DB,
) Integration3CxRepository {
	return &gormIntegration3CxRepository{
		logger: logger.WithService(fmt.Sprintf("%T", &gormIntegration3CxRepository{})),
		tracer: tracer,
		db:     db,
	}
}

func (repository *gormIntegration3CxRepository) DeleteAllForUser(ctx context.Context, userID entities.UserID) error {
	ctx, span := repository.tracer.Start(ctx)
	defer span.End()

	if err := repository.db.WithContext(ctx).Where("user_id = ?", userID).Delete(&entities.Integration3CX{}).Error; err != nil {
		return repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot delete all [%T] for user with ID [%s]", &entities.Integration3CX{}, userID))
	}

	return nil
}

// Load an entities.Integration3CX based on the entities.UserID
func (repository *gormIntegration3CxRepository) Load(ctx context.Context, userID entities.UserID) (*entities.Integration3CX, error) {
	ctx, span := repository.tracer.Start(ctx)
	defer span.End()

	integration := new(entities.Integration3CX)
	err := repository.db.WithContext(ctx).Where("user_id = ?", userID).First(&integration).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCodef(err, ErrCodeNotFound, "[3cx] integration for user [%s] does not exist", userID))
	}

	if err != nil {
		return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot load [3cx] integration for user [%s]", userID))
	}

	return integration, nil
}

// Save an entities.Integration3CX
func (repository *gormIntegration3CxRepository) Save(ctx context.Context, integration *entities.Integration3CX) error {
	ctx, span := repository.tracer.Start(ctx)
	defer span.End()

	if err := repository.db.WithContext(ctx).Save(integration).Error; err != nil {
		return repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot save [%T] with ID [%s]", integration, integration.ID))
	}

	return nil
}