refactor(services): Move staticservice to struct and interface
This commit is contained in:
@@ -16,6 +16,7 @@ type Templater interface {
|
||||
|
||||
type DefaultTemplater struct {
|
||||
BarcodeService BarcodeService
|
||||
StaticService StaticService
|
||||
}
|
||||
|
||||
func idToEan13(id string, prefix string) (string, error) {
|
||||
@@ -47,15 +48,15 @@ func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix st
|
||||
}
|
||||
|
||||
func (t *DefaultTemplater) SelectSponsorImage(id int) (string, error) {
|
||||
sponsors, err := ListFilesInStaticSubFolder("images/sponsors")
|
||||
sponsors, err := t.StaticService.ListFilesInStaticSubFolder("images/sponsors")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return GetImage("sponsors/" + strings.TrimSuffix(sponsors[id%len(sponsors)], ".base64")), nil
|
||||
return t.StaticService.GetImage("sponsors/" + strings.TrimSuffix(sponsors[id%len(sponsors)], ".base64")), nil
|
||||
}
|
||||
|
||||
func (t *DefaultTemplater) LoadImage(name string) (string, error) {
|
||||
return GetImage(name), nil
|
||||
return t.StaticService.GetImage(name), nil
|
||||
}
|
||||
|
||||
func (t *DefaultTemplater) FormatUnit(unit string, locale string, amount int) (string, error) {
|
||||
|
||||
@@ -7,7 +7,16 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetTemplate(locale, templateName string) (string, error) {
|
||||
type StaticService interface {
|
||||
GetTemplate(locale, templateName string) (string, error)
|
||||
ListFilesInStaticSubFolder(folderName string) ([]string, error)
|
||||
GetImage(imageName string) string
|
||||
}
|
||||
|
||||
type DefaultStaticService struct {
|
||||
}
|
||||
|
||||
func (s *DefaultStaticService) GetTemplate(locale, templateName string) (string, error) {
|
||||
content, err := os.ReadFile(fmt.Sprintf("static/templates/%s/%s.html", templateName, locale))
|
||||
if content == nil || err != nil {
|
||||
log.Printf("error reading template %s with locale %s: %v", templateName, locale, err)
|
||||
@@ -16,7 +25,7 @@ func GetTemplate(locale, templateName string) (string, error) {
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
func ListFilesInStaticSubFolder(folderName string) ([]string, error) {
|
||||
func (s *DefaultStaticService) ListFilesInStaticSubFolder(folderName string) ([]string, error) {
|
||||
files, err := os.ReadDir(fmt.Sprintf("static/%s", folderName))
|
||||
if err != nil {
|
||||
log.Printf("error reading files from folder %s: %v", folderName, err)
|
||||
@@ -32,7 +41,7 @@ func ListFilesInStaticSubFolder(folderName string) ([]string, error) {
|
||||
return images, nil
|
||||
}
|
||||
|
||||
func GetImage(imageName string) string {
|
||||
func (s *DefaultStaticService) GetImage(imageName string) string {
|
||||
content, err := os.ReadFile("static/images/" + imageName + ".base64")
|
||||
if content == nil || err != nil {
|
||||
log.Printf("error reading image %s: %v", imageName, err)
|
||||
|
||||
Reference in New Issue
Block a user