clean up
This commit is contained in:
@@ -2,18 +2,18 @@ package postgresql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/lib/pq"
|
||||
"hamster-tycoon/storage/dao"
|
||||
"hamster-tycoon/storage/model"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
func (db *DatabasePostgreSQL) GetAllCages(gameID string) ([]*model.Cage, error) {
|
||||
func (db *DatabasePostgreSQL) GetAllCages() ([]*model.Cage, error) {
|
||||
q := `
|
||||
SELECT c.id, created_at, updated_at, game_id
|
||||
SELECT c.id, created_at, updated_at
|
||||
FROM public.Cage c
|
||||
WHERE c.game_id=$1
|
||||
`
|
||||
rows, err := db.session.Query(q, gameID)
|
||||
rows, err := db.session.Query(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -21,11 +21,8 @@ func (db *DatabasePostgreSQL) GetAllCages(gameID string) ([]*model.Cage, error)
|
||||
|
||||
cages := make([]*model.Cage, 0)
|
||||
for rows.Next() {
|
||||
game := model.Game{}
|
||||
cage := model.Cage{
|
||||
Game: game,
|
||||
}
|
||||
err := rows.Scan(&cage.ID, &cage.CreatedAt, &cage.UpdatedAt, &game.ID)
|
||||
cage := model.Cage{}
|
||||
err := rows.Scan(&cage.ID, &cage.CreatedAt, &cage.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -34,19 +31,16 @@ func (db *DatabasePostgreSQL) GetAllCages(gameID string) ([]*model.Cage, error)
|
||||
return cages, nil
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) GetCageByID(gameID, cageID string) (*model.Cage, error) {
|
||||
func (db *DatabasePostgreSQL) GetCageByID(cageID string) (*model.Cage, error) {
|
||||
q := `
|
||||
SELECT c.id, created_at, updated_at, game_id
|
||||
SELECT c.id, created_at, updated_at
|
||||
FROM public.Cage c
|
||||
WHERE c.game_id=$1 and c.id = $2
|
||||
WHERE c.id = $2
|
||||
`
|
||||
row := db.session.QueryRow(q, gameID, cageID)
|
||||
row := db.session.QueryRow(q, cageID)
|
||||
|
||||
game := model.Game{}
|
||||
cage := model.Cage{
|
||||
Game: game,
|
||||
}
|
||||
err := row.Scan(&cage.ID, &cage.CreatedAt, &cage.UpdatedAt, &game.ID)
|
||||
cage := model.Cage{}
|
||||
err := row.Scan(&cage.ID, &cage.CreatedAt, &cage.UpdatedAt)
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return nil, handlePgError(errPq)
|
||||
}
|
||||
@@ -57,21 +51,7 @@ func (db *DatabasePostgreSQL) GetCageByID(gameID, cageID string) (*model.Cage, e
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) CreateCage(cage *model.Cage) error {
|
||||
q := `
|
||||
INSERT INTO public.Cage
|
||||
(game_id)
|
||||
VALUES
|
||||
($1)
|
||||
RETURNING id, created_at
|
||||
`
|
||||
|
||||
err := db.session.
|
||||
QueryRow(q, cage.Game.ID).
|
||||
Scan(&cage.ID, &cage.CreatedAt)
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return handlePgError(errPq)
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) DeleteCage(id string) error {
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/lib/pq"
|
||||
"hamster-tycoon/storage/dao"
|
||||
"hamster-tycoon/storage/model"
|
||||
)
|
||||
|
||||
func (db *DatabasePostgreSQL) GetAllGames() ([]*model.Game, error) {
|
||||
q := `
|
||||
SELECT g.id, g.name, g.user_id, g.created_at, g.updated_at
|
||||
FROM public.game g
|
||||
`
|
||||
rows, err := db.session.Query(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
games := make([]*model.Game, 0)
|
||||
for rows.Next() {
|
||||
user := model.User{}
|
||||
game := model.Game{}
|
||||
err := rows.Scan(&game.ID, &game.Name, &user.ID, &game.CreatedAt, &game.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
games = append(games, &game)
|
||||
}
|
||||
return games, nil
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) GetGameByID(id string) (*model.Game, error) {
|
||||
q := `
|
||||
SELECT g.id, g.name, g.user_id, g.created_at, g.updated_at
|
||||
FROM public.game g
|
||||
WHERE g.id = $1
|
||||
`
|
||||
row := db.session.QueryRow(q, id)
|
||||
user := model.User{}
|
||||
game := model.Game{}
|
||||
err := row.Scan(&game.ID, &game.Name, &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(game *model.Game) error {
|
||||
q := `
|
||||
INSERT INTO public.game
|
||||
(user_id, name)
|
||||
VALUES
|
||||
($1, $2)
|
||||
RETURNING id, created_at
|
||||
`
|
||||
|
||||
err := db.session.
|
||||
QueryRow(q, game.User.ID, game.Name).
|
||||
Scan(&game.ID, &game.CreatedAt)
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return handlePgError(errPq)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) DeleteGame(id string) error {
|
||||
q := `
|
||||
DELETE FROM public.game
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
_, err := db.session.Exec(q, id)
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return handlePgError(errPq)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) GetHamstersOfGame(gameID string) ([]*model.Hamster, error) {
|
||||
q := `
|
||||
SELECT id, created_at, updated_at, name, number, sexe, age, father_id, mother_id, hunger_level, thirst_level, weight, height, alive, sold, gestation, gestation_period, gestation_cooldown, gestation_father_id
|
||||
FROM public.Hamster h
|
||||
JOIN public.cage c
|
||||
ON c.id = h.cage_id
|
||||
JOIN public.game g
|
||||
on c.game_id = g.id
|
||||
WHERE g.id = $1
|
||||
`
|
||||
rows, err := db.session.Query(q, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
hamsters := make([]*model.Hamster, 0)
|
||||
for rows.Next() {
|
||||
hamster := model.Hamster{
|
||||
Mother: &model.Hamster{},
|
||||
Father: &model.Hamster{},
|
||||
GestationFather: &model.Hamster{},
|
||||
}
|
||||
err := rows.Scan(&hamster.ID, &hamster.CreatedAt,
|
||||
&hamster.UpdatedAt, &hamster.Name,
|
||||
&hamster.Number, &hamster.Sexe,
|
||||
&hamster.Age, &hamster.Father.ID,
|
||||
&hamster.Mother.ID, &hamster.HungerLevel,
|
||||
&hamster.ThirstLevel, &hamster.Weight,
|
||||
&hamster.Height, &hamster.Alive,
|
||||
&hamster.Sold, &hamster.Gestation,
|
||||
&hamster.GestationPeriod, &hamster.GestationCooldown,
|
||||
&hamster.GestationFather.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hamsters = append(hamsters, &hamster)
|
||||
}
|
||||
return hamsters, nil
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) UpdateGame(*model.Game) error {
|
||||
return nil
|
||||
}
|
||||
@@ -2,20 +2,20 @@ package postgresql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/lib/pq"
|
||||
"hamster-tycoon/storage/dao"
|
||||
"hamster-tycoon/storage/model"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
func (db *DatabasePostgreSQL) GetAllHamsters(gameID, cageID string) ([]*model.Hamster, error) {
|
||||
func (db *DatabasePostgreSQL) GetAllHamsters(cageID string) ([]*model.Hamster, error) {
|
||||
q := `
|
||||
SELECT id, created_at, updated_at, name, number, sexe, age, father_id, mother_id, hunger_level, thirst_level, weight, height, alive, sold, gestation, gestation_period, gestation_cooldown, gestation_father_id, cage_id
|
||||
SELECT id, created_at, updated_at, name, number, sexe, age, father_id, mother_id, hunger_level, thirst_level, weight, height, alive, sold, golden, gestation, gestation_period, gestation_cooldown, gestation_father_id, cage_id
|
||||
FROM hamster h
|
||||
JOIN cage c ON h.cage_id = c.id
|
||||
JOIN game g on c.game_id = g.id
|
||||
WHERE c.id=$1 and g.id=$2
|
||||
`
|
||||
rows, err := db.session.Query(q, cageID, gameID)
|
||||
rows, err := db.session.Query(q, cageID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func (db *DatabasePostgreSQL) GetAllHamsters(gameID, cageID string) ([]*model.Ha
|
||||
&hamster.Mother.ID, &hamster.HungerLevel,
|
||||
&hamster.ThirstLevel, &hamster.Weight,
|
||||
&hamster.Height, &hamster.Alive,
|
||||
&hamster.Sold, &hamster.Gestation,
|
||||
&hamster.Sold, &hamster.Golden, &hamster.Gestation,
|
||||
&hamster.GestationPeriod, &hamster.GestationCooldown,
|
||||
&hamster.GestationFather.ID, &hamster.Cage.ID)
|
||||
if err != nil {
|
||||
@@ -47,15 +47,14 @@ func (db *DatabasePostgreSQL) GetAllHamsters(gameID, cageID string) ([]*model.Ha
|
||||
return hamsters, nil
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) GetHamsterByID(id, gameID, cageID string) (*model.Hamster, error) {
|
||||
func (db *DatabasePostgreSQL) GetHamsterByID(id, cageID string) (*model.Hamster, error) {
|
||||
q := `
|
||||
SELECT id, created_at, updated_at, name, number, sexe, age, father_id, mother_id, hunger_level, thirst_level, weight, height, alive, sold, gestation, gestation_period, gestation_cooldown, gestation_father_id, cage_id
|
||||
SELECT id, created_at, updated_at, name, number, sexe, age, father_id, mother_id, hunger_level, thirst_level, weight, height, alive, sold, golden, gestation, gestation_period, gestation_cooldown, gestation_father_id, cage_id
|
||||
FROM hamster h
|
||||
JOIN cage c ON h.cage_id = c.id
|
||||
JOIN game g on c.game_id = g.id
|
||||
WHERE h.id=$1 and c.id=$2 and g.id=$3
|
||||
`
|
||||
row := db.session.QueryRow(q, id, cageID, gameID)
|
||||
row := db.session.QueryRow(q, id, cageID)
|
||||
|
||||
hamster := model.Hamster{
|
||||
Mother: &model.Hamster{},
|
||||
@@ -70,7 +69,7 @@ func (db *DatabasePostgreSQL) GetHamsterByID(id, gameID, cageID string) (*model.
|
||||
&hamster.Mother.ID, &hamster.HungerLevel,
|
||||
&hamster.ThirstLevel, &hamster.Weight,
|
||||
&hamster.Height, &hamster.Alive,
|
||||
&hamster.Sold, &hamster.Gestation,
|
||||
&hamster.Sold, &hamster.Golden, &hamster.Gestation,
|
||||
&hamster.GestationPeriod, &hamster.GestationCooldown,
|
||||
&hamster.GestationFather.ID, &hamster.Cage.ID)
|
||||
|
||||
@@ -86,7 +85,7 @@ func (db *DatabasePostgreSQL) GetHamsterByID(id, gameID, cageID string) (*model.
|
||||
func (db *DatabasePostgreSQL) CreateHamster(hamster *model.Hamster) error {
|
||||
q := `
|
||||
INSERT INTO public.Hamster
|
||||
(name, number, sexe, age, father_id, mother_id, hunger_level, thirst_level, weight, height, alive, sold, gestation, gestation_period, gestation_cooldown, gestation_father_id,cage_id)
|
||||
(name, number, sexe, age, father_id, mother_id, hunger_level, thirst_level, weight, height, alive, sold, golden, gestation, gestation_period, gestation_cooldown, gestation_father_id,cage_id)
|
||||
VALUES
|
||||
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
|
||||
RETURNING id, created_at
|
||||
@@ -99,7 +98,7 @@ func (db *DatabasePostgreSQL) CreateHamster(hamster *model.Hamster) error {
|
||||
&hamster.Mother.ID, &hamster.HungerLevel,
|
||||
&hamster.ThirstLevel, &hamster.Weight,
|
||||
&hamster.Height, &hamster.Alive,
|
||||
&hamster.Sold, &hamster.Gestation,
|
||||
&hamster.Sold, &hamster.Golden, &hamster.Gestation,
|
||||
&hamster.GestationPeriod, &hamster.GestationCooldown,
|
||||
&hamster.GestationFather.ID, &hamster.Cage.ID).
|
||||
Scan(&hamster.ID, &hamster.CreatedAt)
|
||||
|
||||
Reference in New Issue
Block a user