diff --git a/pkg/core/db/models.go b/pkg/core/db/models.go index 6ee63747..0bf49d1d 100644 --- a/pkg/core/db/models.go +++ b/pkg/core/db/models.go @@ -69,6 +69,21 @@ type CoreBlock struct { CreatedAt pgtype.Timestamp } +type CorePlay struct { + Rowid int32 + UserID string + TrackID string + ListenerAddress string + Cid string + City string + Country string + Region string + Signer string + Signature string + Block int64 + CreatedAt pgtype.Timestamptz +} + type CoreTransaction struct { Rowid int64 BlockID int64 diff --git a/pkg/core/db/sql/migrations/00015_entity_tables.sql b/pkg/core/db/sql/migrations/00015_entity_tables.sql new file mode 100644 index 00000000..f9b4b5bd --- /dev/null +++ b/pkg/core/db/sql/migrations/00015_entity_tables.sql @@ -0,0 +1,21 @@ +-- +migrate Up +create table if not exists core_plays( + rowid serial primary key, + user_id text not null, + track_id text not null, + + -- future fields + listener_address text not null, + cid text not null, + + city text not null, + country text not null, + region text not null, + signer text not null, + signature text not null, + block bigint not null, + created_at timestamptz not null +) + +-- +migrate Down +drop table if exists core_plays; diff --git a/pkg/core/db/sql/writes.sql b/pkg/core/db/sql/writes.sql index 2db647f9..b310728a 100644 --- a/pkg/core/db/sql/writes.sql +++ b/pkg/core/db/sql/writes.sql @@ -63,3 +63,7 @@ where block_height = $3 and address = $4; -- name: InsertFailedStorageProof :exec insert into storage_proofs (block_height, address, status) values ($1, $2, 'fail'); + +-- name: InsertPlayRecord :exec +insert into core_plays (user_id, track_id, listener_address, cid, city, country, region, signer, signature, block, created_at) +values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) on conflict do nothing; diff --git a/pkg/core/db/writes.sql.go b/pkg/core/db/writes.sql.go index c8b9e2db..82233093 100644 --- a/pkg/core/db/writes.sql.go +++ b/pkg/core/db/writes.sql.go @@ -87,6 +87,42 @@ func (q *Queries) InsertFailedStorageProof(ctx context.Context, arg InsertFailed return err } +const insertPlayRecord = `-- name: InsertPlayRecord :exec +insert into core_plays (user_id, track_id, listener_address, cid, city, country, region, signer, signature, block, created_at) +values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) on conflict do nothing +` + +type InsertPlayRecordParams struct { + UserID string + TrackID string + ListenerAddress string + Cid string + City string + Country string + Region string + Signer string + Signature string + Block int64 + CreatedAt pgtype.Timestamptz +} + +func (q *Queries) InsertPlayRecord(ctx context.Context, arg InsertPlayRecordParams) error { + _, err := q.db.Exec(ctx, insertPlayRecord, + arg.UserID, + arg.TrackID, + arg.ListenerAddress, + arg.Cid, + arg.City, + arg.Country, + arg.Region, + arg.Signer, + arg.Signature, + arg.Block, + arg.CreatedAt, + ) + return err +} + const insertRegisteredNode = `-- name: InsertRegisteredNode :exec insert into core_validators(pub_key, endpoint, eth_address, comet_address, comet_pub_key, eth_block, node_type, sp_id) values ($1, $2, $3, $4, $5, $6, $7, $8) diff --git a/pkg/core/server/plays.go b/pkg/core/server/plays.go index a1466953..1b38d4f0 100644 --- a/pkg/core/server/plays.go +++ b/pkg/core/server/plays.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/AudiusProject/audiusd/pkg/core/db" "github.com/AudiusProject/audiusd/pkg/core/gen/core_proto" ) @@ -22,5 +23,15 @@ func (s *Server) finalizePlayTransaction(ctx context.Context, stx *core_proto.Si return nil, errors.New("invalid play tx") } + for _, play := range tx.Plays { + if err := s.getDb().InsertPlayRecord(ctx, db.InsertPlayRecordParams{ + TrackID: tx., + UserID: tx.UserId, + }); err != nil { + s.logger.Errorf("error inserting play record: %v", err) + } + + } + return tx, nil }