Skip to content

Commit 3acfa53

Browse files
authored
Merge pull request #1382 from traPtitech/imp-create-game-play-log
CreateGamePlayLogのrepository実装をする
2 parents fbbf862 + c347b9c commit 3acfa53

File tree

2 files changed

+468
-14
lines changed

2 files changed

+468
-14
lines changed

src/repository/gorm2/v2_game_play_log.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package gorm2
22

33
import (
44
"context"
5+
"database/sql"
56
"errors"
7+
"fmt"
68
"time"
79

10+
"github.com/go-sql-driver/mysql"
811
"github.com/google/uuid"
912
"github.com/traPtitech/trap-collection-server/src/domain"
1013
"github.com/traPtitech/trap-collection-server/src/domain/values"
@@ -23,9 +26,40 @@ func NewGamePlayLogV2(db *DB) *GamePlayLogV2 {
2326
}
2427
}
2528

26-
func (g *GamePlayLogV2) CreateGamePlayLog(_ context.Context, _ *domain.GamePlayLog) error {
27-
// TODO: interfaceのコメントを参考に実装を行う
28-
panic("not implemented")
29+
func (g *GamePlayLogV2) CreateGamePlayLog(ctx context.Context, playLog *domain.GamePlayLog) error {
30+
31+
db, err := g.db.getDB(ctx)
32+
if err != nil {
33+
return fmt.Errorf("get db: %w", err)
34+
}
35+
36+
var endTime sql.NullTime
37+
if playLog.GetEndTime() != nil {
38+
endTime = sql.NullTime{
39+
Time: *playLog.GetEndTime(),
40+
Valid: true,
41+
}
42+
}
43+
44+
gamePlayLogTable := schema.GamePlayLogTable{
45+
ID: uuid.UUID(playLog.GetID()),
46+
EditionID: uuid.UUID(playLog.GetEditionID()),
47+
GameID: uuid.UUID(playLog.GetGameID()),
48+
GameVersionID: uuid.UUID(playLog.GetGameVersionID()),
49+
StartTime: playLog.GetStartTime(),
50+
EndTime: endTime,
51+
CreatedAt: playLog.GetCreatedAt(),
52+
UpdatedAt: playLog.GetUpdatedAt(),
53+
}
54+
55+
err = db.Create(&gamePlayLogTable).Error
56+
if err != nil {
57+
if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == 1062 {
58+
return repository.ErrDuplicatedUniqueKey
59+
}
60+
return fmt.Errorf("create game play log: %w", err)
61+
}
62+
return nil
2963
}
3064

3165
func (g *GamePlayLogV2) GetGamePlayLog(ctx context.Context, playLogID values.GamePlayLogID) (*domain.GamePlayLog, error) {
@@ -67,12 +101,12 @@ func (g *GamePlayLogV2) UpdateGamePlayLogEndTime(_ context.Context, _ values.Gam
67101
panic("not implemented")
68102
}
69103

70-
func (g *GamePlayLogV2) GetGamePlayStats(_ context.Context, _ values.GameID, _ *values.GameVersionID, _ time.Time, _ time.Time) (*domain.GamePlayStats, error) {
104+
func (g *GamePlayLogV2) GetGamePlayStats(_ context.Context, _ values.GameID, _ *values.GameVersionID, _, _ time.Time) (*domain.GamePlayStats, error) {
71105
// TODO: interfaceのコメントを参考に実装を行う
72106
panic("not implemented")
73107
}
74108

75-
func (g *GamePlayLogV2) GetEditionPlayStats(_ context.Context, _ values.LauncherVersionID, _ time.Time, _ time.Time) (*domain.EditionPlayStats, error) {
109+
func (g *GamePlayLogV2) GetEditionPlayStats(_ context.Context, _ values.LauncherVersionID, _, _ time.Time) (*domain.EditionPlayStats, error) {
76110
// TODO: interfaceのコメントを参考に実装を行う
77111
panic("not implemented")
78112
}

0 commit comments

Comments
 (0)