2021-09-20 14:57:24 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:31:41 +02:00
|
|
|
func (s *ServerStore) Servers() ([]steamServer.Server, error) {
|
|
|
|
var ss []steamServer.Server
|
2021-09-23 15:58:56 +02:00
|
|
|
var query = `SELECT * FROM servers ORDER BY name ASC`
|
2021-09-20 17:31:41 +02:00
|
|
|
if err := s.Select(&ss, query); err != nil {
|
|
|
|
return []steamServer.Server{}, fmt.Errorf("error gettings servers: %w", err)
|
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:55:12 +02:00
|
|
|
func (s *ServerStore) ServersByRegion(regionID uuid.UUID) ([]steamServer.Server, error) {
|
|
|
|
var ss []steamServer.Server
|
|
|
|
var query = `
|
2021-09-23 15:58:56 +02:00
|
|
|
SELECT * FROM servers WHERE region_id = $1 ORDER BY name ASC`
|
2021-09-20 22:55:12 +02:00
|
|
|
if err := s.Select(&ss, query, regionID); err != nil {
|
2021-09-23 15:58:56 +02:00
|
|
|
return []steamServer.Server{}, fmt.Errorf("error getting region servers: %w", err)
|
2021-09-20 22:55:12 +02:00
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 14:12:49 +02:00
|
|
|
func (s *ServerStore) ServersByRegionAndGamemode(regionID uuid.UUID, gamemodeID uuid.UUID) ([]steamServer.Server, error) {
|
|
|
|
var ss []steamServer.Server
|
2021-09-23 15:58:56 +02:00
|
|
|
var query = `SELECT * FROM servers WHERE gamemode_id = $1 AND region_id = $2 ORDER BY name ASC`
|
2021-09-22 14:12:49 +02:00
|
|
|
if err := s.Select(&ss, query, regionID, gamemodeID); err != nil {
|
2021-09-23 15:58:56 +02:00
|
|
|
return []steamServer.Server{}, fmt.Errorf("error getting region and gamemode servers: %w", err)
|
2021-09-22 14:12:49 +02:00
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:55:12 +02:00
|
|
|
func (s *ServerStore) ServersByGamemode(gamemodeID uuid.UUID) ([]steamServer.Server, error) {
|
|
|
|
var ss []steamServer.Server
|
|
|
|
|
2021-09-23 15:58:56 +02:00
|
|
|
var query = `SELECT * FROM servers WHERE gamemode_id = $1 ORDER BY name ASC`
|
2021-09-20 22:55:12 +02:00
|
|
|
|
|
|
|
if err := s.Select(&ss, query, gamemodeID); err != nil {
|
2021-09-23 15:58:56 +02:00
|
|
|
return []steamServer.Server{}, fmt.Errorf("error getting gamemode servers: %w", err)
|
2021-09-20 22:55:12 +02:00
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2021-09-20 14:57:24 +02:00
|
|
|
func (s *ServerStore) CreateServer(ser *steamServer.Server) error {
|
2021-09-20 18:44:08 +02:00
|
|
|
if err := s.Get(ser, `INSERT INTO servers VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNUNG *`,
|
2021-09-20 14:57:24 +02:00
|
|
|
ser.ID,
|
2021-09-20 18:44:08 +02:00
|
|
|
ser.RegionID,
|
|
|
|
ser.GamemodeID,
|
2021-09-20 14:57:24 +02:00
|
|
|
ser.AppID,
|
|
|
|
ser.Name,
|
2021-09-20 18:44:08 +02:00
|
|
|
ser.ServerIP,
|
2021-09-20 14:57:24 +02:00
|
|
|
ser.Map,
|
2021-09-20 17:31:41 +02:00
|
|
|
ser.Players,
|
2021-09-20 14:57:24 +02:00
|
|
|
ser.MaxPlayers); err != nil {
|
|
|
|
return fmt.Errorf("error creating server: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-20 18:44:08 +02:00
|
|
|
|
2021-09-20 22:55:12 +02:00
|
|
|
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 *`,
|
2021-09-20 18:44:08 +02:00
|
|
|
ser.Name,
|
|
|
|
ser.Map,
|
|
|
|
ser.Players,
|
2021-09-20 22:55:12 +02:00
|
|
|
ser.MaxPlayers,
|
|
|
|
server_ip); err != nil {
|
2021-09-20 18:44:08 +02:00
|
|
|
return fmt.Errorf("error updating server: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|