WIP towndb

pull/1/head
vilmibm 2023-03-04 22:19:27 +00:00
parent df4eeaba13
commit c0a8c50dbb
4 changed files with 100 additions and 24 deletions

View File

@ -1,10 +1,31 @@
package main 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 // 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() { 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)
}
} }

View File

@ -193,22 +193,3 @@ func (s *TownSignup) All(db *sql.DB) ([]*TownSignup, error) {
return out, nil 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
}

View File

@ -1,7 +1,5 @@
package signup package signup
// TODO delete this file
import ( import (
"database/sql" "database/sql"

76
towndb/towndb.go 100644
View File

@ -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
}