generated from schreifuchs/wails-template
178 lines
3.8 KiB
Go
178 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"library-manager/model"
|
|
"os"
|
|
|
|
"github.com/jung-kurt/gofpdf"
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
"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
|
|
}
|
|
|
|
func (a *App) ExportLendings(clientID uint) error {
|
|
|
|
var client model.Client
|
|
if err := a.db.Preload("Lendings.Book.Author").First(&client, clientID).Error; err != nil {
|
|
return fmt.Errorf("error fetching client: %w", err)
|
|
}
|
|
|
|
home, _ := os.UserHomeDir()
|
|
|
|
outputPath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{DefaultDirectory: home, DefaultFilename: fmt.Sprintf("%s.pdf", client.Name)})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pdf := gofpdf.New("P", "mm", "A4", "")
|
|
pdf.SetFont("Arial", "B", 16)
|
|
pdf.AddPage()
|
|
|
|
pdf.Cell(40, 10, fmt.Sprintf("Lendings for %s", client.Name))
|
|
pdf.Ln(10)
|
|
pdf.SetFont("Arial", "", 12)
|
|
|
|
for _, lending := range client.Lendings {
|
|
returnedStatus := "No"
|
|
if lending.Returned {
|
|
returnedStatus = "Yes"
|
|
}
|
|
|
|
pdf.Cell(40, 10, fmt.Sprintf("Book: %s", lending.Book.Title))
|
|
pdf.Ln(5)
|
|
pdf.Cell(40, 10, fmt.Sprintf("Author: %s", lending.Book.Author.Name))
|
|
pdf.Ln(5)
|
|
pdf.Cell(40, 10, fmt.Sprintf("Due Date: %s", lending.DueDate.Format("2006-01-02")))
|
|
pdf.Ln(5)
|
|
pdf.Cell(40, 10, fmt.Sprintf("Returned: %s", returnedStatus))
|
|
pdf.Ln(10)
|
|
}
|
|
|
|
return pdf.OutputFileAndClose(outputPath)
|
|
}
|