-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_games.go
80 lines (75 loc) · 2.04 KB
/
db_games.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"database/sql"
"fmt"
"strings"
)
import _ "github.com/go-sql-driver/mysql"
type DbGame struct {
Id uint `json:"id"`
IgdbId uint `json:"igdb_id"`
Name string `json:"name"`
Summary sql.NullString `json:"summary"`
Rating sql.NullInt64 `json:"rating"`
ReleaseDate sql.NullInt64 `json:"release_date"`
SeriesId sql.NullInt64 `json:"series_id"`
DeveloperId sql.NullInt64 `json:"developer_id"`
PublisherId sql.NullInt64 `json:"publisher_id"`
Cover sql.NullString `json:"cover"`
Screenshots sql.NullString `json:"screenshots"`
}
func (db DbClient) GetGame(igdbId int) DbGame {
var game DbGame
err := db.instance.QueryRow(
"SELECT id, name, summary, rating, release_date, cover, screenshots, series_id, developer_id, publisher_id FROM games WHERE igdb_id = ?",
igdbId,
).Scan(
&game.Id,
&game.Name,
&game.Summary,
&game.Rating,
&game.ReleaseDate,
&game.Cover,
&game.Screenshots,
&game.SeriesId,
&game.DeveloperId,
&game.PublisherId,
)
if err != nil {
panic(err.Error())
}
return game
}
func (db DbClient) AddGame(game DbGame) int64 {
query := fmt.Sprintf("INSERT INTO games (igdb_id, name, summary, rating, release_date, series_id, developer_id, publisher_id, cover, screenshots, created_at) "+
"VALUES (%d, \"%s\", \"%s\", %d, %d, %s, %s, %s, \"%s\", \"%s\", NOW())",
game.IgdbId,
game.Name,
strings.Replace(game.Summary.String, "\"", "'", -1),
game.Rating.Int64,
game.ReleaseDate.Int64,
foreignKey(game.SeriesId),
foreignKey(game.DeveloperId),
foreignKey(game.PublisherId),
game.Cover.String,
game.Screenshots.String,
)
fmt.Println(query)
result, err := db.instance.Exec(query)
if err != nil {
panic(err.Error())
}
id, err := result.LastInsertId()
if err != nil {
panic(err.Error())
}
return id
}
func (db DbClient) GameExists(igdbId int) bool {
var game DbGame
err := db.instance.QueryRow("SELECT name FROM games WHERE igdb_id = ?", igdbId).Scan(&game.Name)
if err != nil {
return false
}
return true
}