schreifuchs 5bedea5312
All checks were successful
build / windows (push) Successful in 2m38s
build / linux (push) Successful in 2m0s
management of library
2025-03-07 11:51:21 +01:00

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
}