awesome-cli/back/internal/service/coffee_service.go

40 lines
902 B
Go

package service
import (
"awesome-back/internal/domain/entities"
"awesome-back/internal/domain/repositories"
"awesome-back/pkg/errors"
)
type CoffeeService struct {
repo repositories.CoffeeRepository
}
func NewCoffeeService(repo repositories.CoffeeRepository) *CoffeeService {
return &CoffeeService{repo: repo}
}
func (s *CoffeeService) GetAllCoffees() ([]entities.Coffee, error) {
coffees, err := s.repo.FindAll()
if err != nil {
return nil, errors.Wrap(err, "failed to get coffees")
}
return coffees, nil
}
func (s *CoffeeService) GetCoffeeByID(id int) (*entities.Coffee, error) {
if id <= 0 {
return nil, errors.NewInvalidError("invalid coffee ID")
}
coffee, err := s.repo.FindByID(id)
if err != nil {
if errors.IsNotFound(err) {
return nil, errors.NewNotFoundError("coffee not found")
}
return nil, errors.Wrap(err, "failed to get coffee")
}
return coffee, nil
}