package postgres import ( "fmt" "git.snrd.de/Spaenny/steamServer" "github.com/google/uuid" "github.com/jmoiron/sqlx" ) type ServerStore struct { *sqlx.DB } func (s *ServerStore) Server(id uuid.UUID) (steamServer.Server, error) { var ser steamServer.Server if err := s.Get(&ser, `SELECT * FROM servers WHERE id = $1`, id); err != nil { return steamServer.Server{}, fmt.Errorf("error getting server: %w", err) } return ser, nil } func (s *ServerStore) Servers() ([]steamServer.Server, error) { var ss []steamServer.Server var query = `SELECT * FROM servers ORDER BY name ASC` if err := s.Select(&ss, query); err != nil { return []steamServer.Server{}, fmt.Errorf("error gettings servers: %w", err) } return ss, nil } func (s *ServerStore) ServersByRegion(regionID uuid.UUID) ([]steamServer.Server, error) { var ss []steamServer.Server var query = ` SELECT * FROM servers WHERE region_id = $1 ORDER BY name ASC` if err := s.Select(&ss, query, regionID); err != nil { return []steamServer.Server{}, fmt.Errorf("error getting region servers: %w", err) } return ss, nil } func (s *ServerStore) ServersByRegionAndGamemode(regionID uuid.UUID, gamemodeID uuid.UUID) ([]steamServer.Server, error) { var ss []steamServer.Server var query = `SELECT * FROM servers WHERE gamemode_id = $1 AND region_id = $2 ORDER BY name ASC` if err := s.Select(&ss, query, regionID, gamemodeID); err != nil { return []steamServer.Server{}, fmt.Errorf("error getting region and gamemode servers: %w", err) } return ss, nil } func (s *ServerStore) ServersByGamemode(gamemodeID uuid.UUID) ([]steamServer.Server, error) { var ss []steamServer.Server var query = `SELECT * FROM servers WHERE gamemode_id = $1 ORDER BY name ASC` if err := s.Select(&ss, query, gamemodeID); err != nil { return []steamServer.Server{}, fmt.Errorf("error getting gamemode servers: %w", err) } return ss, nil } func (s *ServerStore) CreateServer(ser *steamServer.Server) error { if err := s.Get(ser, `INSERT INTO servers VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNUNG *`, ser.ID, ser.RegionID, ser.GamemodeID, ser.AppID, ser.Name, ser.ServerIP, ser.Map, ser.Players, ser.MaxPlayers); err != nil { return fmt.Errorf("error creating server: %w", err) } return nil } func (s *ServerStore) UpdateServerByQuery(ser *steamServer.Server, server_ip string) error { if err := s.Get(s, `UPDATE "servers" SET "name" = $1, "map" = $2, "players" = $3, "max_players" = $4 WHERE "server_ip" = $5 RETURNING *`, ser.Name, ser.Map, ser.Players, ser.MaxPlayers, server_ip); err != nil { return fmt.Errorf("error updating server: %w", err) } return nil }