Restructuring a bit...
This commit is contained in:
parent
a9cc7515d8
commit
8968271c3b
5 changed files with 3 additions and 5 deletions
89
internal/storage/records.go
Normal file
89
internal/storage/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