rip out all auth stuff, add a containerfile

This commit is contained in:
Finn 2024-04-23 17:34:57 -07:00
parent 7fdd02bd33
commit 350fc3b339
27 changed files with 885 additions and 28 deletions

View file

@ -1,6 +1,7 @@
package db
import (
"context"
"database/sql"
"embed"
@ -12,7 +13,7 @@ import (
)
func Get() (*Queries, *sql.DB, error) {
db, err := sql.Open("sqlite3", config.C.Database)
db, err := sql.Open("sqlite3", config.C.SqliteDatabase)
if err != nil {
return nil, nil, err
}
@ -24,7 +25,7 @@ func Get() (*Queries, *sql.DB, error) {
var migrations embed.FS
func Migrate() error {
logrus.WithField("dbfile", config.C.Database).Info("migrating database")
logrus.WithField("dbfile", config.C.SqliteDatabase).Info("migrating database")
_, conn, err := Get()
if err != nil {
@ -51,3 +52,36 @@ func NullString(s string) sql.NullString {
String: s,
}
}
type loggingDBTX struct {
Next DBTX
}
func (l loggingDBTX) ExecContext(ctx context.Context, query string, params ...interface{}) (sql.Result, error) {
logrus.WithFields(logrus.Fields{
"query": query,
"params": params,
}).Debug("ExecContext")
return l.Next.ExecContext(ctx, query, params...)
}
func (l loggingDBTX) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
logrus.WithFields(logrus.Fields{
"query": query,
}).Debug("PrepareContext")
return l.Next.PrepareContext(ctx, query)
}
func (l loggingDBTX) QueryContext(ctx context.Context, query string, params ...interface{}) (*sql.Rows, error) {
logrus.WithFields(logrus.Fields{
"query": query,
"params": params,
}).Debug("QueryContext")
return l.Next.QueryContext(ctx, query, params...)
}
func (l loggingDBTX) QueryRowContext(ctx context.Context, query string, params ...interface{}) *sql.Row {
logrus.WithFields(logrus.Fields{
"query": query,
"params": params,
}).Debug("QueryRowContext")
return l.Next.QueryRowContext(ctx, query, params...)
}

View file

@ -25,10 +25,35 @@ CREATE TABLE lock_log (
state TEXT NOT NULL,
code INTEGER REFERENCES lock_code_slots(id)
);
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL
);
CREATE TABLE user_codes (
id INTEGER PRIMARY KEY,
user INTEGER REFERENCES users(id),
name TEXT,
code TEXT UNIQUE NOT NULL,
start DATETIME,
end DATETIME
);
CREATE TABLE user_code_slots (
user_code REFERENCES user_codes(id),
lock REFERENCES locks(id),
slot REFERENCES lock_code_slots(id),
UNIQUE (user_code, lock)
);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE user_code_slots;
DROP TABLE user_codes;
DROP TABLE users;
DROP TABLE lock_log;
DROP TABLE lock_code_slots;
DROP TABLE locks;

View file

@ -30,3 +30,23 @@ type LockLog struct {
State string
Code sql.NullInt64
}
type User struct {
ID int64
Name string
}
type UserCode struct {
ID int64
User sql.NullInt64
Name sql.NullString
Code string
Start sql.NullTime
End sql.NullTime
}
type UserCodeSlot struct {
UserCode interface{}
Lock interface{}
Slot interface{}
}

14
db/queries/user_codes.sql Normal file
View file

@ -0,0 +1,14 @@
-- name: CreateUserCode :one
INSERT INTO user_codes (user, code, start, end) VALUES (?, ?, ?, ?) RETURNING id;
-- name: DeleteUserCode :exec
DELETE FROM user_codes WHERE id = ?;
-- name: AssignUserCodeSlot :exec
INSERT INTO user_code_slots (user_code, lock, slot) VALUES (?, ?, ?);
-- name: UnassignUserCodeSlot :exec
DELETE FROM user_code_slots WHERE user_code = ? AND lock = ?;
-- name: GetAllUserCodes :many
SELECT user_codes.*, users.name FROM user_codes, users WHERE user_codes.user = users.id;

8
db/queries/users.sql Normal file
View file

@ -0,0 +1,8 @@
-- name: CreateUser :exec
INSERT INTO users (name) VALUES (?);
-- name: GetUserByName :one
SELECT * FROM users WHERE name = ?;
-- name: GetUserByID :one
SELECT * FROM users WHERE id = ?;

27
db/user_codes.go Normal file
View file

@ -0,0 +1,27 @@
package db
import (
"time"
"git.janky.solutions/finn/lockserver/openapi"
)
func (u GetAllUserCodesRow) OpenAPI() openapi.UserCode {
resp := openapi.UserCode{Code: &u.Code}
if u.Name.Valid {
resp.User = &u.Name.String
}
if u.Start.Valid {
start := u.Start.Time.Format(time.RFC3339)
resp.Starts = &start
}
if u.End.Valid {
end := u.End.Time.Format(time.RFC3339)
resp.Ends = &end
}
return resp
}

117
db/user_codes.sql.go Normal file
View file

@ -0,0 +1,117 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.20.0
// source: user_codes.sql
package db
import (
"context"
"database/sql"
)
const assignUserCodeSlot = `-- name: AssignUserCodeSlot :exec
INSERT INTO user_code_slots (user_code, lock, slot) VALUES (?, ?, ?)
`
type AssignUserCodeSlotParams struct {
UserCode interface{}
Lock interface{}
Slot interface{}
}
func (q *Queries) AssignUserCodeSlot(ctx context.Context, arg AssignUserCodeSlotParams) error {
_, err := q.db.ExecContext(ctx, assignUserCodeSlot, arg.UserCode, arg.Lock, arg.Slot)
return err
}
const createUserCode = `-- name: CreateUserCode :one
INSERT INTO user_codes (user, code, start, end) VALUES (?, ?, ?, ?) RETURNING id
`
type CreateUserCodeParams struct {
User sql.NullInt64
Code string
Start sql.NullTime
End sql.NullTime
}
func (q *Queries) CreateUserCode(ctx context.Context, arg CreateUserCodeParams) (int64, error) {
row := q.db.QueryRowContext(ctx, createUserCode,
arg.User,
arg.Code,
arg.Start,
arg.End,
)
var id int64
err := row.Scan(&id)
return id, err
}
const deleteUserCode = `-- name: DeleteUserCode :exec
DELETE FROM user_codes WHERE id = ?
`
func (q *Queries) DeleteUserCode(ctx context.Context, id int64) error {
_, err := q.db.ExecContext(ctx, deleteUserCode, id)
return err
}
const getAllUserCodes = `-- name: GetAllUserCodes :many
SELECT user_codes.id, user_codes.user, user_codes.name, user_codes.code, user_codes.start, user_codes."end", users.name FROM user_codes, users WHERE user_codes.user = users.id
`
type GetAllUserCodesRow struct {
ID int64
User sql.NullInt64
Name sql.NullString
Code string
Start sql.NullTime
End sql.NullTime
Name_2 string
}
func (q *Queries) GetAllUserCodes(ctx context.Context) ([]GetAllUserCodesRow, error) {
rows, err := q.db.QueryContext(ctx, getAllUserCodes)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAllUserCodesRow
for rows.Next() {
var i GetAllUserCodesRow
if err := rows.Scan(
&i.ID,
&i.User,
&i.Name,
&i.Code,
&i.Start,
&i.End,
&i.Name_2,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const unassignUserCodeSlot = `-- name: UnassignUserCodeSlot :exec
DELETE FROM user_code_slots WHERE user_code = ? AND lock = ?
`
type UnassignUserCodeSlotParams struct {
UserCode interface{}
Lock interface{}
}
func (q *Queries) UnassignUserCodeSlot(ctx context.Context, arg UnassignUserCodeSlotParams) error {
_, err := q.db.ExecContext(ctx, unassignUserCodeSlot, arg.UserCode, arg.Lock)
return err
}

41
db/users.sql.go Normal file
View file

@ -0,0 +1,41 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.20.0
// source: users.sql
package db
import (
"context"
)
const createUser = `-- name: CreateUser :exec
INSERT INTO users (name) VALUES (?)
`
func (q *Queries) CreateUser(ctx context.Context, name string) error {
_, err := q.db.ExecContext(ctx, createUser, name)
return err
}
const getUserByID = `-- name: GetUserByID :one
SELECT id, name FROM users WHERE id = ?
`
func (q *Queries) GetUserByID(ctx context.Context, id int64) (User, error) {
row := q.db.QueryRowContext(ctx, getUserByID, id)
var i User
err := row.Scan(&i.ID, &i.Name)
return i, err
}
const getUserByName = `-- name: GetUserByName :one
SELECT id, name FROM users WHERE name = ?
`
func (q *Queries) GetUserByName(ctx context.Context, name string) (User, error) {
row := q.db.QueryRowContext(ctx, getUserByName, name)
var i User
err := row.Scan(&i.ID, &i.Name)
return i, err
}