Skip to content

Commit

Permalink
BatchGetの実装
Browse files Browse the repository at this point in the history
  • Loading branch information
ebakazu committed Feb 1, 2024
1 parent 1838590 commit 7789fcb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
44 changes: 42 additions & 2 deletions pkg/repository/database/season.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package database
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"

"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/entity"
"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/errcode"
"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/repository"
"github.com/aws/aws-xray-sdk-go/xray"
"go.uber.org/multierr"
)

type Season struct {
Expand Down Expand Up @@ -123,5 +123,45 @@ func (e *Season) BatchGet(ctx context.Context, ids []string) (entity.Seasons, er
ctx, seg := xray.BeginSubsegment(ctx, "database.Season#BatchGet")
defer seg.Close(nil)

return nil, errcode.New(errors.New("not implemtented yet"))
if len(ids) <= 0 {
return nil, nil
}

fields := []string{
"seasonID",
"seriesID",
"displayName",
"imageURL",
"displayOrder",
}

query := fmt.Sprintf(
"SELECT %s FROM seasons",
strings.Join(fields, ","),
)
rows, err := e.db.QueryContext(ctx, query, ids)
if err != nil {
return nil, errcode.New(err)
}

var seasons entity.Seasons
var multiErr error
for rows.Next() {
var s entity.Season
if err = rows.Scan(&s); err != nil {
multiErr = multierr.Append(multiErr, err)
continue
}
seasons = append(seasons, &s)
}

if cerr := rows.Close(); cerr != nil {
return nil, errcode.New(cerr)
}

if multiErr != nil {
return nil, errcode.New(multiErr)
}

return seasons, nil
}
31 changes: 29 additions & 2 deletions pkg/repository/database/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package database
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"

"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/entity"
"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/errcode"
"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/repository"
"github.com/aws/aws-xray-sdk-go/xray"
"go.uber.org/multierr"
)

type Series struct {
Expand Down Expand Up @@ -102,5 +102,32 @@ func (e *Series) BatchGet(ctx context.Context, ids []string) (entity.SeriesMulti
ctx, seg := xray.BeginSubsegment(ctx, "database.Series#BatchGet")
defer seg.Close(nil)

return nil, errcode.New(errors.New("not implemtented yet"))
fields := []string{"id", "displayName", "description", "imageUrl", "genreId"}
query := fmt.Sprintf( // TODO: fix query
"SELECT %s FROM series",
strings.Join(fields, ", "),
)

rows, err := e.db.QueryContext(ctx, query, ids)

var seriesMulti entity.SeriesMulti
var multiErr error
for rows.Next() {
var series entity.Series
if err := rows.Scan(&series); err != nil {
multiErr = multierr.Append(multiErr, err)
continue
}
seriesMulti = append(seriesMulti, &series)
}

if err := rows.Close(); err != nil {
return nil, errcode.New(err)
}

if multiErr != nil {
return nil, errcode.New(multiErr)
}

return seriesMulti, errcode.New(err)
}

0 comments on commit 7789fcb

Please sign in to comment.