rentease/internal/service/booking/service.go
Ruidy bddc4bb0fc
embed html template (#40)
### TL;DR

Implemented embedded file system for static assets using Go's `embed` package.

### What changed?

- Created a new `assets.go` file to define an embedded filesystem for static assets
- Moved all static assets (HTML, icons, images, JS) under a nested `assets` directory
- Updated PDF generation to use the embedded filesystem when parsing HTML templates
- Modified main application to use the embedded filesystem for serving static files
- Added logging statements for invoice generation

### How to test?

1. Run the application and verify static assets are served correctly
2. Generate a PDF invoice and confirm it renders properly
3. Check that all HTML error pages (400, 401, 403, 404, 500) are accessible
4. Verify images and icons load correctly throughout the application

### Why make this change?

Using an embedded filesystem ensures all static assets are compiled into the binary, making deployment simpler and more reliable. This eliminates the need to manage separate asset files and ensures the application has all required resources available at runtime.
2025-02-04 18:49:20 +01:00

106 lines
2.7 KiB
Go

package booking
import (
"log"
"time"
"github.com/rjNemo/rentease/internal/config"
)
type Store interface {
All() []*Line
Search(value string) []*Line
List(from, to time.Time) ([]*Line, error)
CardTotal(from, to time.Time) (float64, error)
Get(id int) *Booking
Create(b *Booking) error
Update(b *Booking) error
Cancel(id int) error
// Item methods
CreateItem(i *Item) error
PayItem(id int) (*Item, error)
GetItem(id int) (*Item, error)
UpdateItem(id int, item string, paymentMethod string, paymentStatus string, qty int, price float64) (*Item, error)
// Payment methods
CreatePayment(p *Payment) (*Payment, error)
GetPayment(id int) (*Payment, error)
UpdatePayment(id int, amount float64, paymentMethod string) (*Payment, error)
}
type PdfClient interface {
BuildInvoice(invoice Invoice) (string, error)
BuildReport(context map[string]any, period string, month, year int) error
}
type CalendarClient interface {
Create(calendarId, name, description string, from, to time.Time) error
}
type parserClient interface {
Parse(rawContent string) (*Booking, error)
}
type Service struct {
store Store
parser parserClient
calendar CalendarClient
pdf PdfClient
}
func NewService(store Store, parser parserClient, calendar CalendarClient, pdf PdfClient) (*Service, error) {
return &Service{
store: store,
parser: parser,
calendar: calendar,
pdf: pdf,
}, nil
}
func (bs Service) All() []*Line {
return bs.store.All()
}
func (bs Service) Search(value string) []*Line {
return bs.store.Search(value)
}
// TODO: return the error
func (bs Service) Create(From time.Time, To time.Time, Name, PhoneNumber, Email, Platform string,
CustomerNumber int, PlatformFees float64, externalId *string,
) *Booking {
b := NewBooking(From, To, Name, PhoneNumber, Email, Platform, CustomerNumber, PlatformFees, externalId)
err := bs.store.Create(b)
if err != nil {
log.Println(err)
}
return b
}
func (bs Service) One(id int) *Booking {
return bs.store.Get(id)
}
// Update updates an existing booking with new data
func (bs Service) Update(id int, From time.Time, To time.Time, Name string, PhoneNumber string, Email string, Platform string,
CustomerNumber int, PlatformFees float64, externalId *string,
) *Booking {
b := NewBooking(From, To, Name, PhoneNumber, Email, Platform, CustomerNumber, PlatformFees, externalId).WithId(id)
if err := bs.store.Update(b); err != nil {
log.Println(err)
}
return b
}
func (bs Service) Cancel(id int) {
err := bs.store.Cancel(id)
if err != nil {
log.Println(err)
}
}
func (bs Service) BuildInvoice(b *Booking, hc *config.Host) (string, error) {
log.Println("Build invoice")
return bs.pdf.BuildInvoice(b.ToInvoice(hc))
}