Doing packages wrong...
This commit is contained in:
parent
0083f2887e
commit
5811525c64
5 changed files with 104 additions and 8 deletions
BIN
local/.records.go.swp
Normal file
BIN
local/.records.go.swp
Normal file
Binary file not shown.
3
local/go.mod
Normal file
3
local/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module git.rdx4.com/monqui/records
|
||||
|
||||
go 1.18
|
||||
89
local/records.go
Normal file
89
local/records.go
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package records
|
||||
|
||||
// User record management stuff. Eventually want to abstract
|
||||
// this out to store any sort of record...
|
||||
type User struct {
|
||||
id int64
|
||||
name string
|
||||
}
|
||||
|
||||
func CreateUser(db *sql.DB, name string) (User) {
|
||||
s := prep_query(db, "INSERT INTO users (name) VALUES (?)")
|
||||
res, err := s.Exec(name)
|
||||
check(err)
|
||||
|
||||
var u User
|
||||
|
||||
id, _ := res.LastInsertId()
|
||||
u.id = id
|
||||
u.name = name
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
func GetUser(db *sql.DB, name string) (User) {
|
||||
s := prep_query(db, "SELECT * FROM users WHERE name = ? LIMIT 1")
|
||||
res, err := s.Query(name)
|
||||
|
||||
check(err)
|
||||
|
||||
var u User
|
||||
|
||||
res.Next()
|
||||
res.Scan(&u.id, &u.name)
|
||||
res.Close()
|
||||
|
||||
if u.id == 0 {
|
||||
return create_user(db, name)
|
||||
} else {
|
||||
return u
|
||||
}
|
||||
}
|
||||
|
||||
// Blerk. Start here with a get/create and see where
|
||||
// that goes...
|
||||
type Entity struct {
|
||||
id int64
|
||||
user_id int64
|
||||
name string
|
||||
spawn_line string
|
||||
}
|
||||
|
||||
func CreateEntity(db *sql.DB, user_id int64, name string, spawn_line string) (Entity) {
|
||||
s := prep_query(db, "INSERT INTO entities (user_id, name, spawn_line) VALUES (?, ?, ?)")
|
||||
res, err := s.Exec(user_id, name, spawn_line)
|
||||
check(err)
|
||||
|
||||
var ent Entity
|
||||
|
||||
id, _ := res.LastInsertId()
|
||||
ent.id = id
|
||||
ent.user_id = user_id
|
||||
ent.name = name
|
||||
ent.spawn_line = spawn_line
|
||||
|
||||
return ent
|
||||
}
|
||||
|
||||
func GetEntity(db *sql.DB, id int64) (Entity) {
|
||||
s := prep_query(db, "SELECT * FROM entities WHERE id = ? LIMIT 1")
|
||||
res, err := s.Query(id)
|
||||
|
||||
check(err)
|
||||
|
||||
var ent Entity
|
||||
|
||||
res.Next()
|
||||
err = res.Scan(&ent.id, &ent.user_id, &ent.name, &ent.spawn_line)
|
||||
check(err)
|
||||
|
||||
res.Close()
|
||||
|
||||
|
||||
if ent.id == 0 {
|
||||
log.Printf("Entity id %d doesn't exist!", id)
|
||||
}
|
||||
|
||||
return ent
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue