Skip to content

Commit

Permalink
feat: add category, account apis; monitor price update events (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Nov 6, 2023
1 parent ba6b0d5 commit 9c51b37
Show file tree
Hide file tree
Showing 46 changed files with 2,943 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cmd/mind-marketplace-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func main() {

metricServer := metric.NewMetricService(config)

categoryDao := dao.NewDbCategoryDao(db)
itemDao := dao.NewDbItemDao(db)

gnfdBlockDao := dao.NewDbGnfdBlockDao(db)
gnfdClient := monitor.NewGnfdCompositClients(config.GnfdRpcAddrs, config.GnfdChainId, false)
gnfdProcessor := monitor.NewGnfdBlockProcessor(gnfdClient, gnfdBlockDao, itemDao, db, metricServer,
gnfdProcessor := monitor.NewGnfdBlockProcessor(gnfdClient, gnfdBlockDao, categoryDao, itemDao, db, metricServer,
config.GroupBucketRegex, config.GroupBucketPrefix, config.GroupObjectRegex, config.GroupObjectPrefix)
gnfdMonitor := monitor.NewMonitor(gnfdProcessor, config.GnfdStartHeight)

Expand Down
10 changes: 10 additions & 0 deletions dao/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type AccountDao interface {
Create(context context.Context, account *database.Account) error
Get(context context.Context, id int64) (database.Account, error)
GetByAddress(context context.Context, address string) (database.Account, error)
GetByUserName(context context.Context, userName string) (database.Account, error)
Update(context context.Context, account *database.Account) error
}

Expand Down Expand Up @@ -48,6 +49,15 @@ func (dao *dbAccountDao) GetByAddress(context context.Context, address string) (
return account, nil
}

func (dao *dbAccountDao) GetByUserName(context context.Context, userName string) (database.Account, error) {
var account database.Account

if err := dao.db.Where(&database.Account{UserName: userName}).Take(&account).Error; err != nil {
return account, err
}
return account, nil
}

func (dao *dbAccountDao) Update(context context.Context, account *database.Account) error {
if err := dao.db.Save(account).Error; err != nil {
return err
Expand Down
47 changes: 47 additions & 0 deletions dao/category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dao

import (
"context"
"gorm.io/gorm"

"github.com/bnb-chain/mind-marketplace-backend/database"
)

type CategoryDao interface {
Create(context context.Context, category *database.Category) error
Get(context context.Context, name string) (database.Category, error)
GetAll(context context.Context) ([]database.Category, error)
}

type dbCategoryDao struct {
db *gorm.DB
}

func NewDbCategoryDao(db *gorm.DB) CategoryDao {
return &dbCategoryDao{
db: db,
}
}

func (dao *dbCategoryDao) Create(context context.Context, category *database.Category) error {
if err := dao.db.Omit("Items").Create(category).Error; err != nil {
return err
}
return nil
}

func (dao *dbCategoryDao) Get(context context.Context, name string) (database.Category, error) {
var category = database.Category{}
if err := dao.db.Raw("select from categories where lower(name) = ?", name).Take(&category).Error; err != nil {
return category, err
}
return category, nil
}

func (dao *dbCategoryDao) GetAll(context context.Context) ([]database.Category, error) {
var categories []database.Category
if err := dao.db.Omit("Items").Find(&categories).Error; err != nil {
return categories, err
}
return categories, nil
}
9 changes: 7 additions & 2 deletions dao/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ItemDao interface {
Update(context context.Context, collection *database.Item) error
Get(context context.Context, id int64, includeAll bool) (database.Item, error)
GetByGroupId(context context.Context, groupId int64, includeAll bool) (database.Item, error)
Search(context context.Context, address, keyword string, includeAll bool, sort string, offset, limit int) (int64, []*database.Item, error)
Search(context context.Context, categoryId int64, address, keyword string, includeAll bool, sort string, offset, limit int) (int64, []*database.Item, error)
}

type dbItemDao struct {
Expand Down Expand Up @@ -97,10 +97,15 @@ func (dao *dbItemDao) GetByGroupId(context context.Context, groupId int64, inclu
return item, nil
}

func (dao *dbItemDao) Search(context context.Context, address, keyword string, includeAll bool, sort string, offset, limit int) (total int64, items []*database.Item, err error) {
func (dao *dbItemDao) Search(context context.Context, categoryId int64, address, keyword string, includeAll bool, sort string, offset, limit int) (total int64, items []*database.Item, err error) {
rawSql := " where 1 = 1 "
parameters := make([]interface{}, 0)

if categoryId > 0 {
rawSql = rawSql + ` and category_id = ?`
parameters = append(parameters, categoryId)
}

if len(address) > 0 {
rawSql = rawSql + ` and owner_address = ?`
parameters = append(parameters, address)
Expand Down
3 changes: 3 additions & 0 deletions database/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import "time"
type Account struct {
Id int64 `json:"id" gorm:"primaryKey"`
Address string `json:"address" gorm:"index:idx_account_address"`
UserName string `json:"user_name" gorm:"uniqueIndex:idx_account_user_name;size:32"`
Avatar string `json:"avatar" gorm:"size:256"`
TwitterUserName string `json:"twitter"`
TwitterVerified bool `json:"twitter_verified"`
InstagramUserName string `json:"instagram"`
InstagramVerified bool `json:"instagram_verified"`
Bio string `json:"bio" gorm:"size:1024"`
CreatedAt time.Time `json:"created_at" gorm:"NOT NULL;type:TIMESTAMP;default:CURRENT_TIMESTAMP;<-:create"`
UpdatedAt time.Time `json:"updated_at" gorm:"NOT NULL;type:TIMESTAMP;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP"`
}
8 changes: 8 additions & 0 deletions database/category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package database

type Category struct {
Id int64 `json:"id" gorm:"primaryKey;not null;autoIncrement:false"`
Name string `json:"name" gorm:"not null;size:32"`

Items []*Item `json:"items" gorm:"foreignKey:CategoryId"`
}
3 changes: 3 additions & 0 deletions database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func ConnectDBWithConfig(config *util.DBConfig) (*gorm.DB, error) {
if err = db.AutoMigrate(&Account{}); err != nil {
panic(err)
}
if err = db.AutoMigrate(&Category{}); err != nil {
panic(err)
}
if err = db.AutoMigrate(&Item{}); err != nil {
panic(err)
}
Expand Down
10 changes: 10 additions & 0 deletions database/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
insert into categories(id, name) values (100, 'Uncategorized');
insert into categories(id, name) values (1, 'AI Model');
insert into categories(id, name) values (2, 'Code Resource');
insert into categories(id, name) values (3, 'Digital Media');
insert into categories(id, name) values (4, 'Literary Creation');
insert into categories(id, name) values (5, 'Scientific Data');
insert into categories(id, name) values (6, 'Game');
insert into categories(id, name) values (7, 'AIGC');
insert into categories(id, name) values (8, 'Education');
insert into categories(id, name) values (9, 'Finance');
1 change: 1 addition & 0 deletions database/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (

type Item struct {
Id int64 `json:"id" gorm:"primaryKey"`
CategoryId int64 `json:"category_id"`
Type int8 `json:"type"` // collection (bucket) or data (object)
Name string `json:"name" gorm:"index:idx_item_name;not null;size:256"` // bucket name or object name
ResourceId int64 `json:"resource_id" gorm:"index:idx_item_resource_id"` // bucket id or object id
Expand Down
8 changes: 8 additions & 0 deletions models/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions models/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9c51b37

Please sign in to comment.