This repository has been archived on 2024-05-18. You can view files and clone it, but cannot push or open issues or pull requests.
steamServer/postgres/server_store.go

89 lines
2.7 KiB
Go
Raw Normal View History

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
}
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`
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
}
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`
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)
}
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 {
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,
ser.RegionID,
ser.GamemodeID,
2021-09-20 14:57:24 +02:00
ser.AppID,
ser.Name,
ser.ServerIP,
2021-09-20 14:57:24 +02:00
ser.Map,
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 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 *`,
ser.Name,
ser.Map,
ser.Players,
2021-09-20 22:55:12 +02:00
ser.MaxPlayers,
server_ip); err != nil {
return fmt.Errorf("error updating server: %w", err)
}
return nil
}