48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
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
|
|
}
|