generated from schreifuchs/wails-template
133 lines
2.6 KiB
Go
133 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"library-manager/model"
|
|
|
|
"github.com/gen2brain/beeep"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// App struct
|
|
type App struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewApp creates a new App application struct
|
|
func NewApp(db *gorm.DB) *App {
|
|
|
|
return &App{db: db}
|
|
}
|
|
|
|
// startup is called when the app starts. The context is saved
|
|
// so we can call the runtime methods
|
|
func (a *App) startup(ctx context.Context) {
|
|
a.ctx = ctx
|
|
|
|
err := beeep.Notify("Hello", "World", "")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
|
|
// Greet returns a greeting for the given name
|
|
func (a *App) Greet(name string) string {
|
|
return fmt.Sprintf("Hello %s, It's show time!", name)
|
|
}
|
|
|
|
// Authors CRIUD
|
|
func (a *App) SaveAuthor(au *model.Author) {
|
|
a.db.Save(au)
|
|
}
|
|
|
|
func (a *App) DeleteAuthor(au *model.Author) {
|
|
a.db.Delete(au)
|
|
}
|
|
|
|
func (a *App) GetAuthors() (authors []model.Author) {
|
|
a.db.Find(&authors)
|
|
return
|
|
}
|
|
|
|
func (a *App) SaveBook(au *model.Book) {
|
|
a.db.Save(au)
|
|
}
|
|
|
|
func (a *App) DeleteBook(au *model.Book) {
|
|
a.db.Delete(au)
|
|
}
|
|
|
|
func (a *App) GetBooks() (authors []model.Book) {
|
|
a.db.Preload("Author").Preload("Lendings").Find(&authors)
|
|
return
|
|
}
|
|
func (a *App) GetAvailableBooks() (books []model.Book) {
|
|
var bs []model.Book
|
|
a.db.Preload("Author").Preload("Lendings").Find(&bs)
|
|
books = make([]model.Book, 0, len(bs))
|
|
|
|
for _, b := range bs {
|
|
if !a.BookLended(b.ID) {
|
|
books = append(books, b)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (a *App) BookLended(bId uint) bool {
|
|
var lendings []model.Lending
|
|
a.db.Where("book_id = ?", bId).Where("returned = FALSE").Find(&lendings)
|
|
|
|
fmt.Println(lendings)
|
|
|
|
return len(lendings) != 0
|
|
}
|
|
|
|
func (a *App) SaveClient(au *model.Client) {
|
|
a.db.Save(au)
|
|
}
|
|
|
|
func (a *App) DeleteClient(au *model.Client) {
|
|
a.db.Delete(au)
|
|
}
|
|
|
|
func (a *App) GetClients() (authors []model.Client) {
|
|
a.db.Preload("Lendings").Find(&authors)
|
|
return
|
|
}
|
|
|
|
func (a *App) SaveLending(au *model.Lending) string {
|
|
if a.BookLended(Greater(au.BookID, au.Book.ID)) {
|
|
return "Book is not available"
|
|
}
|
|
a.db.Save(au)
|
|
|
|
return ""
|
|
}
|
|
|
|
func (a *App) ReturnLending(l *model.Lending) {
|
|
l.Returned = true
|
|
a.db.Save(l)
|
|
|
|
}
|
|
|
|
func (a *App) DeleteLending(au *model.Lending) {
|
|
a.db.Delete(au)
|
|
}
|
|
|
|
func (a *App) GetLendings() (lendings []model.Lending) {
|
|
a.db.Preload("Client").Preload("Book").Where("returned = FALSE").Find(&lendings)
|
|
return
|
|
}
|
|
func (a *App) GetReturnedLendings() (lendings []model.Lending) {
|
|
a.db.Preload("Client").Preload("Book").Where("returned = TRUE").Find(&lendings)
|
|
return
|
|
}
|
|
func (a *App) GetAllLendings() (lendings []model.Lending) {
|
|
a.db.Preload("Client").Preload("Book").Find(&lendings)
|
|
return
|
|
}
|