add missing path property handling
This commit is contained in:
@@ -7,12 +7,15 @@ import (
|
||||
"hamster-tycoon/storage/model"
|
||||
)
|
||||
|
||||
func (db *DatabasePostgreSQL) GetAllHamsters() ([]*model.Hamster, error) {
|
||||
func (db *DatabasePostgreSQL) GetAllHamsters(gameID, 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
|
||||
FROM public.Hamster h
|
||||
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
|
||||
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)
|
||||
rows, err := db.session.Query(q, cageID, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -24,6 +27,7 @@ func (db *DatabasePostgreSQL) GetAllHamsters() ([]*model.Hamster, error) {
|
||||
Mother: &model.Hamster{},
|
||||
Father: &model.Hamster{},
|
||||
GestationFather: &model.Hamster{},
|
||||
Cage: &model.Cage{},
|
||||
}
|
||||
err := rows.Scan(&hamster.ID, &hamster.CreatedAt,
|
||||
&hamster.UpdatedAt, &hamster.Name,
|
||||
@@ -34,7 +38,7 @@ func (db *DatabasePostgreSQL) GetAllHamsters() ([]*model.Hamster, error) {
|
||||
&hamster.Height, &hamster.Alive,
|
||||
&hamster.Sold, &hamster.Gestation,
|
||||
&hamster.GestationPeriod, &hamster.GestationCooldown,
|
||||
&hamster.GestationFather.ID)
|
||||
&hamster.GestationFather.ID, &hamster.Cage.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -43,18 +47,21 @@ func (db *DatabasePostgreSQL) GetAllHamsters() ([]*model.Hamster, error) {
|
||||
return hamsters, nil
|
||||
}
|
||||
|
||||
func (db *DatabasePostgreSQL) GetHamsterById(id string) (*model.Hamster, error) {
|
||||
func (db *DatabasePostgreSQL) GetHamsterByID(id, gameID, 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
|
||||
FROM public.Hamster h
|
||||
WHERE c.id = $1
|
||||
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
|
||||
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)
|
||||
row := db.session.QueryRow(q, id, cageID, gameID)
|
||||
|
||||
hamster := model.Hamster{
|
||||
Mother: &model.Hamster{},
|
||||
Father: &model.Hamster{},
|
||||
GestationFather: &model.Hamster{},
|
||||
Cage: &model.Cage{},
|
||||
}
|
||||
err := row.Scan(&hamster.ID, &hamster.CreatedAt,
|
||||
&hamster.UpdatedAt, &hamster.Name,
|
||||
@@ -65,7 +72,7 @@ func (db *DatabasePostgreSQL) GetHamsterById(id string) (*model.Hamster, error)
|
||||
&hamster.Height, &hamster.Alive,
|
||||
&hamster.Sold, &hamster.Gestation,
|
||||
&hamster.GestationPeriod, &hamster.GestationCooldown,
|
||||
&hamster.GestationFather.ID)
|
||||
&hamster.GestationFather.ID, &hamster.Cage.ID)
|
||||
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return nil, handlePgError(errPq)
|
||||
@@ -79,9 +86,9 @@ func (db *DatabasePostgreSQL) GetHamsterById(id string) (*model.Hamster, error)
|
||||
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)
|
||||
(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)
|
||||
VALUES
|
||||
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
|
||||
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
|
||||
RETURNING id, created_at
|
||||
`
|
||||
|
||||
@@ -94,7 +101,7 @@ func (db *DatabasePostgreSQL) CreateHamster(hamster *model.Hamster) error {
|
||||
&hamster.Height, &hamster.Alive,
|
||||
&hamster.Sold, &hamster.Gestation,
|
||||
&hamster.GestationPeriod, &hamster.GestationCooldown,
|
||||
&hamster.GestationFather.ID).
|
||||
&hamster.GestationFather.ID, &hamster.Cage.ID).
|
||||
Scan(&hamster.ID, &hamster.CreatedAt)
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return handlePgError(errPq)
|
||||
|
||||
Reference in New Issue
Block a user