All checks were successful
ci/woodpecker/push/govulncheck Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/tag/release Pipeline was successful
ci/woodpecker/tag/deployment Pipeline was successful
ci/woodpecker/cron/govulncheck Pipeline was successful
Reviewed-on: #52
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
)
|
|
|
|
type ServerSnapshotRepository interface {
|
|
// Create persists tribe snapshots in a store (e.g. Postgres).
|
|
// Duplicates are ignored.
|
|
Create(ctx context.Context, params ...domain.CreateServerSnapshotParams) error
|
|
ListWithRelations(
|
|
ctx context.Context,
|
|
params domain.ListServerSnapshotsParams,
|
|
) (domain.ListServerSnapshotsWithRelationsResult, error)
|
|
}
|
|
|
|
type ServerSnapshotService struct {
|
|
repo ServerSnapshotRepository
|
|
serverSvc *ServerService
|
|
pub SnapshotPublisher
|
|
}
|
|
|
|
func NewServerSnapshotService(
|
|
repo ServerSnapshotRepository,
|
|
serverSvc *ServerService,
|
|
pub SnapshotPublisher,
|
|
) *ServerSnapshotService {
|
|
return &ServerSnapshotService{repo: repo, serverSvc: serverSvc, pub: pub}
|
|
}
|
|
|
|
func (svc *ServerSnapshotService) Create(
|
|
ctx context.Context,
|
|
createSnapshotsCmdPayload domain.CreateSnapshotsCmdPayload,
|
|
) error {
|
|
versionCode := createSnapshotsCmdPayload.VersionCode()
|
|
serverKey := createSnapshotsCmdPayload.ServerKey()
|
|
date := createSnapshotsCmdPayload.Date()
|
|
|
|
server, err := svc.serverSvc.GetNormalByVersionCodeAndServerKey(ctx, versionCode, serverKey)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
}
|
|
|
|
if !server.Open() {
|
|
return nil
|
|
}
|
|
|
|
params, err := domain.NewCreateServerSnapshotParams(server, date)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
}
|
|
|
|
if err = svc.repo.Create(ctx, params); err != nil {
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
}
|
|
|
|
if err = svc.serverSvc.UpdateSnapshotCreatedAt(ctx, serverKey); err != nil {
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (svc *ServerSnapshotService) ListWithRelations(
|
|
ctx context.Context,
|
|
params domain.ListServerSnapshotsParams,
|
|
) (domain.ListServerSnapshotsWithRelationsResult, error) {
|
|
return svc.repo.ListWithRelations(ctx, params)
|
|
}
|