feat: add sql request for getting game
This commit is contained in:
@@ -1,13 +1,37 @@
|
|||||||
package postgresql
|
package postgresql
|
||||||
|
|
||||||
import "hamster-tycoon/storage/model"
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"github.com/lib/pq"
|
||||||
|
"hamster-tycoon/storage/dao"
|
||||||
|
"hamster-tycoon/storage/model"
|
||||||
|
)
|
||||||
|
|
||||||
func (db *DatabasePostgreSQL) GetAllGames() ([]*model.Game, error) {
|
func (db *DatabasePostgreSQL) GetAllGames() ([]*model.Game, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DatabasePostgreSQL) GetGameById(string) (*model.Game, error) {
|
func (db *DatabasePostgreSQL) GetGameById(string) (*model.Game, error) {
|
||||||
return nil, nil
|
q := `
|
||||||
|
SELECT g.id, g.server_id, g.user_id, g.created_at, g.updated_at
|
||||||
|
FROM public.game g
|
||||||
|
WHERE g.id = $1
|
||||||
|
`
|
||||||
|
row := db.session.QueryRow(q, id)
|
||||||
|
|
||||||
|
server := model.Server{}
|
||||||
|
user := model.User{}
|
||||||
|
game := model.Game{
|
||||||
|
Server: server,
|
||||||
|
}
|
||||||
|
err := row.Scan(&game.ID, &server.ID, &user.ID, &game.CreatedAt, &game.UpdatedAt)
|
||||||
|
if errPq, ok := err.(*pq.Error); ok {
|
||||||
|
return nil, handlePgError(errPq)
|
||||||
|
}
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, dao.NewDAOError(dao.ErrTypeNotFound, err)
|
||||||
|
}
|
||||||
|
return &game, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DatabasePostgreSQL) CreateGame(*model.Game) error {
|
func (db *DatabasePostgreSQL) CreateGame(*model.Game) error {
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type Cage struct {
|
type Cage struct {
|
||||||
Hamsters []*Hamster `json:"hamsters"`
|
ID string `json:"id"`
|
||||||
|
Game Game `json:"game"`
|
||||||
|
Hamsters []*Hamster `json:"hamsters"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
UpdatedAt *time.Time `json:"updatedAt"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
ID string `json:"game_id"`
|
ID string `json:"game_id"`
|
||||||
User User `json:"-"`
|
User User `json:"-"`
|
||||||
Server Server `json:"server"`
|
Server Server `json:"server"`
|
||||||
Cages []*Cage `json:"cages"`
|
Cages []*Cage `json:"cages"`
|
||||||
SoldHamster []*Hamster `json:"sold_hamsters"`
|
SoldHamster []*Hamster `json:"sold_hamsters"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
UpdatedAt *time.Time `json:"updatedAt"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
uuid "github.com/satori/go.uuid"
|
uuid "github.com/satori/go.uuid"
|
||||||
"hamster-tycoon/randomizer"
|
"hamster-tycoon/randomizer"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -48,6 +49,8 @@ type Hamster struct {
|
|||||||
GestationCooldown int8 `json:"gestation_cooldown"`
|
GestationCooldown int8 `json:"gestation_cooldown"`
|
||||||
GestationFather *Hamster `json:"gestation_father"`
|
GestationFather *Hamster `json:"gestation_father"`
|
||||||
Child []*Hamster `json:"childs"`
|
Child []*Hamster `json:"childs"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
UpdatedAt *time.Time `json:"updatedAt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hamster) Die() {
|
func (h *Hamster) Die() {
|
||||||
|
|||||||
Reference in New Issue
Block a user