131 lines
3.1 KiB
Go
131 lines
3.1 KiB
Go
package profile
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
messages "git.k6n.net/mats/go-cart-actor/proto/profile"
|
|
)
|
|
|
|
// HandleAddAddress adds a new address to the user's address book.
|
|
func HandleAddAddress(p *ProfileGrain, m *messages.AddAddress) error {
|
|
if m == nil {
|
|
return fmt.Errorf("AddAddress: nil payload")
|
|
}
|
|
if m.Address == nil {
|
|
return fmt.Errorf("AddAddress: address is required")
|
|
}
|
|
|
|
id := p.NextAddrId()
|
|
addr := protoToStored(id, m.Address)
|
|
if addr.IsDefaultShipping {
|
|
p.clearDefaultShipping()
|
|
}
|
|
if addr.IsDefaultBilling {
|
|
p.clearDefaultBilling()
|
|
}
|
|
p.Addresses = append(p.Addresses, *addr)
|
|
return nil
|
|
}
|
|
|
|
// HandleUpdateAddress updates an existing address (matched by id).
|
|
func HandleUpdateAddress(p *ProfileGrain, m *messages.UpdateAddress) error {
|
|
if m == nil {
|
|
return fmt.Errorf("UpdateAddress: nil payload")
|
|
}
|
|
existing := p.findAddress(m.Id)
|
|
if existing == nil {
|
|
return fmt.Errorf("UpdateAddress: address %d not found", m.Id)
|
|
}
|
|
if m.Label != nil {
|
|
existing.Label = *m.Label
|
|
}
|
|
if m.FullName != nil {
|
|
existing.FullName = *m.FullName
|
|
}
|
|
if m.AddressLine1 != nil {
|
|
existing.AddressLine1 = *m.AddressLine1
|
|
}
|
|
if m.AddressLine2 != nil {
|
|
existing.AddressLine2 = *m.AddressLine2
|
|
}
|
|
if m.City != nil {
|
|
existing.City = *m.City
|
|
}
|
|
if m.State != nil {
|
|
existing.State = *m.State
|
|
}
|
|
if m.Zip != nil {
|
|
existing.Zip = *m.Zip
|
|
}
|
|
if m.Country != nil {
|
|
existing.Country = *m.Country
|
|
}
|
|
if m.Phone != nil {
|
|
existing.Phone = *m.Phone
|
|
}
|
|
if m.IsDefaultShipping != nil {
|
|
if *m.IsDefaultShipping {
|
|
p.clearDefaultShipping()
|
|
}
|
|
existing.IsDefaultShipping = *m.IsDefaultShipping
|
|
}
|
|
if m.IsDefaultBilling != nil {
|
|
if *m.IsDefaultBilling {
|
|
p.clearDefaultBilling()
|
|
}
|
|
existing.IsDefaultBilling = *m.IsDefaultBilling
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// HandleRemoveAddress removes an address from the address book.
|
|
func HandleRemoveAddress(p *ProfileGrain, m *messages.RemoveAddress) error {
|
|
if m == nil {
|
|
return fmt.Errorf("RemoveAddress: nil payload")
|
|
}
|
|
idx := -1
|
|
for i, a := range p.Addresses {
|
|
if a.Id == m.Id {
|
|
idx = i
|
|
break
|
|
}
|
|
}
|
|
if idx == -1 {
|
|
return fmt.Errorf("RemoveAddress: address %d not found", m.Id)
|
|
}
|
|
p.Addresses = append(p.Addresses[:idx], p.Addresses[idx+1:]...)
|
|
return nil
|
|
}
|
|
|
|
// clearDefaultShipping unsets IsDefaultShipping on all addresses.
|
|
func (p *ProfileGrain) clearDefaultShipping() {
|
|
for i := range p.Addresses {
|
|
p.Addresses[i].IsDefaultShipping = false
|
|
}
|
|
}
|
|
|
|
// clearDefaultBilling unsets IsDefaultBilling on all addresses.
|
|
func (p *ProfileGrain) clearDefaultBilling() {
|
|
for i := range p.Addresses {
|
|
p.Addresses[i].IsDefaultBilling = false
|
|
}
|
|
}
|
|
|
|
// protoToStored converts a proto Address to a stored StoredAddress.
|
|
func protoToStored(id uint32, m *messages.Address) *StoredAddress {
|
|
return &StoredAddress{
|
|
Id: id,
|
|
Label: m.Label,
|
|
FullName: m.FullName,
|
|
AddressLine1: m.AddressLine1,
|
|
AddressLine2: m.GetAddressLine2(),
|
|
City: m.City,
|
|
State: m.State,
|
|
Zip: m.Zip,
|
|
Country: m.Country,
|
|
Phone: m.GetPhone(),
|
|
IsDefaultShipping: m.IsDefaultShipping,
|
|
IsDefaultBilling: m.IsDefaultBilling,
|
|
}
|
|
}
|