CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/golang-gorm-io--gorm

tessl install tessl/golang-gorm-io--gorm@1.31.0

Full-featured ORM library for Golang with associations, hooks, transactions, migrations, and developer-friendly chainable API

Agent Success

Agent success rate when using this tile

81%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.13x

Baseline

Agent success rate without this tile

72%

task.mdevals/scenario-8/

Order Processing with Savepoints

Build an order processing function that uses transaction savepoints to handle failures gracefully.

Requirements

Process an order containing multiple items within a single transaction. For each item, create a savepoint before attempting to add it. If adding an item fails (e.g., insufficient inventory), roll back to that savepoint and skip the item. Successfully added items should be preserved.

The function should:

  1. Start a transaction
  2. Create an order record
  3. For each requested item:
    • Create a savepoint
    • Attempt to add a line item and update inventory
    • If successful, continue to the next item
    • If failed, roll back to the savepoint and skip this item
  4. Calculate and update the order total
  5. Commit the transaction if at least one item was added successfully
  6. Roll back everything if no items could be added

Database Schema

Use the following models:

type Order struct {
    ID          uint
    CustomerID  uint
    TotalAmount float64
    Status      string
    CreatedAt   time.Time
}

type LineItem struct {
    ID        uint
    OrderID   uint
    ProductID uint
    Quantity  int
    Price     float64
}

type Inventory struct {
    ID        uint
    ProductID uint
    Quantity  int
}

Implementation

@generates

Implement a function with the following signature:

package main

import (
    "gorm.io/gorm"
)

// ProcessOrder processes an order with savepoint-based error recovery.
// Returns the created order ID and any error encountered.
// If no items can be added, returns an error and rolls back everything.
func ProcessOrder(db *gorm.DB, customerID uint, items []OrderItem) (uint, error)

// OrderItem represents a requested order item
type OrderItem struct {
    ProductID uint
    Quantity  int
    Price     float64
}

Test Cases

  • Processing three items with sufficient inventory creates all three line items and commits the transaction @test
  • Processing three items where the second item has insufficient inventory creates two line items (first and third) and skips the second @test
  • Processing three items with no inventory for any item rolls back the entire transaction and returns an error @test

Dependencies { .dependencies }

gorm.io/gorm { .dependency }

Provides ORM functionality and transaction management with savepoint support.

@satisfied-by

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
golangpkg:golang/gorm.io/gorm@v1.31.1
tile.json