This commit is contained in:
2020-09-16 23:49:17 +02:00
parent 66713b5722
commit 57e6f051f4
6 changed files with 9 additions and 42 deletions

View File

@@ -5,7 +5,6 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<include file="prepare-database.sql" relativeToChangelogFile="true"/> <include file="prepare-database.sql" relativeToChangelogFile="true"/>
<include file="changeset/create-user.xml" relativeToChangelogFile="true"/> <include file="changeset/create-user.xml" relativeToChangelogFile="true"/>
<include file="changeset/create-server.xml" relativeToChangelogFile="true"/>
<include file="changeset/create-game.xml" relativeToChangelogFile="true"/> <include file="changeset/create-game.xml" relativeToChangelogFile="true"/>
<include file="changeset/create-cage.xml" relativeToChangelogFile="true"/> <include file="changeset/create-cage.xml" relativeToChangelogFile="true"/>
<include file="changeset/create-hamster.xml" relativeToChangelogFile="true"/> <include file="changeset/create-hamster.xml" relativeToChangelogFile="true"/>

View File

@@ -8,9 +8,6 @@
<column name="id" type="uuid" defaultValueComputed="gen_random_uuid()"> <column name="id" type="uuid" defaultValueComputed="gen_random_uuid()">
<constraints nullable="false" unique="true"/> <constraints nullable="false" unique="true"/>
</column> </column>
<column name="server_id" type="uuid">
<constraints nullable="false"/>
</column>
<column name="user_id" type="uuid"> <column name="user_id" type="uuid">
<constraints nullable="false"/> <constraints nullable="false"/>
</column> </column>

View File

@@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet id="add-server-table" author="kratisto">
<createTable tableName="server">
<column name="id" type="uuid" defaultValueComputed="gen_random_uuid()">
<constraints nullable="false" unique="true"/>
</column>
<column name="name" type="text">
<constraints nullable="true"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>

View File

@@ -9,7 +9,7 @@ import (
func (db *DatabasePostgreSQL) GetAllGames() ([]*model.Game, error) { func (db *DatabasePostgreSQL) GetAllGames() ([]*model.Game, error) {
q := ` q := `
SELECT g.id, g.server_id, g.name, g.user_id, g.created_at, g.updated_at SELECT g.id, g.name, g.user_id, g.created_at, g.updated_at
FROM public.game g FROM public.game g
` `
rows, err := db.session.Query(q) rows, err := db.session.Query(q)
@@ -20,12 +20,9 @@ func (db *DatabasePostgreSQL) GetAllGames() ([]*model.Game, error) {
games := make([]*model.Game, 0) games := make([]*model.Game, 0)
for rows.Next() { for rows.Next() {
server := model.Server{}
user := model.User{} user := model.User{}
game := model.Game{ game := model.Game{}
Server: server, err := rows.Scan(&game.ID, &game.Name, &user.ID, &game.CreatedAt, &game.UpdatedAt)
}
err := rows.Scan(&game.ID, &server.ID, &game.Name, &user.ID, &game.CreatedAt, &game.UpdatedAt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -36,18 +33,14 @@ func (db *DatabasePostgreSQL) GetAllGames() ([]*model.Game, error) {
func (db *DatabasePostgreSQL) GetGameByID(id string) (*model.Game, error) { func (db *DatabasePostgreSQL) GetGameByID(id string) (*model.Game, error) {
q := ` q := `
SELECT g.id, g.server_id, g.name, g.user_id, g.created_at, g.updated_at SELECT g.id, g.name, g.user_id, g.created_at, g.updated_at
FROM public.game g FROM public.game g
WHERE g.id = $1 WHERE g.id = $1
` `
row := db.session.QueryRow(q, id) row := db.session.QueryRow(q, id)
server := model.Server{}
user := model.User{} user := model.User{}
game := model.Game{ game := model.Game{}
Server: server, err := row.Scan(&game.ID, &game.Name, &user.ID, &game.CreatedAt, &game.UpdatedAt)
}
err := row.Scan(&game.ID, &server.ID, &game.Name, &user.ID, &game.CreatedAt, &game.UpdatedAt)
if errPq, ok := err.(*pq.Error); ok { if errPq, ok := err.(*pq.Error); ok {
return nil, handlePgError(errPq) return nil, handlePgError(errPq)
} }
@@ -60,14 +53,14 @@ func (db *DatabasePostgreSQL) GetGameByID(id string) (*model.Game, error) {
func (db *DatabasePostgreSQL) CreateGame(game *model.Game) error { func (db *DatabasePostgreSQL) CreateGame(game *model.Game) error {
q := ` q := `
INSERT INTO public.game INSERT INTO public.game
(server_id, user_id, name) (user_id, name)
VALUES VALUES
($1, $2, $3) ($1, $2)
RETURNING id, created_at RETURNING id, created_at
` `
err := db.session. err := db.session.
QueryRow(q, game.Server.ID, game.User.ID, game.Name). QueryRow(q, game.User.ID, game.Name).
Scan(&game.ID, &game.CreatedAt) Scan(&game.ID, &game.CreatedAt)
if errPq, ok := err.(*pq.Error); ok { if errPq, ok := err.(*pq.Error); ok {
return handlePgError(errPq) return handlePgError(errPq)

View File

@@ -5,7 +5,6 @@ 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"`
Name string `json:"name"` Name string `json:"name"`
Cages []*Cage `json:"cages"` Cages []*Cage `json:"cages"`
SoldHamster []*Hamster `json:"sold_hamsters"` SoldHamster []*Hamster `json:"sold_hamsters"`

View File

@@ -1,5 +0,0 @@
package model
type Server struct {
ID string
}