68 lines
1.9 KiB
Go
Executable File
68 lines
1.9 KiB
Go
Executable File
package gmodel
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (a *FsAddressModel) GetOne(ctx context.Context, id int64, userId int64) (resp FsAddress, err error) {
|
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`id` = ? and `user_id` = ? and `status` = ? ", id, userId, 1).Take(&resp).Error
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return FsAddress{}, err
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []FsAddress, err error) {
|
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = ?", userId, 1).Order("`id` DESC").Find(&resp).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (result *FsAddress, err error) {
|
|
|
|
err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
// now := time.Now().Unix()
|
|
result = &FsAddress{
|
|
UserId: address.UserId,
|
|
Name: address.Name,
|
|
FirstName: address.FirstName,
|
|
LastName: address.LastName,
|
|
Mobile: address.Mobile,
|
|
Street: address.Street,
|
|
Suite: address.Suite,
|
|
City: address.City,
|
|
State: address.State,
|
|
Country: address.Country,
|
|
ZipCode: address.ZipCode,
|
|
Status: address.Status,
|
|
IsDefault: address.IsDefault,
|
|
}
|
|
|
|
return tx.Create(result).Error
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (a *FsAddressModel) UpdateAddAddress(ctx context.Context, address *FsAddress) (err error) {
|
|
err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
if *address.IsDefault > 0 {
|
|
err = tx.Where("user_id = ? and is_default = 1", *address.UserId).Update("is_default", 0).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return a.db.WithContext(ctx).Where("id = ? and user_id = ?", address.Id, address.UserId).Omit("id", "user_id").Updates(address).Error
|
|
})
|
|
return err
|
|
}
|