-- @block create table
CREATE TABLE zeiten (
    id SERIAL PRIMARY KEY,
    logged_time TIMESTAMP DEFAULT NOW(),
    card_id VARCHAR,
    reader_id VARCHAR,
    booking_type INTEGER
);
-- @block insert data
INSERT INTO zeiten (card_id, reader_id, booking_type)
VALUES ('test_card', 'test_reader', '2')
RETURNING id,
    logged_time;
-- @block select
SELECT *
FROM anwesenheit;
-- @block select last entry from card id
SELECT *
FROM "zeiten"
WHERE "card_id" = 'test_card'
    AND "logged_time" >= now()::date + interval '1h'
ORDER BY "logged_time" DESC
LIMIT 1;
-- @block delete table
DROP TABLE IF EXISTS zeiten;
-- @block create table anwesenheit
DROP TABLE IF EXISTS "public"."anwesenheit";
CREATE TABLE "public"."anwesenheit" (
    "counter_id" SERIAL PRIMARY KEY,
    "timestamp" timestamp(6) DEFAULT CURRENT_TIMESTAMP,
    "card_uid" varchar(255) COLLATE "pg_catalog"."default",
    "check_in_out" int2,
    "geraet_id" int2
);
COMMENT ON COLUMN "public"."anwesenheit"."check_in_out" IS '1=Check In 2=Check Out 255=Automatic Check Out';
COMMENT ON COLUMN "public"."anwesenheit"."geraet_id" IS 'ID des Lesegerätes';
