Skip to content

Instantly share code, notes, and snippets.

@eftalyurtseven
Last active January 26, 2022 11:40
Show Gist options
  • Save eftalyurtseven/6e7339811c9dd24e4fdcc217fcb9a203 to your computer and use it in GitHub Desktop.
Save eftalyurtseven/6e7339811c9dd24e4fdcc217fcb9a203 to your computer and use it in GitHub Desktop.
package order
import (
"context"
"errors"
"fmt"
"time"
"github.com/jackc/pgx/v4"
)
type orderRequest struct {
TableNumber string `json:"table_number"`
Products map[string]int `json:"products"`
}
// Make checks orderRequest and write to the orders table
func (o *OrderService) Make(ctx context.Context, req orderRequest) (*orderResponse, error) {
if len(req.TableNumber) == 0 {
return nil, errors.New("CustomerID varible is not set!")
}
if len(req.Products) == 0 {
return nil, errors.New("Orders variable is not set!")
}
tx, err := o.db.BeginTx(ctx, pgx.TxOptions{})
if err != nil {
return nil, fmt.Errorf("db.BeginTx failed with error: %w", err)
}
insertQuery := `INSERT INTO orders(time, table_number, product_id, product_total) VALUES($1, $2, $3)`
for k, v := range req.Products {
_, err := tx.Exec(ctx, insertQuery, time.Now().Format(time.RFC3339), req.TableNumber, k, v)
if err != nil {
tx.Rollback(ctx)
return nil, fmt.Errorf("tx.Exec failed with error: %w", err)
}
}
if err := tx.Commit(ctx); err != nil {
return nil, fmt.Errorf("tx.Commit failed with err: %w", err)
}
return &orderResponse{}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment