first commit

This commit is contained in:
i.smyshlyaev
2025-10-16 12:46:23 +03:00
commit 9c813d4754
37 changed files with 1671 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
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
}

View File

@@ -0,0 +1,57 @@
// internal/service/order_service.go
package service
import (
"awesome-back/internal/domain/entities"
"awesome-back/internal/domain/repositories"
"awesome-back/pkg/errors"
)
type OrderService struct {
orderRepo repositories.OrderRepository
}
func NewOrderService(orderRepo repositories.OrderRepository) *OrderService {
return &OrderService{orderRepo: orderRepo}
}
func (s *OrderService) GetAllOrders() ([]entities.Order, error) {
orders, err := s.orderRepo.FindAll()
if err != nil {
return nil, errors.Wrap(err, "failed to get orders")
}
return orders, nil
}
func (s *OrderService) GetOrderByID(id int) (*entities.Order, error) {
if id <= 0 {
return nil, errors.NewInvalidError("invalid order ID")
}
order, err := s.orderRepo.FindByID(id)
if err != nil {
if errors.IsNotFound(err) {
return nil, errors.NewNotFoundError("order not found")
}
return nil, errors.Wrap(err, "failed to get order")
}
return order, nil
}
func (s *OrderService) CreateOrder(order *entities.Order) error {
if order == nil {
return errors.NewInvalidError("order cannot be nil")
}
if len(order.Items) == 0 {
return errors.NewInvalidError("order must contain at least one item")
}
err := s.orderRepo.Save(order)
if err != nil {
return errors.Wrap(err, "failed to create order")
}
return nil
}

View File

@@ -0,0 +1,52 @@
package service
import (
"awesome-back/internal/domain/entities"
"awesome-back/internal/domain/repositories"
"awesome-back/pkg/errors"
)
type PastryService struct {
repo repositories.PastryRepository
}
func NewPastryService(repo repositories.PastryRepository) *PastryService {
return &PastryService{repo: repo}
}
func (s *PastryService) GetAllPastries() ([]entities.Pastry, error) {
pastries, err := s.repo.FindAll()
if err != nil {
return nil, errors.Wrap(err, "failed to get pastries")
}
return pastries, nil
}
func (s *PastryService) GetPastryByID(id int) (*entities.Pastry, error) {
if id <= 0 {
return nil, errors.NewInvalidError("invalid pastry ID")
}
pastry, err := s.repo.FindByID(id)
if err != nil {
if errors.IsNotFound(err) {
return nil, errors.NewNotFoundError("pastry not found")
}
return nil, errors.Wrap(err, "failed to get pastry")
}
return pastry, nil
}
func (s *PastryService) GetPastriesByCategory(category string) ([]entities.Pastry, error) {
if category == "" {
return nil, errors.NewInvalidError("category cannot be empty")
}
pastries, err := s.repo.FindByCategory(category)
if err != nil {
return nil, errors.Wrap(err, "failed to get pastries by category")
}
return pastries, nil
}