mirror of
https://github.com/bestnite/go-igdb.git
synced 2025-05-18 00:01:51 +08:00
u
This commit is contained in:
parent
5cb4ab4c61
commit
3df648929d
@ -42,14 +42,30 @@ func (b *BaseEndpoint[T]) GetByID(id uint64) (*T, error) {
|
||||
}
|
||||
|
||||
func (b *BaseEndpoint[T]) GetByIDs(ids []uint64) ([]*T, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, fmt.Errorf("ids cant be empty")
|
||||
}
|
||||
batches := make([][]uint64, 0)
|
||||
for i := 0; i < len(ids); i += 500 {
|
||||
end := min(i+500, len(ids))
|
||||
batches = append(batches, ids[i:end])
|
||||
}
|
||||
res := []*T{}
|
||||
for _, batch := range batches {
|
||||
builder := strings.Builder{}
|
||||
for i, v := range ids {
|
||||
for i, v := range batch {
|
||||
if i > 0 {
|
||||
builder.WriteByte(',')
|
||||
}
|
||||
builder.WriteString(strconv.FormatUint(v, 10))
|
||||
}
|
||||
return b.Query(fmt.Sprintf("where id = (%s); fields *;", builder.String()))
|
||||
batchRes, err := b.Query(fmt.Sprintf("where id = (%s); fields *; limit 500;", builder.String()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = append(res, batchRes...)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (b *BaseEndpoint[T]) Count() (uint64, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user