From c983677da3f4244017a729f778b8b619c0fd7b24 Mon Sep 17 00:00:00 2001 From: gearnode Date: Thu, 6 Feb 2025 10:58:34 -0800 Subject: [PATCH] Add create vendor Signed-off-by: gearnode --- pkg/probo/coredata/vendor.go | 33 ++++++++++++++++ pkg/probo/create_vendor.go | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 pkg/probo/create_vendor.go diff --git a/pkg/probo/coredata/vendor.go b/pkg/probo/coredata/vendor.go index 63f90a21c..a40379244 100644 --- a/pkg/probo/coredata/vendor.go +++ b/pkg/probo/coredata/vendor.go @@ -91,6 +91,39 @@ LIMIT 1; return nil } +func (v Vendor) Insert( + ctx context.Context, + conn pg.Conn, +) error { + q := ` +INSERT INTO + vendors ( + id, + organization_id, + name, + created_at, + updated_at + ) +VALUES ( + @vendor_id, + @organization_id, + @name, + @created_at, + @updated_at +) +` + + args := pgx.NamedArgs{ + "vendor_id": v.ID, + "organization_id": v.OrganizationID, + "name": v.Name, + "created_at": v.CreatedAt, + "updated_at": v.UpdatedAt, + } + _, err := conn.Exec(ctx, q, args) + return err +} + func (v *Vendors) LoadByOrganizationID( ctx context.Context, conn pg.Conn, diff --git a/pkg/probo/create_vendor.go b/pkg/probo/create_vendor.go new file mode 100644 index 000000000..b49631fd7 --- /dev/null +++ b/pkg/probo/create_vendor.go @@ -0,0 +1,73 @@ +// Copyright (c) 2025 Probo Inc . +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +package probo + +import ( + "context" + "fmt" + "time" + + "github.com/getprobo/probo/pkg/gid" + "github.com/getprobo/probo/pkg/probo/coredata" + "go.gearno.de/kit/pg" +) + +type ( + CreateVendorRequest struct { + OrganizationID gid.GID + Name string + } +) + +func (s Service) CreateVendor( + ctx context.Context, + req CreateVendorRequest, +) (*coredata.Vendor, error) { + now := time.Now() + vendorID, err := gid.NewGID(coredata.VendorEntityType) + if err != nil { + return nil, fmt.Errorf("cannot create vendor global id: %w", err) + } + + organization := &coredata.Organization{} + vendor := &coredata.Vendor{ + ID: vendorID, + OrganizationID: req.OrganizationID, + Name: req.Name, + CreatedAt: now, + UpdatedAt: now, + } + + err = s.pg.WithTx( + ctx, + func(conn pg.Conn) error { + if err := organization.LoadByID(ctx, conn, s.scope, req.OrganizationID); err != nil { + return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err) + } + + if err := vendor.Insert(ctx, conn); err != nil { + return fmt.Errorf("cannot insert vendor: %w", err) + } + + return nil + }, + ) + + if err != nil { + return nil, err + } + + return vendor, nil +}