commit 59c69250d97a7f26276a3394a82ec3c90607299b Author: jolheiser Date: Tue Jan 4 23:45:23 2022 -0600 Initial commit Signed-off-by: jolheiser diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..433f7db --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2021 John Olheiser + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/database/application.go b/database/application.go new file mode 100644 index 0000000..2e557a5 --- /dev/null +++ b/database/application.go @@ -0,0 +1,55 @@ +package database + +import ( + "database/sql" + _ "embed" + "strings" +) + +type Application struct { + ID int64 `db:"id"` + Username string `db:"username"` + Age int64 `db:"age"` + PlayerType string `db:"player_type"` + EverBanned bool `db:"ever_banned"` + EverBannedExplanation string `db:"ever_banned_explanation"` + Reference string `db:"reference"` + ReadRules string `db:"read_rules"` + Accepted sql.NullBool `db:"accepted"` + Date string `db:"date"` +} + +func (a *Application) String() string { + return a.Username +} + +//go:embed queries/application_by_id.sql +var applicationByIDSQL string + +func (d *Database) ApplicationByID(id int64) (*Application, error) { + var app Application + return &app, d.db.Get(&app, applicationByIDSQL, id) +} + +//go:embed queries/application_by_player_username.sql +var applicationByPlayerUsernameSQL string + +func (d *Database) ApplicationByPlayerUsername(username string) (*Application, error) { + var app Application + return &app, d.db.Get(&app, applicationByPlayerUsernameSQL, strings.ToLower(username)) +} + +//go:embed queries/applications_by_username.sql +var applicationsByUsernameSQL string + +func (d *Database) ApplicationsByUsername(username string) (apps []*Application, err error) { + return apps, d.db.Select(&apps, applicationsByUsernameSQL, strings.ToLower(username)) +} + +//go:embed queries/application_update_accepted.sql +var applicationUpdateAcceptedSQL string + +func (d *Database) ApplicationUpdateAccepted(accepted *bool) (*Application, error) { + var app Application + return &app, d.db.Get(&app, applicationUpdateAcceptedSQL, accepted) +} diff --git a/database/database.go b/database/database.go new file mode 100644 index 0000000..cfa0416 --- /dev/null +++ b/database/database.go @@ -0,0 +1,23 @@ +package database + +import ( + _ "github.com/go-sql-driver/mysql" + "github.com/jmoiron/sqlx" +) + +type Database struct { + db *sqlx.DB +} + +func New(dns string) (*Database, error) { + db, err := sqlx.Open("mysql", dns) + if err != nil { + return nil, err + } + + if err := db.Ping(); err != nil { + return nil, err + } + + return &Database{db: db}, nil +} diff --git a/database/player.go b/database/player.go new file mode 100644 index 0000000..20744c6 --- /dev/null +++ b/database/player.go @@ -0,0 +1,60 @@ +package database + +import ( + "database/sql" + _ "embed" + "strings" +) + +type Player struct { + ID int64 `db:"id"` + AuthUserID sql.NullInt64 `db:"auth_user_id"` + UUID string `db:"uuid"` + Username string `db:"username"` + ApplicationID sql.NullInt64 `db:"application_id"` + Application *Application `db:"-"` + FirstSeen string `db:"first_seen"` + LastSeen string `db:"last_seen"` + DiscordID sql.NullString `db:"discord_id"` +} + +func (p *Player) String() string { + return p.Username +} + +//go:embed queries/players_by_username.sql +var playersByUsernameSQL string + +func (d *Database) PlayersByUsername(username string) (pl []*Player, err error) { + return pl, d.db.Select(&pl, playersByUsernameSQL, strings.ToLower(username)) +} + +//go:embed queries/players_by_username_application_notnull.sql +var playersByUsernameApplicationNotNullSQL string + +func (d *Database) PlayersByUsernameApplicationNotNull(username string) (pl []*Player, err error) { + return pl, d.db.Get(&pl, playersByUsernameApplicationNotNullSQL, strings.ToLower(username)) +} + +//go:embed queries/players_by_discord_isnull.sql +var playersByDiscordIsNullSQL string + +func (d *Database) PlayersByDiscordIsNull() (pl []*Player, err error) { + return pl, d.db.Select(pl, playersByDiscordIsNullSQL) +} + +//go:embed queries/player_update_application.sql +var playerUpdateApplicationSQL string + +func (d *Database) PlayerUpdateApplication(appID, playerID int64) error { + _, err := d.db.Exec(playerUpdateApplicationSQL, appID, playerID) + return err +} + +//go:embed queries/player_update_discord_id.sql +var playerUpdateDiscordIDSQL string + +func (d *Database) PlayerUpdateDiscordID(discordID string, playerID int64) error { + _, err := d.db.Exec(playerUpdateDiscordIDSQL, discordID, playerID) + return err +} diff --git a/database/queries/application_by_id.sql b/database/queries/application_by_id.sql new file mode 100644 index 0000000..2c3742b --- /dev/null +++ b/database/queries/application_by_id.sql @@ -0,0 +1,3 @@ +SELECT * +FROM minecraft_manager_application +WHERE id = ?; \ No newline at end of file diff --git a/database/queries/application_by_player_username.sql b/database/queries/application_by_player_username.sql new file mode 100644 index 0000000..66baf98 --- /dev/null +++ b/database/queries/application_by_player_username.sql @@ -0,0 +1,3 @@ +SELECT * +FROM minecraft_manager_application +WHERE id = (SELECT application_id FROM minecraft_manager_player WHERE LOWER(username) = ?); \ No newline at end of file diff --git a/database/queries/application_update_accepted.sql b/database/queries/application_update_accepted.sql new file mode 100644 index 0000000..9533771 --- /dev/null +++ b/database/queries/application_update_accepted.sql @@ -0,0 +1,3 @@ +UPDATE minecraft_manager_application +SET accepted = ? +WHERE id = ?; \ No newline at end of file diff --git a/database/queries/applications_by_username.sql b/database/queries/applications_by_username.sql new file mode 100644 index 0000000..570416b --- /dev/null +++ b/database/queries/applications_by_username.sql @@ -0,0 +1,3 @@ +SELECT * +FROM minecraft_manager_application +WHERE LOWER(username) LIKE ?; \ No newline at end of file diff --git a/database/queries/player_update_application.sql b/database/queries/player_update_application.sql new file mode 100644 index 0000000..fcc7dde --- /dev/null +++ b/database/queries/player_update_application.sql @@ -0,0 +1,3 @@ +UPDATE minecraft_manager_player +SET application_id = ? +WHERE id = ?; \ No newline at end of file diff --git a/database/queries/player_update_discord_id.sql b/database/queries/player_update_discord_id.sql new file mode 100644 index 0000000..188e4c2 --- /dev/null +++ b/database/queries/player_update_discord_id.sql @@ -0,0 +1,3 @@ +UPDATE minecraft_manager_player +SET discord_id = ? +WHERE id = ?; \ No newline at end of file diff --git a/database/queries/players_by_discord_isnull.sql b/database/queries/players_by_discord_isnull.sql new file mode 100644 index 0000000..ae33c28 --- /dev/null +++ b/database/queries/players_by_discord_isnull.sql @@ -0,0 +1,3 @@ +SELECT * +FROM minecraft_manager_player +WHERE discord_id IS NULL; \ No newline at end of file diff --git a/database/queries/players_by_username.sql b/database/queries/players_by_username.sql new file mode 100644 index 0000000..76d5986 --- /dev/null +++ b/database/queries/players_by_username.sql @@ -0,0 +1,3 @@ +SELECT * +FROM minecraft_manager_player +WHERE username LIKE ?; \ No newline at end of file diff --git a/database/queries/players_by_username_application_notnull.sql b/database/queries/players_by_username_application_notnull.sql new file mode 100644 index 0000000..de754de --- /dev/null +++ b/database/queries/players_by_username_application_notnull.sql @@ -0,0 +1,4 @@ +SELECT * +FROM minecraft_manager_player +WHERE username LIKE ? + AND application_id IS NOT NULL; \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1dc514c --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.jojodev.com/minecraft/mcm-discord + +go 1.17 + +require ( + github.com/go-sql-driver/mysql v1.6.0 + github.com/jmoiron/sqlx v1.3.4 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d76d949 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w= +github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= +github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= diff --git a/main.go b/main.go new file mode 100644 index 0000000..da29a2c --- /dev/null +++ b/main.go @@ -0,0 +1,4 @@ +package main + +func main() { +}