commit
59c69250d9
|
@ -0,0 +1 @@
|
|||
.idea/
|
|
@ -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.
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
SELECT *
|
||||
FROM minecraft_manager_application
|
||||
WHERE id = ?;
|
|
@ -0,0 +1,3 @@
|
|||
SELECT *
|
||||
FROM minecraft_manager_application
|
||||
WHERE id = (SELECT application_id FROM minecraft_manager_player WHERE LOWER(username) = ?);
|
|
@ -0,0 +1,3 @@
|
|||
UPDATE minecraft_manager_application
|
||||
SET accepted = ?
|
||||
WHERE id = ?;
|
|
@ -0,0 +1,3 @@
|
|||
SELECT *
|
||||
FROM minecraft_manager_application
|
||||
WHERE LOWER(username) LIKE ?;
|
|
@ -0,0 +1,3 @@
|
|||
UPDATE minecraft_manager_player
|
||||
SET application_id = ?
|
||||
WHERE id = ?;
|
|
@ -0,0 +1,3 @@
|
|||
UPDATE minecraft_manager_player
|
||||
SET discord_id = ?
|
||||
WHERE id = ?;
|
|
@ -0,0 +1,3 @@
|
|||
SELECT *
|
||||
FROM minecraft_manager_player
|
||||
WHERE discord_id IS NULL;
|
|
@ -0,0 +1,3 @@
|
|||
SELECT *
|
||||
FROM minecraft_manager_player
|
||||
WHERE username LIKE ?;
|
|
@ -0,0 +1,4 @@
|
|||
SELECT *
|
||||
FROM minecraft_manager_player
|
||||
WHERE username LIKE ?
|
||||
AND application_id IS NOT NULL;
|
|
@ -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
|
||||
)
|
|
@ -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=
|
Loading…
Reference in New Issue