49 lines
1003 B
Go
49 lines
1003 B
Go
package units
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Service struct {
|
|
repo Repository
|
|
}
|
|
|
|
func NewService(repo Repository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) List(ctx context.Context) ([]*Unit, error) {
|
|
return s.repo.List(ctx)
|
|
}
|
|
|
|
func (s *Service) Get(ctx context.Context, id uuid.UUID) (*Unit, error) {
|
|
return s.repo.FindByID(ctx, id)
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, name, symbol string) (*Unit, error) {
|
|
u := &Unit{ID: uuid.New(), Name: name, Symbol: symbol}
|
|
if err := s.repo.Create(ctx, u); err != nil {
|
|
return nil, err
|
|
}
|
|
return u, nil
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, id uuid.UUID, name, symbol string) (*Unit, error) {
|
|
u, err := s.repo.FindByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
u.Name = name
|
|
u.Symbol = symbol
|
|
if err := s.repo.Update(ctx, u); err != nil {
|
|
return nil, err
|
|
}
|
|
return u, nil
|
|
}
|
|
|
|
func (s *Service) Delete(ctx context.Context, id uuid.UUID) error {
|
|
return s.repo.Delete(ctx, id)
|
|
}
|