Esempio n. 1
0
// New creates o.
func (os Offers) New(o offer.Offer) (offer.Offer, error) {
	o.ID = tigertonic.RandomBase62String(8)
	o.Created = time.Now().UTC()
	o.Deleted = time.Time{}
	o.Updated = time.Time{}
	if err := o.Validate(); err != nil {
		return offer.Offer{}, err
	}
	os.Configuration.OfferRepository.Store(o)
	return o, nil
}
Esempio n. 2
0
// Set updates o.
func (os Offers) Set(o offer.Offer) (offer.Offer, error) {
	var old, ok = os.Configuration.OfferRepository.Load(o.ID)
	if !ok {
		return offer.Offer{}, fmt.Errorf("invalid id: %q", o.ID)
	}
	if !old.Deleted.IsZero() {
		return offer.Offer{}, fmt.Errorf("invalid id: %q", o.ID)
	}
	o.Created = old.Created
	o.Deleted = old.Deleted
	o.Updated = time.Now().UTC()
	if err := o.Validate(); err != nil {
		return offer.Offer{}, err
	}
	os.Configuration.OfferRepository.Store(o)
	return o, nil
}