This repository has been archived on 2024-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
core-old/internal/service/version.go
Dawid Wysokiński 4e4493e33f
All checks were successful
continuous-integration/drone/push Build is passing
feat: tracing (#76)
Reviewed-on: twhelp/core#76
2022-09-12 05:34:46 +00:00

55 lines
1.4 KiB
Go

package service
import (
"context"
"fmt"
"gitea.dwysokinski.me/twhelp/core/internal/domain"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type VersionRepository interface {
List(ctx context.Context, params domain.ListVersionsParams) ([]domain.Version, int64, error)
GetByCode(ctx context.Context, code string) (domain.Version, error)
}
type Version struct {
repo VersionRepository
}
func NewVersion(repo VersionRepository) *Version {
return &Version{repo: repo}
}
func (v *Version) List(ctx context.Context, params domain.ListVersionsParams) ([]domain.Version, int64, error) {
ctx, span := tracer.Start(ctx, "Version.List")
defer span.End()
versions, count, err := v.repo.List(ctx, params)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return nil, 0, fmt.Errorf("VersionRepository.List: %w", err)
}
return versions, count, nil
}
func (v *Version) GetByCode(ctx context.Context, code string) (domain.Version, error) {
ctx, span := tracer.Start(ctx, "Version.GetByCode", trace.WithAttributes(
attribute.String("version.code", code),
))
defer span.End()
version, err := v.repo.GetByCode(ctx, code)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return domain.Version{}, fmt.Errorf("VersionRepository.GetByCode: %w", err)
}
return version, nil
}