Setting up CRUD ops for Locations

main v0.0.1
Nick Dumas 1 week ago
parent 77123d56b2
commit 3bdc35e197

@ -35,4 +35,35 @@ func main() {
} }
queries := models.New(db) queries := models.New(db)
obsvID, err := queries.CreateObservable(
ctx,
models.CreateObservableParams{
Name: "The Crossroads",
Description: "You stand at the intersection of two well trod roads. You don't remember how you got here, where you were going, or who you were. All you know is that you must go forward.",
},
)
if err != nil {
log.Printf("error creating room observable: %s\n", err)
return
}
if err != nil {
log.Printf("error creating room observable: %s\n", err)
return
}
locID, err := queries.CreateLocation(
ctx,
models.CreateLocationParams{
X: 0,
Y: 0,
Z: 0,
World: 0,
Observable: obsvID,
},
)
log.Printf("created room: %d\n", locID)
} }

@ -1,28 +1,32 @@
create TABLE locations ( CREATE TABLE locations (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
x INTEGER NOT NULL, x INTEGER NOT NULL,
y INTEGER NOT NULL, y INTEGER NOT NULL,
z INTEGER NOT NULL, z INTEGER NOT NULL,
world INTEGER NOT NULL world INTEGER NOT NULL,
) observable INTEGER NOT NULL references observables(id)
);
create TABLE exits (
id INTEGER PRIMARY KEY,
to INTEGER REFERENCES locations(id),
from INTEGER REFERENCES locations(id)
)
create TABLE observables ( CREATE TABLE observables (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT NOT NULL description TEXT NOT NULL
) );
create TABLE inventories ( CREATE TABLE inventories (
id INTEGER PRIMARY KEY id INTEGER PRIMARY KEY
) );
create TABLE pools ( CREATE TABLE pools (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
type INTEGER NOT NULL type INTEGER NOT NULL
) );
CREATE TABLE exits (
id INTEGER PRIMARY KEY,
origin INTEGER NOT NULL,
dest INTEGER NOT NULL,
FOREIGN KEY (origin) REFERENCES locations (id),
FOREIGN KEY (dest) REFERENCES locations (id)
);

@ -4,14 +4,10 @@
package db package db
import (
"database/sql"
)
type Exit struct { type Exit struct {
ID int64 `db:"id" json:"id"` ID int64 `db:"id" json:"id"`
To sql.NullInt64 `db:"to" json:"to"` Origin int64 `db:"origin" json:"origin"`
From sql.NullInt64 `db:"from" json:"from"` Dest int64 `db:"dest" json:"dest"`
} }
type Inventory struct { type Inventory struct {
@ -24,6 +20,7 @@ type Location struct {
Y int64 `db:"y" json:"y"` Y int64 `db:"y" json:"y"`
Z int64 `db:"z" json:"z"` Z int64 `db:"z" json:"z"`
World int64 `db:"world" json:"world"` World int64 `db:"world" json:"world"`
Observable int64 `db:"observable" json:"observable"`
} }
type Observable struct { type Observable struct {

@ -6,26 +6,51 @@ package db
import ( import (
"context" "context"
"database/sql"
) )
type Querier interface { type Querier interface {
//CreateLocation // Location
// //
// insert INTO LOCATIONS (x,y,z,world) VALUES (?,?,?,?) // insert INTO locations (x,y,z,world, observable) VALUES (?,?,?,?,?)
CreateLocation(ctx context.Context, arg CreateLocationParams) error CreateLocation(ctx context.Context, arg CreateLocationParams) (int64, error)
// Observable
//
// insert INTO observables (name, description) VALUES (?,?)
CreateObservable(ctx context.Context, arg CreateObservableParams) (int64, error)
//DestroyLocation
//
// delete from locations where id = ?
DestroyLocation(ctx context.Context, id int64) error
// Exit
//
// select id, origin, dest from exits where id = ? LIMIT 1
GetExit(ctx context.Context, id int64) (Exit, error)
//GetLocation //GetLocation
// //
// select id, x, y, z, world from locations where id = ? LIMIT 1 // select id, x, y, z, world, observable from locations where id = ? LIMIT 1
GetLocation(ctx context.Context, id int64) (Location, error) GetLocation(ctx context.Context, id int64) (Location, error)
//GetLocationExitsFrom //GetLocationExitsFrom
// //
// select id, "to", "from" from exits where from = ? LIMIT 1 // select id, origin, dest from exits where dest = ?
GetLocationExitsFrom(ctx context.Context, from sql.NullInt64) ([]Exit, error) GetLocationExitsFrom(ctx context.Context, dest int64) ([]Exit, error)
//GetLocationExitsTo //GetLocationExitsTo
// //
// select id, "to", "from" from exits where to = ? LIMIT 1 // select id, origin, dest from exits where origin = ?
GetLocationExitsTo(ctx context.Context, to sql.NullInt64) ([]Exit, error) GetLocationExitsTo(ctx context.Context, origin int64) ([]Exit, error)
//GetObservable
//
// select id, name, description from observables where id = ? LIMIT 1
GetObservable(ctx context.Context, id int64) (Observable, error)
//UpdateLocation
//
// update locations SET
// x = ?,
// y = ?,
// z = ?,
// world = ?,
// observable = ?
// where id = ?
UpdateLocation(ctx context.Context, arg UpdateLocationParams) error
} }
var _ Querier = (*Queries)(nil) var _ Querier = (*Queries)(nil)

@ -7,11 +7,10 @@ package db
import ( import (
"context" "context"
"database/sql"
) )
const createLocation = `-- name: CreateLocation :exec const createLocation = `-- name: CreateLocation :execlastid
insert INTO LOCATIONS (x,y,z,world) VALUES (?,?,?,?) insert INTO locations (x,y,z,world, observable) VALUES (?,?,?,?,?)
` `
type CreateLocationParams struct { type CreateLocationParams struct {
@ -19,28 +18,79 @@ type CreateLocationParams struct {
Y int64 `db:"y" json:"y"` Y int64 `db:"y" json:"y"`
Z int64 `db:"z" json:"z"` Z int64 `db:"z" json:"z"`
World int64 `db:"world" json:"world"` World int64 `db:"world" json:"world"`
Observable int64 `db:"observable" json:"observable"`
} }
// CreateLocation // Location
// //
// insert INTO LOCATIONS (x,y,z,world) VALUES (?,?,?,?) // insert INTO locations (x,y,z,world, observable) VALUES (?,?,?,?,?)
func (q *Queries) CreateLocation(ctx context.Context, arg CreateLocationParams) error { func (q *Queries) CreateLocation(ctx context.Context, arg CreateLocationParams) (int64, error) {
_, err := q.db.ExecContext(ctx, createLocation, result, err := q.db.ExecContext(ctx, createLocation,
arg.X, arg.X,
arg.Y, arg.Y,
arg.Z, arg.Z,
arg.World, arg.World,
arg.Observable,
) )
if err != nil {
return 0, err
}
return result.LastInsertId()
}
const createObservable = `-- name: CreateObservable :execlastid
insert INTO observables (name, description) VALUES (?,?)
`
type CreateObservableParams struct {
Name string `db:"name" json:"name"`
Description string `db:"description" json:"description"`
}
// Observable
//
// insert INTO observables (name, description) VALUES (?,?)
func (q *Queries) CreateObservable(ctx context.Context, arg CreateObservableParams) (int64, error) {
result, err := q.db.ExecContext(ctx, createObservable, arg.Name, arg.Description)
if err != nil {
return 0, err
}
return result.LastInsertId()
}
const destroyLocation = `-- name: DestroyLocation :exec
delete from locations where id = ?
`
// DestroyLocation
//
// delete from locations where id = ?
func (q *Queries) DestroyLocation(ctx context.Context, id int64) error {
_, err := q.db.ExecContext(ctx, destroyLocation, id)
return err return err
} }
const getExit = `-- name: GetExit :one
select id, origin, dest from exits where id = ? LIMIT 1
`
// Exit
//
// select id, origin, dest from exits where id = ? LIMIT 1
func (q *Queries) GetExit(ctx context.Context, id int64) (Exit, error) {
row := q.db.QueryRowContext(ctx, getExit, id)
var i Exit
err := row.Scan(&i.ID, &i.Origin, &i.Dest)
return i, err
}
const getLocation = `-- name: GetLocation :one const getLocation = `-- name: GetLocation :one
select id, x, y, z, world from locations where id = ? LIMIT 1 select id, x, y, z, world, observable from locations where id = ? LIMIT 1
` `
// GetLocation // GetLocation
// //
// select id, x, y, z, world from locations where id = ? LIMIT 1 // select id, x, y, z, world, observable from locations where id = ? LIMIT 1
func (q *Queries) GetLocation(ctx context.Context, id int64) (Location, error) { func (q *Queries) GetLocation(ctx context.Context, id int64) (Location, error) {
row := q.db.QueryRowContext(ctx, getLocation, id) row := q.db.QueryRowContext(ctx, getLocation, id)
var i Location var i Location
@ -50,19 +100,20 @@ func (q *Queries) GetLocation(ctx context.Context, id int64) (Location, error) {
&i.Y, &i.Y,
&i.Z, &i.Z,
&i.World, &i.World,
&i.Observable,
) )
return i, err return i, err
} }
const getLocationExitsFrom = `-- name: GetLocationExitsFrom :many const getLocationExitsFrom = `-- name: GetLocationExitsFrom :many
select id, "to", "from" from exits where from = ? LIMIT 1 select id, origin, dest from exits where dest = ?
` `
// GetLocationExitsFrom // GetLocationExitsFrom
// //
// select id, "to", "from" from exits where from = ? LIMIT 1 // select id, origin, dest from exits where dest = ?
func (q *Queries) GetLocationExitsFrom(ctx context.Context, from sql.NullInt64) ([]Exit, error) { func (q *Queries) GetLocationExitsFrom(ctx context.Context, dest int64) ([]Exit, error) {
rows, err := q.db.QueryContext(ctx, getLocationExitsFrom, from) rows, err := q.db.QueryContext(ctx, getLocationExitsFrom, dest)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -70,7 +121,7 @@ func (q *Queries) GetLocationExitsFrom(ctx context.Context, from sql.NullInt64)
var items []Exit var items []Exit
for rows.Next() { for rows.Next() {
var i Exit var i Exit
if err := rows.Scan(&i.ID, &i.To, &i.From); err != nil { if err := rows.Scan(&i.ID, &i.Origin, &i.Dest); err != nil {
return nil, err return nil, err
} }
items = append(items, i) items = append(items, i)
@ -85,14 +136,14 @@ func (q *Queries) GetLocationExitsFrom(ctx context.Context, from sql.NullInt64)
} }
const getLocationExitsTo = `-- name: GetLocationExitsTo :many const getLocationExitsTo = `-- name: GetLocationExitsTo :many
select id, "to", "from" from exits where to = ? LIMIT 1 select id, origin, dest from exits where origin = ?
` `
// GetLocationExitsTo // GetLocationExitsTo
// //
// select id, "to", "from" from exits where to = ? LIMIT 1 // select id, origin, dest from exits where origin = ?
func (q *Queries) GetLocationExitsTo(ctx context.Context, to sql.NullInt64) ([]Exit, error) { func (q *Queries) GetLocationExitsTo(ctx context.Context, origin int64) ([]Exit, error) {
rows, err := q.db.QueryContext(ctx, getLocationExitsTo, to) rows, err := q.db.QueryContext(ctx, getLocationExitsTo, origin)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -100,7 +151,7 @@ func (q *Queries) GetLocationExitsTo(ctx context.Context, to sql.NullInt64) ([]E
var items []Exit var items []Exit
for rows.Next() { for rows.Next() {
var i Exit var i Exit
if err := rows.Scan(&i.ID, &i.To, &i.From); err != nil { if err := rows.Scan(&i.ID, &i.Origin, &i.Dest); err != nil {
return nil, err return nil, err
} }
items = append(items, i) items = append(items, i)
@ -113,3 +164,57 @@ func (q *Queries) GetLocationExitsTo(ctx context.Context, to sql.NullInt64) ([]E
} }
return items, nil return items, nil
} }
const getObservable = `-- name: GetObservable :one
select id, name, description from observables where id = ? LIMIT 1
`
// GetObservable
//
// select id, name, description from observables where id = ? LIMIT 1
func (q *Queries) GetObservable(ctx context.Context, id int64) (Observable, error) {
row := q.db.QueryRowContext(ctx, getObservable, id)
var i Observable
err := row.Scan(&i.ID, &i.Name, &i.Description)
return i, err
}
const updateLocation = `-- name: UpdateLocation :exec
update locations SET
x = ?,
y = ?,
z = ?,
world = ?,
observable = ?
where id = ?
`
type UpdateLocationParams struct {
X int64 `db:"x" json:"x"`
Y int64 `db:"y" json:"y"`
Z int64 `db:"z" json:"z"`
World int64 `db:"world" json:"world"`
Observable int64 `db:"observable" json:"observable"`
ID int64 `db:"id" json:"id"`
}
// UpdateLocation
//
// update locations SET
// x = ?,
// y = ?,
// z = ?,
// world = ?,
// observable = ?
// where id = ?
func (q *Queries) UpdateLocation(ctx context.Context, arg UpdateLocationParams) error {
_, err := q.db.ExecContext(ctx, updateLocation,
arg.X,
arg.Y,
arg.Z,
arg.World,
arg.Observable,
arg.ID,
)
return err
}

@ -1,11 +1,30 @@
-- name: CreateLocation :exec -- Location
insert INTO LOCATIONS (x,y,z,world) VALUES (?,?,?,?); -- name: CreateLocation :execlastid
insert INTO locations (x,y,z,world, observable) VALUES (?,?,?,?,?);
-- name: GetLocation :one -- name: GetLocation :one
select * from locations where id = ? LIMIT 1; select * from locations where id = ? LIMIT 1;
-- name: UpdateLocation :exec
update locations SET
x = ?,
y = ?,
z = ?,
world = ?,
observable = ?
where id = ?;
-- name: DestroyLocation :exec
delete from locations where id = ?;
-- name: GetLocationExitsTo :many
select * from exits where to = ? LIMIT 1;
-- Exit
-- name: GetExit :one
select * from exits where id = ? LIMIT 1;
-- name: GetLocationExitsTo :many
select * from exits where origin = ?;
-- name: GetLocationExitsFrom :many -- name: GetLocationExitsFrom :many
select * from exits where from = ? LIMIT 1; select * from exits where dest = ?;
-- Observable
-- name: CreateObservable :execlastid
insert INTO observables (name, description) VALUES (?,?);
-- name: GetObservable :one
select * from observables where id = ? LIMIT 1;

@ -1,28 +1,32 @@
create TABLE locations ( CREATE TABLE locations (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
x INTEGER NOT NULL, x INTEGER NOT NULL,
y INTEGER NOT NULL, y INTEGER NOT NULL,
z INTEGER NOT NULL, z INTEGER NOT NULL,
world INTEGER NOT NULL world INTEGER NOT NULL,
) observable INTEGER NOT NULL references observables(id)
);
create TABLE exits (
id INTEGER PRIMARY KEY,
to INTEGER REFERENCES locations(id),
from INTEGER REFERENCES locations(id)
)
create TABLE observables ( CREATE TABLE observables (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT NOT NULL description TEXT NOT NULL
) );
create TABLE inventories ( CREATE TABLE inventories (
id INTEGER PRIMARY KEY id INTEGER PRIMARY KEY
) );
create TABLE pools ( CREATE TABLE pools (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
type INTEGER NOT NULL type INTEGER NOT NULL
) );
CREATE TABLE exits (
id INTEGER PRIMARY KEY,
origin INTEGER NOT NULL,
dest INTEGER NOT NULL,
FOREIGN KEY (origin) REFERENCES locations (id),
FOREIGN KEY (dest) REFERENCES locations (id)
);

Loading…
Cancel
Save