backend/internal/db/mongo/transaction_crud.go

Summary

Maintainability
F
3 days
Test Coverage
package mongo

import (
    "bar/internal/models"
    "context"
    "time"

    "github.com/google/uuid"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func (b *Backend) CreateTransaction(ctx context.Context, tx *models.Transaction) error {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    tx.Transaction.CreatedAt = uint64(time.Now().Unix())

    _, err := b.db.Collection(TransactionsCollection).InsertOne(ctx, tx)
    if err != nil {
        return err
    }

    return nil
}

func (b *Backend) GetTransaction(ctx context.Context, id string) (*models.Transaction, error) {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    var tx models.Transaction
    err := b.db.Collection(TransactionsCollection).FindOne(ctx,
        bson.M{
            "id": uuid.MustParse(id),

            "$or": []bson.M{
                {
                    "deleted_at": bson.M{
                        "$exists": false,
                    },
                },
                {
                    "deleted_at": nil,
                },
            },
        },
    ).Decode(&tx)
    if err != nil {
        return nil, err
    }

    return &tx, nil
}

func (b *Backend) UpdateTransaction(ctx context.Context, tx *models.Transaction) error {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    res := b.db.Collection(TransactionsCollection).FindOneAndUpdate(ctx,
        bson.M{
            "id": tx.Id,

            "$or": []bson.M{
                {
                    "deleted_at": bson.M{
                        "$exists": false,
                    },
                },
                {
                    "deleted_at": nil,
                },
            },
        },
        bson.M{
            "$set": tx,
        },
        options.FindOneAndUpdate().SetUpsert(true))
    if res.Err() != nil {
        return res.Err()
    }

    return nil
}

func (b *Backend) MarkDeleteTransaction(ctx context.Context, id, by string) error {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    res := b.db.Collection(TransactionsCollection).FindOneAndUpdate(ctx,
        bson.M{
            "id": uuid.MustParse(id),
        },
        bson.M{
            "$set": bson.M{
                "deleted_at": time.Now().Unix(),
                "deleted_by": uuid.MustParse(by),
            },
        },
        options.FindOneAndUpdate().SetUpsert(false))
    if res.Err() != nil {
        return res.Err()
    }

    return nil
}

func (b *Backend) UnMarkDeleteTransaction(ctx context.Context, id string) error {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    res := b.db.Collection(TransactionsCollection).FindOneAndUpdate(ctx,
        bson.M{
            "id": uuid.MustParse(id),
        },
        bson.M{
            "$set": bson.M{
                "deleted_at": nil,
                "deleted_by": nil,
            },
        },
        options.FindOneAndUpdate().SetUpsert(false))
    if res.Err() != nil {
        return res.Err()
    }

    return nil
}

func (b *Backend) DeleteTransaction(ctx context.Context, id string) error {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    res := b.db.Collection(TransactionsCollection).FindOneAndDelete(ctx,
        bson.M{
            "id": uuid.MustParse(id),
        },
    )
    if res.Err() != nil {
        return res.Err()
    }

    return nil
}

func (b *Backend) RestoreTransaction(ctx context.Context, id string) error {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    res := b.db.Collection(TransactionsCollection).FindOneAndUpdate(ctx,
        bson.M{
            "id": uuid.MustParse(id),
        },
        bson.M{
            "$unset": bson.M{
                "deleted_at": "",
                "deleted_by": "",
            },
        },
    )
    if res.Err() != nil {
        return res.Err()
    }

    return nil
}

func (b *Backend) GetDeletedTransactions(ctx context.Context, page uint64, size uint64) ([]*models.Transaction, error) {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    var accs []*models.Transaction
    cursor, err := b.db.Collection(TransactionsCollection).Find(ctx,
        bson.M{
            "deleted_at": bson.M{
                "$ne": nil,
            },
        },
        options.Find().SetSkip(int64(page*size)).SetLimit(int64(size)))
    if err != nil {
        return nil, err
    }

    if err := cursor.All(ctx, &accs); err != nil {
        return nil, err
    }

    return accs, nil
}

func (b *Backend) CountDeletedTransactions(ctx context.Context) (uint64, error) {
    ctx, cancel := b.TimeoutContext(ctx)
    defer cancel()

    count, err := b.db.Collection(TransactionsCollection).CountDocuments(ctx, bson.M{
        "deleted_at": bson.M{
            "$ne": nil,
        },
    })
    if err != nil {
        return 0, err
    }

    return uint64(count), nil
}