adding rest-api
This commit is contained in:
57
rest-api/db/db.go
Normal file
57
rest-api/db/db.go
Normal 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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user