Files
mach_examples/07_roundest_pokemon_htmx/main.c
2026-05-23 21:05:25 -05:00

46 lines
1023 B
C

#include <mach.h>
#include <htmx.h>
#include <sqlite.h>
#include <tailwind.h>
void mach(){
htmx_config();
sqlite_config();
tailwind_config();
sqlite_database(
.name = "pokemon_db",
.connect = "file::memory:?cache=shared",
.migrations = {"create_pokemons_table"}
);
resource("home", "/",
.get = {
sqlite_query({"pokemon_db", "get_challengers", "challengers"}),
exec(^(){
auto const t = get("challengers");
auto const p0 = table_get(t, 0);
auto const p1 = table_get(t, 1);
record_set(p0, "opponent_id", record_get(p1, "id"));
record_set(p1, "opponent_id", record_get(p0, "id"));
}),
mustache("home")
},
.post = {
input(
{"winner", m_positive},
{"loser", m_positive}
),
sqlite_query({"pokemon_db", "vote"}),
reroute("home")
}
);
resource("result", "/results",
.get = {
sqlite_query({"pokemon_db", "get_results", "results"}),
mustache("results")
}
);
}