refactor services to use Interface and change name to app

This commit is contained in:
Robin Olsen
2026-02-22 16:16:09 +01:00
parent dbf619a054
commit 118e67dbf4
36 changed files with 247 additions and 219 deletions

View File

@@ -18,8 +18,9 @@ func NewBaseIssue() *IssueBuilder {
WithID("pm-abc").
WithTitle("Basic Issue").
WithDescription("Basic Description").
WithIssueType(TypeTask).
WithStatus(StatusOpen)
WithStatus(StatusOpen).
WithIssueType(TypeTask)
}
func (b *IssueBuilder) WithID(id string) *IssueBuilder {

View File

@@ -8,6 +8,14 @@ type Config struct {
StatisticsStoragePath string
}
var BaseConfig = Config{
RootCmd: "pm",
IssuePrefix: "pm",
WebAddress: ":8080",
BeadsDBPath: "./.pm/db.db",
StatisticsStoragePath: "./.pm/stats.json",
}
func (c Config) WithRootCmd(rootCmd string) Config {
c.RootCmd = rootCmd
return c

View File

@@ -0,0 +1,28 @@
package service
import (
"context"
"log/slog"
"github.com/LazyBachelor/LazyPM/internal/models"
"github.com/steveyegge/beads"
)
type App struct {
Config Config
Issues IssueService
Stats StatsService
Logger *slog.Logger
}
type IssueService interface {
beads.Storage
AllIssues(ctx context.Context) ([]models.Issue, error)
DeleteIssues() error
}
type StatsService interface {
Load(ctx context.Context) error
Save(ctx context.Context) error
GetStatistics() (models.Statistics, error)
}

View File

@@ -2,8 +2,8 @@ package service
import (
"context"
"database/sql"
"fmt"
"log/slog"
"os"
"time"
@@ -15,14 +15,7 @@ import (
"github.com/steveyegge/beads"
)
type Services struct {
Config Config
DB *sql.DB
Beads *BeadsService
Statistics *StatisticsService
}
func NewServices(ctx context.Context, config Config) (*Services, func(), error) {
func NewServices(ctx context.Context, config Config) (*App, func(), error) {
var cleanupFuncs []func()
if !initialized(config.BeadsDBPath) {
@@ -47,16 +40,18 @@ func NewServices(ctx context.Context, config Config) (*Services, func(), error)
StartTime: time.Now(),
})
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
statSvc, err := NewStatisticsService(statStore)
if err != nil {
return nil, nil, err
}
return &Services{
DB: beadsSvc.UnderlyingDB(),
Beads: beadsSvc,
Statistics: statSvc,
Config: config,
return &App{
Issues: beadsSvc,
Stats: statSvc,
Config: config,
Logger: logger,
}, func() { runCleanup(cleanupFuncs) }, nil
}