diff --git a/pkg/repository/database/season.go b/pkg/repository/database/season.go index 7a38cb6..970b07c 100644 --- a/pkg/repository/database/season.go +++ b/pkg/repository/database/season.go @@ -3,7 +3,6 @@ package database import ( "context" "database/sql" - "errors" "fmt" "strings" @@ -11,6 +10,7 @@ import ( "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 { @@ -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 } diff --git a/pkg/repository/database/series.go b/pkg/repository/database/series.go index c259ad4..79d0a31 100644 --- a/pkg/repository/database/series.go +++ b/pkg/repository/database/series.go @@ -3,7 +3,6 @@ package database import ( "context" "database/sql" - "errors" "fmt" "strings" @@ -11,6 +10,7 @@ import ( "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 { @@ -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) }