management of library
All checks were successful
build / windows (push) Successful in 2m38s
build / linux (push) Successful in 2m0s

This commit is contained in:
2025-03-07 11:51:21 +01:00
parent e90cdf4e6a
commit 5bedea5312
26 changed files with 1078 additions and 228 deletions

View File

@ -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
}