|
| 1 | +package repositories |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + |
| 9 | + "github.com/NdoleStudio/httpsms/pkg/entities" |
| 10 | + "github.com/NdoleStudio/httpsms/pkg/telemetry" |
| 11 | + "github.com/palantir/stacktrace" |
| 12 | + "gorm.io/gorm" |
| 13 | +) |
| 14 | + |
| 15 | +// gormHeartbeatRepository is responsible for persisting entities.Heartbeat |
| 16 | +type gormHeartbeatMonitorRepository struct { |
| 17 | + logger telemetry.Logger |
| 18 | + tracer telemetry.Tracer |
| 19 | + db *gorm.DB |
| 20 | +} |
| 21 | + |
| 22 | +func (repository *gormHeartbeatMonitorRepository) Delete(ctx context.Context, userID entities.UserID, owner string) error { |
| 23 | + ctx, span := repository.tracer.Start(ctx) |
| 24 | + defer span.End() |
| 25 | + |
| 26 | + err := repository.db.WithContext(ctx). |
| 27 | + Where("user_id = ?", userID). |
| 28 | + Where("owner = ?", owner). |
| 29 | + Delete(&entities.HeartbeatMonitor{}).Error |
| 30 | + if err != nil { |
| 31 | + msg := fmt.Sprintf("cannot delete heartbeat monitor with owner [%s] and userID [%s]", owner, userID) |
| 32 | + return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 33 | + } |
| 34 | + |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +// NewGormHeartbeatMonitorRepository creates the GORM version of the HeartbeatMonitorRepository |
| 39 | +func NewGormHeartbeatMonitorRepository( |
| 40 | + logger telemetry.Logger, |
| 41 | + tracer telemetry.Tracer, |
| 42 | + db *gorm.DB, |
| 43 | +) HeartbeatMonitorRepository { |
| 44 | + return &gormHeartbeatMonitorRepository{ |
| 45 | + logger: logger.WithService(fmt.Sprintf("%T", &gormHeartbeatRepository{})), |
| 46 | + tracer: tracer, |
| 47 | + db: db, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Index entities.Message between 2 parties |
| 52 | +func (repository *gormHeartbeatMonitorRepository) Index(ctx context.Context, userID entities.UserID, owner string, params IndexParams) (*[]entities.Heartbeat, error) { |
| 53 | + ctx, span := repository.tracer.Start(ctx) |
| 54 | + defer span.End() |
| 55 | + |
| 56 | + query := repository.db.WithContext(ctx).Where("user_id = ?", userID).Where("owner = ?", owner) |
| 57 | + if len(params.Query) > 0 { |
| 58 | + queryPattern := "%" + params.Query + "%" |
| 59 | + query.Where("quantity ILIKE ?", queryPattern) |
| 60 | + } |
| 61 | + |
| 62 | + heartbeats := new([]entities.Heartbeat) |
| 63 | + if err := query.Order("timestamp DESC").Limit(params.Limit).Offset(params.Skip).Find(&heartbeats).Error; err != nil { |
| 64 | + msg := fmt.Sprintf("cannot fetch heartbeats with owner [%s] and params [%+#v]", owner, params) |
| 65 | + return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 66 | + } |
| 67 | + |
| 68 | + return heartbeats, nil |
| 69 | +} |
| 70 | + |
| 71 | +// Store a new heartbeat monitor |
| 72 | +func (repository *gormHeartbeatMonitorRepository) Store(ctx context.Context, heartbeatMonitor *entities.HeartbeatMonitor) error { |
| 73 | + ctx, span := repository.tracer.Start(ctx) |
| 74 | + defer span.End() |
| 75 | + |
| 76 | + if err := repository.db.WithContext(ctx).Create(heartbeatMonitor).Error; err != nil { |
| 77 | + msg := fmt.Sprintf("cannot save heartbeatMonitor monitor with ID [%s]", heartbeatMonitor.ID) |
| 78 | + return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// Load a heartbeat monitor by userID and owner |
| 85 | +func (repository *gormHeartbeatMonitorRepository) Load(ctx context.Context, userID entities.UserID, owner string) (*entities.HeartbeatMonitor, error) { |
| 86 | + ctx, span := repository.tracer.Start(ctx) |
| 87 | + defer span.End() |
| 88 | + |
| 89 | + phone := new(entities.HeartbeatMonitor) |
| 90 | + err := repository.db.WithContext(ctx). |
| 91 | + Where("user_id = ?", userID). |
| 92 | + Where("owner = ?", owner). |
| 93 | + First(&phone).Error |
| 94 | + |
| 95 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 96 | + msg := fmt.Sprintf("heartbeat monitor with userID [%s] and owner [%s] does not exist", userID, owner) |
| 97 | + return nil, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, ErrCodeNotFound, msg)) |
| 98 | + } |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + msg := fmt.Sprintf("cannot load heartbeat monitor with userID [%s] and owner [%s]", userID, owner) |
| 102 | + return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 103 | + } |
| 104 | + |
| 105 | + return phone, nil |
| 106 | +} |
| 107 | + |
| 108 | +// Exists checks of a heartbeat monitor exists for the userID and owner |
| 109 | +func (repository *gormHeartbeatMonitorRepository) Exists(ctx context.Context, userID entities.UserID, owner string) (bool, error) { |
| 110 | + ctx, span := repository.tracer.Start(ctx) |
| 111 | + defer span.End() |
| 112 | + |
| 113 | + var exists bool |
| 114 | + err := repository.db.WithContext(ctx). |
| 115 | + Model(&entities.HeartbeatMonitor{}). |
| 116 | + Select("count(*) > 0"). |
| 117 | + Where("user_id = ?", userID). |
| 118 | + Where("owner = ?", owner). |
| 119 | + Find(&exists).Error |
| 120 | + if err != nil { |
| 121 | + msg := fmt.Sprintf("cannot check if heartbeat monitor exists with userID [%s] and owner [%s]", userID, owner) |
| 122 | + return exists, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 123 | + } |
| 124 | + |
| 125 | + return exists, nil |
| 126 | +} |
0 commit comments