diff --git a/cmd/registeruser/main.go b/cmd/registeruser/main.go index dd27c8d..fd573db 100644 --- a/cmd/registeruser/main.go +++ b/cmd/registeruser/main.go @@ -1,10 +1,31 @@ package main -import "fmt" +import ( + "database/sql" + "fmt" + "os" + + _ "github.com/mattn/go-sqlite3" + + "git.tilde.town/tildetown/town/towndb" +) // TODO this command adds a new user to /town/var/town.db -// it's meant to be invoked by the welcome binary upon successfully creating a new user account +// it's meant to be invoked by the welcome binary upon successfully +// creating a new user account + +func _main(db *sql.DB) error { + return nil +} func main() { - fmt.Println("lol") + towndb, err := towndb.ConnectDB() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + if err = _main(towndb); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } } diff --git a/models/models.go b/models/models.go index f6a25b0..ffce1c6 100644 --- a/models/models.go +++ b/models/models.go @@ -193,22 +193,3 @@ func (s *TownSignup) All(db *sql.DB) ([]*TownSignup, error) { return out, nil } - -// below is all TODO and unused rn -type UserState string - -const ( - StateActive = "active" - StateTempBan = "temp_banned" - StateBan = "banned" -) - -type TownAccount struct { - ID int64 - Emails []string - Username string - Signup int - //Notes []AdminNote - State UserState - Admin bool -} diff --git a/signup/signup.go b/signup/signup.go index e7c6cc5..72d3562 100644 --- a/signup/signup.go +++ b/signup/signup.go @@ -1,7 +1,5 @@ package signup -// TODO delete this file - import ( "database/sql" diff --git a/towndb/towndb.go b/towndb/towndb.go new file mode 100644 index 0000000..f009e41 --- /dev/null +++ b/towndb/towndb.go @@ -0,0 +1,76 @@ +package towndb + +import ( + "database/sql" + "time" + + _ "github.com/mattn/go-sqlite3" +) + +const dsn = "/town/var/town.db?mode=rw" + +type UserState string + +const ( + StateActive = "active" + StateTempBan = "temp_banned" + StateBan = "banned" + StateDeleted = "deleted" // some users request deletion +) + +type AdminNote struct { + ID int64 + Created time.Time + AuthorID int64 + Content string + UserID int64 +} + +func (n *AdminNote) Insert(db *sql.DB) error { + n.Created = time.Now() + stmt, err := db.Prepare(` + INSERT INTO notes (created, authorid, content, userid) + VALUES ( + ?, ?, ?, ? + )`) + if err != nil { + return err + } + + result, err := stmt.Exec( + n.Created.Unix(), + n.AuthorID, + n.Content, + n.UserID) + if err != nil { + return err + } + defer stmt.Close() + + liid, err := result.LastInsertId() + if err != nil { + return err + } + + n.ID = liid + + return nil +} + +type TownUser struct { + ID int64 + Emails []string + Username string + Notes []AdminNote + State UserState + IsAdmin bool +} + +func ConnectDB() (*sql.DB, error) { + db, err := sql.Open("sqlite3", dsn) + if err != nil { + return nil, err + } + + return db, nil +}