Initial commit still with some hardcoded things

This commit is contained in:
2025-09-16 22:57:21 +01:00
commit b578976dad
14 changed files with 722 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
// Package model models the SW data
package model
import (
"encoding/json"
"time"
"gopkg.in/yaml.v2"
)
// SW holds the data that describe an AT certified invoicing software
type SW struct {
Name string `json:"name" yaml:"name"`
Version string `json:"version" yaml:"version"`
Developer string `json:"developer" yaml:"developer"`
CertNo int `json:"certNo" yaml:"certNo"`
State string `json:"state" yaml:"state"`
DeveloperNIF string `json:"string" yaml:"string"`
CertDate time.Time `json:"certDate" yaml:"certDate"`
}
// SWList is just a list of SW
type SWList []SW
// ToYAML returns a []byte representing the struct in YAML.
func (s *SWList) ToYAML() []byte {
yamlData, _ := yaml.Marshal(s)
return yamlData
}
// ToJSON returns a []byte representing the struct in JSON.
func (s *SWList) ToJSON() []byte {
jsondata, _ := json.Marshal(s)
return jsondata
}
// FromJSON loads a SW struct from a []byte of a SW in JSON.
func (s *SWList) FromJSON(data []byte) error {
return json.Unmarshal(data, s)
}
// FromYAML loads a SW struct from a []byte of a SW in YAML.
func (s *SWList) FromYAML(data []byte) error {
return yaml.Unmarshal(data, s)
}
+62
View File
@@ -0,0 +1,62 @@
package repo
import (
"errors"
"os"
models "github.com/ESilva15/PTInvCertSWDB/sw/models"
"gopkg.in/yaml.v2"
)
type YamlRepo struct {
Data map[int]models.SW
}
func getSWList(fp string) (models.SWList, error) {
var swList models.SWList
file, err := os.ReadFile(fp)
if err != nil {
return models.SWList{}, err
}
err = yaml.Unmarshal(file, &swList)
if err != nil {
return models.SWList{}, err
}
return swList, nil
}
func loadDataFromFile(fp string) (map[int]models.SW, error) {
list, err := getSWList(fp)
if err != nil {
return map[int]models.SW{}, err
}
m := make(map[int]models.SW, len(list))
for k := range list {
m[list[k].CertNo] = list[k]
}
return m, nil
}
func NewYAMLRepo(fp string) (*YamlRepo, error) {
data, err := loadDataFromFile(fp)
if err != nil {
return nil, err
}
return &YamlRepo{
Data: data,
}, nil
}
func (yr YamlRepo) Get(id int) (models.SW, error) {
if val, ok := yr.Data[id]; ok {
return val, nil
}
return models.SW{}, errors.New("not found")
}
+8
View File
@@ -0,0 +1,8 @@
// Package repo has repositories to access the data
package repo
import models "github.com/ESilva15/PTInvCertSWDB/sw/models"
type SWRepo interface {
Get(id int) (models.SW, error)
}
+47
View File
@@ -0,0 +1,47 @@
// Package sw will represent the sw
package sw
import (
"errors"
models "github.com/ESilva15/PTInvCertSWDB/sw/models"
repo "github.com/ESilva15/PTInvCertSWDB/sw/repo"
)
type RepoType int
const (
FILEDB RepoType = iota
POSTGRES
)
var service *SWService
type SWService struct {
SWRepo repo.SWRepo
}
func StartSWService(rt RepoType) error {
var err error
var swRepo repo.SWRepo
switch rt {
case FILEDB:
swRepo, err = repo.NewYAMLRepo("./swdb.yaml")
if err != nil {
return errors.New("Failed to create yaml repo: " + err.Error())
}
default:
return errors.New("repo type not implemented yet")
}
service = &SWService{
SWRepo: swRepo,
}
return nil
}
func Get(id int) (models.SW, error) {
return service.SWRepo.Get(id)
}