68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package catalog
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.vdhsn.com/barretthousen/barretthousen/src/lib/kernel"
|
|
)
|
|
|
|
var ErrDuplicateAuctionImported = errors.New("this auction's fingerprint matches one that has already been imported")
|
|
|
|
type (
|
|
Domain struct {
|
|
Storage
|
|
}
|
|
|
|
Storage interface {
|
|
GetUpcoming(context.Context, UpcomingQuery) ([]Auction, int, error)
|
|
CreateUpcoming(context.Context, Auction) error
|
|
}
|
|
|
|
UpcomingQuery struct {
|
|
Term string
|
|
StartDateFilter time.Time
|
|
}
|
|
|
|
ImportAuctionMessage struct {
|
|
Auction
|
|
}
|
|
|
|
AuctionCreated struct {
|
|
Auction
|
|
Duplicate bool
|
|
}
|
|
|
|
UpcomingResults struct {
|
|
Page int
|
|
Total int
|
|
Results []Auction
|
|
}
|
|
)
|
|
|
|
func (d *Domain) GetUpcoming(ctx context.Context, q UpcomingQuery) (results UpcomingResults, err error) {
|
|
if results.Results, results.Total, err = d.Storage.GetUpcoming(ctx, q); err != nil {
|
|
err = fmt.Errorf("could not get upcoming from storage: %w", err)
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (d *Domain) ImportAuction(ctx context.Context, in ImportAuctionMessage) (event AuctionCreated, err error) {
|
|
if err = d.Storage.CreateUpcoming(ctx, in.Auction); err != nil && !errors.Is(err, ErrDuplicateAuctionImported) {
|
|
err = fmt.Errorf("could not import auction: %w", err)
|
|
return
|
|
}
|
|
|
|
kernel.TraceLog.Printf("Imported: %+v", in.Auction)
|
|
|
|
event = AuctionCreated{
|
|
Auction: in.Auction,
|
|
Duplicate: errors.Is(err, ErrDuplicateAuctionImported),
|
|
}
|
|
return
|
|
}
|