generated from schreifuchs/wails-template
59 lines
912 B
Go
59 lines
912 B
Go
package model
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Author struct {
|
|
gorm.Model
|
|
Name string
|
|
Books []Book
|
|
}
|
|
|
|
type Book struct {
|
|
gorm.Model
|
|
Title string
|
|
ISBN string
|
|
AuthorID uint
|
|
Author Author `gorm:"foreignKey:AuthorID"`
|
|
Lendings []Lending `gorm:"foreignKey:BookID"`
|
|
}
|
|
|
|
type Client struct {
|
|
gorm.Model
|
|
Name string
|
|
Email string
|
|
Lendings []Lending `gorm:"foreignKey:ClientID"`
|
|
}
|
|
|
|
type Lending struct {
|
|
gorm.Model
|
|
DueDate time.Time
|
|
Returned bool
|
|
|
|
ClientID uint
|
|
Client Client `gorm:"foreignKey:ClientID"`
|
|
|
|
BookID uint
|
|
Book Book `gorm:"foreignKey:BookID"`
|
|
}
|
|
|
|
func InitDB() *gorm.DB {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
db, err := gorm.Open(sqlite.Open(path.Join(home, "library.db")))
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
db.AutoMigrate(&Author{}, &Book{}, &Client{}, &Lending{})
|
|
return db
|
|
}
|