First working API.

Simple auth by creating sessions and storing in db
This commit is contained in:
Martin Berg Alstad
2024-08-29 16:43:04 +02:00
commit 5d5e6393ac
50 changed files with 6410 additions and 0 deletions

0
migrations/.keep Normal file
View File

View File

@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();

View File

@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,6 @@
DROP TABLE IF EXISTS hotel CASCADE;
DROP TABLE IF EXISTS room CASCADE;
DROP TABLE IF EXISTS reservation CASCADE;
DROP TABLE IF EXISTS task CASCADE;
DROP TABLE IF EXISTS "user" CASCADE;
DROP TABLE IF EXISTS session CASCADE;

View File

@ -0,0 +1,51 @@
CREATE TABLE hotel
(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL
);
CREATE TABLE room
(
id INTEGER PRIMARY KEY,
hotel_id INTEGER NOT NULL,
beds INTEGER NOT NULL CHECK (beds > 0),
size INTEGER NOT NULL CHECK (size > 0),
FOREIGN KEY (hotel_id) REFERENCES hotel (id)
);
CREATE TABLE "user"
(
email VARCHAR(255) PRIMARY KEY,
hash VARCHAR(255) NOT NULL,
salt VARCHAR(255) NOT NULL,
role SMALLINT NOT NULL CHECK ( role IN (0, 3) )
);
CREATE TABLE reservation
(
id SERIAL PRIMARY KEY,
room_id INTEGER NOT NULL,
start TIMESTAMP NOT NULL,
"end" TIMESTAMP NOT NULL,
"user" VARCHAR(255) NOT NULL,
checked_in BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (room_id) REFERENCES room (id),
FOREIGN KEY ("user") REFERENCES "user" (email)
);
CREATE TABLE task
(
id SERIAL PRIMARY KEY,
room_id INTEGER NOT NULL,
description TEXT NOT NULL,
status VARCHAR(12) NOT NULL CHECK ( status IN ('todo', 'in_progress', 'done') ),
FOREIGN KEY (room_id) REFERENCES room (id)
);
CREATE TABLE session
(
id VARCHAR(128) PRIMARY KEY,
data JSONB NOT NULL,
expiry_date TIMESTAMP NOT NULL
)