Wednesday, June 26, 2024
HomeGolangTransactions in go - Code Evaluation

Transactions in go – Code Evaluation


I’m growing an app for a desktop software for a fitness center in order I progress the app grows with out specializing in oop and issues like that, holding it so simple as potential and making it work, so I’ve reached the restrict of my data, now I’ve made my transaction to register a person, There isn’t a authentication or so for the reason that person will register the shopper so I don’t know if I’m dealing with my transaction accurately or what I can enhance. In a number of phrases, it could assist me rather a lot should you gave me some suggestions

sort userRepository struct {
    storage *postgres.PgxStorage
}

func NewUserRepository(storage *postgres.PgxStorage) person.Repository {
    return &userRepository{storage: storage}
}

func (ur *userRepository) RegisterUser(ctx context.Context, register *entities.RegisterUsertx) (int32, error) {

    tx, err := ur.storage.DBPool.Start(ctx)
    if err != nil {
        log.Printf("error starting transaction: %v", err)
        return 0, fmt.Errorf("start transaction: %w", err)
    }
    defer func() {
        if err != nil {
            tx.Rollback(ctx)
            log.Printf("transaction rolled again on account of error: %v", err)
        }
    }()

    // Registrar usuario
    var userID int32
    question := "INSERT INTO customers (identify, lastname1, lastname2, electronic mail, telephone, created_at) VALUES($1, $2, $3, $4, $5, $6) RETURNING id"
    err = tx.QueryRow(ctx, question, register.Title, register.Lastname1, register.Lastname2, register.Electronic mail, register.Cellphone, register.CreatedAt).Scan(&userID)
    if err != nil {
        log.Printf("error inserting person: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("insert person: %w", err)
    }
    register.ID = userID

    // Crear cuenta
    accountID := uuid.New()
    var account uuid.UUID
    question = "INSERT INTO accounts (user_id, account_id, account_type_id, created_at) VALUES($1, $2, $3, $4) RETURNING account_id"
    err = tx.QueryRow(ctx, question, userID, accountID, register.AccountTypeID, time.Now()).Scan(&account)
    if err != nil {
        log.Printf("error inserting account: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("insert account: %w", err)
    }

    // Obtener la duracion mediante el id de la suscripcion
    var subscriptionDuration int
    question = "SELECT subscription_day FROM subscription_costs WHERE id = $1"
    err = tx.QueryRow(ctx, question, register.SubscriptionCostID).Scan(&subscriptionDuration)
    if err != nil {
        log.Printf("error getting subscription period: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("get subscription period: %w", err)
    }

    startDate := time.Now()
    endDate := startDate.AddDate(0, 0, subscriptionDuration)

    // Crear subscripcion
    question = "INSERT INTO subscriptions (account_id, subscription_cost_id, start_date, end_date) VALUES($1, $2, $3, $4) RETURNING id"
    err = tx.QueryRow(ctx, question, account, register.SubscriptionCostID, startDate, endDate).Scan(&register.SubscriptionID)
    if err != nil {
        log.Printf("error inserting subscription: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("insert subscription: %w", err)
    }

    // Obtener el monto y comparar con el monto de la suscripcion
    var expectedCost float64
    question = "SELECT value from subscription_costs the place id = $1"
    err = tx.QueryRow(ctx, question, register.SubscriptionCostID).Scan(&expectedCost)
    if err != nil {
        log.Printf("error getting subscription value: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("ammount: %w", err)
    }

    if register.Ammount != expectedCost {
        log.Printf("quantity incorrect: anticipated %v, obtained %v", expectedCost, register.Ammount)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("quantity incorrect: %w", err)
    }

    question = "INSERT INTO funds (account_id, payment_type_id, value, payment_date) VALUES($1, $2, $3, $4)"
    _, err = tx.Exec(ctx, question, account, register.PaymentTypeID, register.Ammount, time.Now())
    if err != nil {
        log.Printf("error inserting cost: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("insert cost: %w", err)
    }

    // Obtener el id del standing
    var statusPayment, statusAcccount int32

    question = `SELECT id FROM STATUS WHERE id = 5 OR id = 1`
    rows, err := tx.Question(ctx, question)
    if err != nil {
        log.Printf("error getting statuses: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("get statuses: %w", err)
    }
    defer rows.Shut()

    for rows.Subsequent() {
        var id int32
        if err := rows.Scan(&id); err != nil {
            log.Printf("error scanning standing: %v", err)
            tx.Rollback(ctx)
            return 0, fmt.Errorf("scan standing: %w", err)
        }

        if id == 5 {
            statusPayment = id
        } else {
            statusAcccount = id
        }
    }
    if err := rows.Err(); err != nil {
        log.Printf("rows error: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("rows error: %w", err)
    }

    question = `SELECT id from standing the place id = 5`
    err = tx.QueryRow(ctx, question).Scan(&statusPayment)
    if err != nil {
        log.Printf("error getting standing: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("get standing: %w", err)
    }

    question = "UPDATE funds SET status_id = $1 WHERE account_id = $2"
    _, err = tx.Exec(ctx, question, statusPayment, &account)
    if err != nil {
        log.Printf("error updating cost standing: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("insert standing: %w", err)
    }

    question = "UPDATE accounts SET subscription_id = $1, status_id = $2 WHERE account_id = $3"
    _, err = tx.Exec(ctx, question, &register.SubscriptionID, statusAcccount, &account)
    if err != nil {
        log.Printf("error updating account standing: %v", err)
        tx.Rollback(ctx)
        return 0, fmt.Errorf("get standing: %w", err)
    }

    err = tx.Commit(ctx)
    if err != nil {
        log.Printf("error committing transaction: %v", err)
        return 0, fmt.Errorf("commit transaction: %w", err)
    }

    return userID, nil
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments