summary refs log tree commit diff stats
path: root/models/database.go
blob: 4998bde2fbf782af3c86ce84bf0b64823ea455dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package models

import (
	"os"
	"time"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type List struct {
	Name      string `gorm:"primaryKey"`
	Password  []byte
	CreatedAt time.Time
	UpdatedAt time.Time
	Entries   []Entry
}

type Entry struct {
	ID        uint `gorm:"primaryKey"`
	Text      string
	Checked   bool
	CreatedAt time.Time
	UpdatedAt time.Time
	ListName  string
}

var DB *gorm.DB

func ConnectDatabase() {
	db, err := gorm.Open(sqlite.Open("shopping_list.db"), &gorm.Config{})

	if err != nil {
		panic("Failed to connect to database!")
	}

	db.AutoMigrate(&List{})
	db.AutoMigrate(&Entry{})

	DB = db
}

func BasePath() string {
	basePath := os.Getenv("BASE_PATH")
	// if basePath == "" {
		// basePath = "/"
	// }

	return basePath
}