github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/publisher/publisher_models.go (about)

     1  package publisher
     2  
     3  import (
     4  	"github.com/machinefi/w3bstream/pkg/depends/base/types"
     5  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
     6  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
     7  	"github.com/machinefi/w3bstream/pkg/models"
     8  )
     9  
    10  type CreateReq struct {
    11  	Name string `json:"name"`
    12  	Key  string `json:"key"`
    13  }
    14  
    15  type UpdateReq struct {
    16  	PublisherID types.SFID `json:"-"`
    17  	Name        string     `json:"name"`
    18  	Key         string     `json:"key"`
    19  }
    20  
    21  type CondArgs struct {
    22  	ProjectIDs   []types.SFID `name:"-"`
    23  	PublisherIDs []types.SFID `in:"query" name:"publisherIDs,omitempty"`
    24  	Names        []string     `in:"query" name:"name,omitempty"`
    25  	Keys         []string     `in:"query" name:"key,omitempty"`
    26  	NameLike     string       `in:"query" name:"name,omitempty"`
    27  	LNameLike    string       `in:"query" name:"lname,omitempty"`
    28  	RNameLike    string       `in:"query" name:"rname,omitempty"`
    29  }
    30  
    31  func (r *CondArgs) Condition() builder.SqlCondition {
    32  	var (
    33  		m = &models.Publisher{}
    34  		c []builder.SqlCondition
    35  	)
    36  
    37  	if len(r.ProjectIDs) > 0 {
    38  		if len(r.ProjectIDs) == 1 {
    39  			c = append(c, m.ColProjectID().Eq(r.ProjectIDs[0]))
    40  		} else {
    41  			c = append(c, m.ColProjectID().In(r.ProjectIDs))
    42  		}
    43  	}
    44  	if len(r.PublisherIDs) > 0 {
    45  		if len(r.PublisherIDs) == 1 {
    46  			c = append(c, m.ColPublisherID().Eq(r.PublisherIDs[0]))
    47  		} else {
    48  			c = append(c, m.ColPublisherID().In(r.PublisherIDs))
    49  		}
    50  	}
    51  	if len(r.Names) > 0 {
    52  		if len(r.PublisherIDs) == 1 {
    53  			c = append(c, m.ColName().Eq(r.Names[0]))
    54  		} else {
    55  			c = append(c, m.ColName().In(r.Names))
    56  		}
    57  	}
    58  	if len(r.Keys) > 0 {
    59  		if len(r.PublisherIDs) == 1 {
    60  			c = append(c, m.ColKey().Eq(r.Keys[0]))
    61  		} else {
    62  			c = append(c, m.ColKey().In(r.Keys))
    63  		}
    64  	}
    65  	if r.NameLike != "" {
    66  		c = append(c, m.ColName().Like(r.NameLike))
    67  	}
    68  	if r.LNameLike != "" {
    69  		c = append(c, m.ColName().LLike(r.LNameLike))
    70  	}
    71  	if r.RNameLike != "" {
    72  		c = append(c, m.ColName().RLike(r.RNameLike))
    73  	}
    74  
    75  	return builder.And(c...)
    76  }
    77  
    78  type ListReq struct {
    79  	CondArgs
    80  	datatypes.Pager
    81  }
    82  
    83  func (r *ListReq) Additions() builder.Additions {
    84  	m := &models.Publisher{}
    85  	return builder.Additions{
    86  		builder.OrderBy(
    87  			builder.DescOrder(m.ColUpdatedAt()),
    88  			builder.DescOrder(m.ColCreatedAt()),
    89  		),
    90  		r.Pager.Addition(),
    91  	}
    92  }
    93  
    94  type ListRsp struct {
    95  	Data  []models.Publisher `json:"data"`
    96  	Total int64              `json:"total"`
    97  }
    98  
    99  type Detail struct {
   100  	ProjectName string `json:"projectName" db:"f_project_name"`
   101  	models.Publisher
   102  	datatypes.OperationTimes
   103  }
   104  
   105  type ListDetailRsp struct {
   106  	Total int64     `json:"total"`
   107  	Data  []*Detail `json:"data"`
   108  }