initial commit

This commit is contained in:
2025-02-03 18:16:07 -05:00
commit 8ef3c029f2
19 changed files with 1354 additions and 0 deletions

47
models/merchants.go Normal file
View File

@ -0,0 +1,47 @@
package models
import (
"context"
"time"
"github.com/uptrace/bun"
)
type Merchant struct {
bun.BaseModel `bun:"table:merchants"`
MerchantID int64 `bun:",pk,autoincrement" json:"merchant_id" yaml:"merchant_id"`
Title string `bun:"title" json:"title" yaml:"title"`
Address string `bun:"address,type:text,unique" json:"address,omitempty" yaml:"address,omitempty"`
Phone string `bun:"phone,type:text,unique" json:"phone,omitempty" yaml:"phone,omitempty"`
Created time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"created" yaml:"created"`
Updated time.Time `json:"updated" yaml:"updated"`
}
var _ bun.AfterCreateTableHook = (*Merchant)(nil)
func (*Merchant) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().Model((*Merchant)(nil)).Index("idx_merchant_title").Column("title").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*Merchant)(nil)).Index("idx_merchant_address").Column("address").Exec(ctx)
if err != nil {
return err
}
_, err = query.DB().NewCreateIndex().Model((*Merchant)(nil)).Index("idx_merchant_phone").Column("phone").Exec(ctx)
if err != nil {
return err
}
return nil
}
// Add .
func (m *Merchant) Add(db *bun.DB, ctx context.Context) error {
_, err := db.NewInsert().Model(m).On("CONFLICT (phone) DO UPDATE").Set("updated = EXCLUDED.updated").Exec(ctx)
// _, err := db.NewInsert().Model(m).Exec(ctx)
if err != nil {
return err
}
return nil
}