48 lines
956 B
Go
48 lines
956 B
Go
package tariff
|
|
|
|
import (
|
|
"context"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.uber.org/zap"
|
|
"hub_admin_backend_service/internal/errors"
|
|
"hub_admin_backend_service/internal/models"
|
|
)
|
|
|
|
type Deps struct {
|
|
Mdb *mongo.Collection
|
|
Logger *zap.Logger
|
|
}
|
|
|
|
type Tariff struct {
|
|
mdb *mongo.Collection
|
|
logger *zap.Logger
|
|
}
|
|
|
|
func NewTariffRepo(deps Deps) *Tariff {
|
|
return &Tariff{
|
|
mdb: deps.Mdb,
|
|
logger: deps.Logger,
|
|
}
|
|
}
|
|
|
|
func (t *Tariff) GetByID(ctx context.Context, id primitive.ObjectID) (models.Tariff, error) {
|
|
var tariff models.Tariff
|
|
filter := bson.M{
|
|
"_id": id,
|
|
"isDeleted": false,
|
|
}
|
|
|
|
err := t.mdb.FindOne(ctx, filter).Decode(&tariff)
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return tariff, errors.ErrNotFound
|
|
}
|
|
t.logger.Error("failed to get tariff by object id", zap.Error(err))
|
|
return tariff, err
|
|
}
|
|
|
|
return tariff, nil
|
|
}
|