From 7f5f0305871e9b89ee22bba5ece9fae307957694 Mon Sep 17 00:00:00 2001 From: ebakazu Date: Wed, 31 Jan 2024 15:58:49 +0900 Subject: [PATCH] =?UTF-8?q?listViewingHistories=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/app/app.go | 2 +- pkg/app/http/viewing_history_test.go | 12 +++---- pkg/entity/viewing_history.go | 9 ++++- pkg/repository/database/viewing_history.go | 40 +++++++++++++++++++--- pkg/repository/repository.go | 1 + pkg/usecase/viewing_history.go | 12 +++++-- 6 files changed, 60 insertions(+), 16 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 538a951..5a47239 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -89,7 +89,7 @@ func (a *App) runWithContext(ctx context.Context) (err error) { Series: database.NewSeries(mysql), Season: database.NewSeason(mysql), Genre: database.NewGenre(mysql), - ViewingHistory: database.NewViewingHistory(), + ViewingHistory: database.NewViewingHistory(mysql), } uc := usecase.NewUsecase(database, redis) diff --git a/pkg/app/http/viewing_history_test.go b/pkg/app/http/viewing_history_test.go index 6a3bc6d..277166a 100644 --- a/pkg/app/http/viewing_history_test.go +++ b/pkg/app/http/viewing_history_test.go @@ -34,7 +34,7 @@ func TestCreateViewingHistory(t *testing.T) { name: "failed to UpdateViewingHistory", setup: func(m *mocks) { m.uc.EXPECT().CreateViewingHistory(gomock.Any(), &usecase.CreateViewingHistoryRequest{ - ViewingHistory: &entity.ViewingHistory{ID: viewingHistoryID}, + ViewingHistory: &entity.ViewingHistory{UserID: viewingHistoryID}, }).Return(nil, errcode.NewInternal("error")) }, body: `{"id":"` + viewingHistoryID + `"}`, @@ -44,13 +44,13 @@ func TestCreateViewingHistory(t *testing.T) { name: "success", setup: func(m *mocks) { m.uc.EXPECT().CreateViewingHistory(gomock.Any(), &usecase.CreateViewingHistoryRequest{ - ViewingHistory: &entity.ViewingHistory{ID: viewingHistoryID}, + ViewingHistory: &entity.ViewingHistory{UserID: viewingHistoryID}, }).Return(&usecase.CreateViewingHistoryResponse{ - ViewingHistory: &entity.ViewingHistory{ID: viewingHistoryID}, + ViewingHistory: &entity.ViewingHistory{UserID: viewingHistoryID}, }, nil) }, body: `{"id":"` + viewingHistoryID + `"}`, - expected: &entity.ViewingHistory{ID: viewingHistoryID}, + expected: &entity.ViewingHistory{UserID: viewingHistoryID}, expectedCode: http.StatusOK, }, } @@ -102,10 +102,10 @@ func TestListViewingHistories(t *testing.T) { UserID: userID, EpisodeIDs: episodeIDs, }).Return(&usecase.BatchGetViewingHistoriesResponse{ - ViewingHistories: entity.ViewingHistories{{ID: viewingHistoryID}}, + ViewingHistories: entity.ViewingHistories{{UserID: viewingHistoryID}}, }, nil) }, - expected: &entity.ViewingHistories{{ID: viewingHistoryID}}, + expected: &entity.ViewingHistories{{UserID: viewingHistoryID}}, expectedCode: http.StatusOK, }, } diff --git a/pkg/entity/viewing_history.go b/pkg/entity/viewing_history.go index 0c96834..f3ea660 100644 --- a/pkg/entity/viewing_history.go +++ b/pkg/entity/viewing_history.go @@ -1,7 +1,14 @@ package entity type ViewingHistory struct { - ID string `json:"id"` + UserID string `json:"userID"` + EpisodeID string `json:"episodeID"` + SeriesID string `json:"seriesID"` + SeasonID string `json:"seasonID"` + IsWatched bool `json:"isWatched"` // 視聴完了済み判定フラグ + Position int64 `json:"position"` // どの時点まで番組を見たか + Duration int64 `json:"duration"` // 視聴時間 + LastViewingAt int64 `json:"lastViewingAt"` // 最後の視聴日時 } type ViewingHistories []*ViewingHistory diff --git a/pkg/repository/database/viewing_history.go b/pkg/repository/database/viewing_history.go index 7637ea9..1aa3fb4 100644 --- a/pkg/repository/database/viewing_history.go +++ b/pkg/repository/database/viewing_history.go @@ -2,28 +2,58 @@ package database import ( "context" + "database/sql" "github.com/CyberAgentHack/server-performance-tuning-2023/pkg/entity" + "github.com/CyberAgentHack/server-performance-tuning-2023/pkg/errcode" "github.com/aws/aws-xray-sdk-go/xray" ) type ViewingHistory struct { + db *sql.DB } -func NewViewingHistory() *ViewingHistory { - return &ViewingHistory{} +func NewViewingHistory(db *sql.DB) *ViewingHistory { + return &ViewingHistory{db: db} } func (e *ViewingHistory) Create(ctx context.Context, viewingHistory *entity.ViewingHistory) (*entity.ViewingHistory, error) { _, seg := xray.BeginSubsegment(ctx, "database.ViewingHistory#Create") defer seg.Close(nil) // TODO - return &entity.ViewingHistory{ID: "id"}, nil + return &entity.ViewingHistory{}, nil +} + +func (e *ViewingHistory) Get(ctx context.Context, id string, userID string) (*entity.ViewingHistory, error) { + _, seg := xray.BeginSubsegment(ctx, "database.ViewingHistory#Get") + defer seg.Close(nil) + + query := `SELECT userID, episodeID, seriesID, seasonID, isWatched, position, duration, lastViewingAt FROM viewingHistories WHERE userID = ? AND episodeID = ?` + row := e.db.QueryRowContext(ctx, query, userID, id) + if err := row.Err(); err != nil { + return nil, errcode.New(err) + } + + viewingHistory := &entity.ViewingHistory{} + err := row.Scan( + &viewingHistory.UserID, + &viewingHistory.EpisodeID, + &viewingHistory.SeriesID, + &viewingHistory.SeasonID, + &viewingHistory.IsWatched, + &viewingHistory.Position, + &viewingHistory.Duration, + &viewingHistory.LastViewingAt, + ) + if err != nil { + return nil, errcode.New(err) + } + + return viewingHistory, nil } func (e *ViewingHistory) BatchGet(ctx context.Context, ids []string, userID string) (entity.ViewingHistories, error) { _, seg := xray.BeginSubsegment(ctx, "database.ViewingHistory#BatchGet") defer seg.Close(nil) - // TODO - return entity.ViewingHistories{{ID: "id"}}, nil + return nil, errcode.New(errcode.NewCode(errcode.CodeUnimplemented)) } diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go index cce07c6..9f8518c 100644 --- a/pkg/repository/repository.go +++ b/pkg/repository/repository.go @@ -55,6 +55,7 @@ type ListSeasonsParams struct { type ViewingHistory interface { Create(ctx context.Context, viewingHistory *entity.ViewingHistory) (*entity.ViewingHistory, error) + Get(ctx context.Context, id string, userID string) (*entity.ViewingHistory, error) BatchGet(ctx context.Context, ids []string, userID string) (entity.ViewingHistories, error) } diff --git a/pkg/usecase/viewing_history.go b/pkg/usecase/viewing_history.go index f7b4c0a..b906cee 100644 --- a/pkg/usecase/viewing_history.go +++ b/pkg/usecase/viewing_history.go @@ -55,9 +55,15 @@ func (u *UsecaseImpl) BatchGetViewingHistories(ctx context.Context, req *BatchGe return nil, errcode.New(err) } - viewingHistories, err := u.db.ViewingHistory.BatchGet(ctx, req.EpisodeIDs, req.UserID) - if err != nil { - return nil, errcode.New(err) + viewingHistories := make(entity.ViewingHistories, 0, len(req.EpisodeIDs)) + + for _, episodeID := range req.EpisodeIDs { + viewingHistory, err := u.db.ViewingHistory.Get(ctx, episodeID, req.UserID) + if err != nil { + return nil, errcode.New(err) + } + viewingHistories = append(viewingHistories, viewingHistory) } + return &BatchGetViewingHistoriesResponse{ViewingHistories: viewingHistories}, nil }