generated from schreifuchs/wails-template
management of library
This commit is contained in:
@ -4,21 +4,44 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Thing struct {
|
||||
ID int
|
||||
Name string
|
||||
Subthings []SubThing
|
||||
type Author struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Books []Book
|
||||
}
|
||||
|
||||
type SubThing struct {
|
||||
ID int
|
||||
ThingID int
|
||||
Name string
|
||||
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 {
|
||||
@ -26,10 +49,10 @@ func InitDB() *gorm.DB {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
db, err := gorm.Open(sqlite.Open(path.Join(home, "things.db")))
|
||||
db, err := gorm.Open(sqlite.Open(path.Join(home, "library.db")))
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
db.AutoMigrate(&Thing{}, &SubThing{})
|
||||
db.AutoMigrate(&Author{}, &Book{}, &Client{}, &Lending{})
|
||||
return db
|
||||
}
|
||||
|
Reference in New Issue
Block a user