adding rest-api

This commit is contained in:
2026-07-08 06:59:16 -05:00
parent 0ef9b90bcd
commit 60c2f20fea
20 changed files with 524 additions and 0 deletions

57
rest-api/db/db.go Normal file
View File

@@ -0,0 +1,57 @@
package db
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
var DB *sql.DB
func InitDB() {
var err error
DB, err = sql.Open("sqlite3", "api.db")
if err != nil {
panic("Could not connect to database")
}
DB.SetMaxOpenConns(10)
DB.SetMaxIdleConns(5)
createTables()
}
func createTables() {
createUsersTable := `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
)
`
_, err := DB.Exec(createUsersTable)
if err != nil {
panic("Could not create users table.")
}
createEventsTable := `
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT NOT NULL,
location TEXT NOT NULL,
dateTime DATETIME NOT NULL,
user_id INTEGER,
FOREIGN KEY(user_id) REFERENCES users(id)
)
`
_, err = DB.Exec(createEventsTable)
if err != nil {
panic("Could not create table")
}
}