Skip to content

Commit

Permalink
listViewingHistoriesの実装
Browse files Browse the repository at this point in the history
  • Loading branch information
ebakazu committed Feb 1, 2024
1 parent 1838590 commit 7f5f030
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions pkg/app/http/viewing_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + `"}`,
Expand All @@ -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,
},
}
Expand Down Expand Up @@ -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,
},
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/entity/viewing_history.go
Original file line number Diff line number Diff line change
@@ -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
40 changes: 35 additions & 5 deletions pkg/repository/database/viewing_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/usecase/viewing_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7f5f030

Please sign in to comment.