first commit
This commit is contained in:
34
internal/datastore/mock_coffee_repository.go
Normal file
34
internal/datastore/mock_coffee_repository.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package datastore
|
||||
|
||||
import (
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/internal/domain/entities"
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/internal/domain/repositories"
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/pkg/errors"
|
||||
)
|
||||
|
||||
type MockCoffeeRepository struct {
|
||||
coffees []entities.Coffee
|
||||
}
|
||||
|
||||
func NewMockCoffeeRepository() repositories.CoffeeRepository {
|
||||
return &MockCoffeeRepository{
|
||||
coffees: []entities.Coffee{
|
||||
{ID: 1, Name: "Эспрессо", Description: "Крепкий черный кофе", Price: 120.0, Size: "S"},
|
||||
{ID: 2, Name: "Капучино", Description: "Кофе с молочной пенкой", Price: 180.0, Size: "M"},
|
||||
// ... остальные данные
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MockCoffeeRepository) FindAll() ([]entities.Coffee, error) {
|
||||
return r.coffees, nil
|
||||
}
|
||||
|
||||
func (r *MockCoffeeRepository) FindByID(id int) (*entities.Coffee, error) {
|
||||
for _, coffee := range r.coffees {
|
||||
if coffee.ID == id {
|
||||
return &coffee, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.NewNotFoundError("coffee not found")
|
||||
}
|
||||
82
internal/datastore/mock_order_repository.go
Normal file
82
internal/datastore/mock_order_repository.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package datastore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/internal/domain/entities"
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/internal/domain/repositories"
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/pkg/errors"
|
||||
)
|
||||
|
||||
type MockOrderRepository struct {
|
||||
orders []entities.Order
|
||||
nextID int
|
||||
}
|
||||
|
||||
func NewMockOrderRepository() repositories.OrderRepository {
|
||||
return &MockOrderRepository{
|
||||
orders: []entities.Order{
|
||||
{
|
||||
ID: 1,
|
||||
Items: []entities.Item{
|
||||
{ProductID: 1, Quantity: 1},
|
||||
{ProductID: 1, Quantity: 2},
|
||||
},
|
||||
Total: 320.0,
|
||||
Status: "completed",
|
||||
Timestamp: "2024-01-15 10:30:00",
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Items: []entities.Item{
|
||||
{ProductID: 2, Quantity: 1},
|
||||
{ProductID: 3, Quantity: 1},
|
||||
},
|
||||
Total: 380.0,
|
||||
Status: "preparing",
|
||||
Timestamp: "2024-01-15 11:15:00",
|
||||
},
|
||||
},
|
||||
nextID: 3,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MockOrderRepository) FindAll() ([]entities.Order, error) {
|
||||
return r.orders, nil
|
||||
}
|
||||
|
||||
func (r *MockOrderRepository) FindByID(id int) (*entities.Order, error) {
|
||||
for _, order := range r.orders {
|
||||
if order.ID == id {
|
||||
return &order, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.NewNotFoundError("order not found")
|
||||
}
|
||||
|
||||
func (r *MockOrderRepository) Save(order *entities.Order) error {
|
||||
if order == nil {
|
||||
return errors.NewInvalidError("order cannot be nil")
|
||||
}
|
||||
|
||||
// Если это новый заказ (ID = 0), присваиваем следующий ID
|
||||
if order.ID == 0 {
|
||||
order.ID = r.nextID
|
||||
order.Timestamp = time.Now().Format("2006-01-02 15:04:05")
|
||||
if order.Status == "" {
|
||||
order.Status = "created"
|
||||
}
|
||||
r.orders = append(r.orders, *order)
|
||||
r.nextID++
|
||||
} else {
|
||||
// Обновление существующего заказа
|
||||
for i, existingOrder := range r.orders {
|
||||
if existingOrder.ID == order.ID {
|
||||
r.orders[i] = *order
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
51
internal/datastore/mock_pastry_repository.go
Normal file
51
internal/datastore/mock_pastry_repository.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package datastore
|
||||
|
||||
import (
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/internal/domain/entities"
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/internal/domain/repositories"
|
||||
"code.linberg.su/linberg/awesome-cli/awesome-back/pkg/errors"
|
||||
)
|
||||
|
||||
type MockPastryRepository struct {
|
||||
pastries []entities.Pastry
|
||||
}
|
||||
|
||||
func NewMockPastryRepository() repositories.PastryRepository {
|
||||
return &MockPastryRepository{
|
||||
pastries: []entities.Pastry{
|
||||
{ID: 1, Name: "Круассан", Description: "Слоеная выпечка с маслом", Price: 80.0, Category: "Выпечка"},
|
||||
{ID: 2, Name: "Тирамису", Description: "Итальянский десерт", Price: 150.0, Category: "Десерты"},
|
||||
{ID: 3, Name: "Чизкейк", Description: "Сырный торт", Price: 130.0, Category: "Десерты"},
|
||||
{ID: 4, Name: "Маффин", Description: "Шоколадный кекс", Price: 70.0, Category: "Выпечка"},
|
||||
{ID: 5, Name: "Печенье", Description: "Домашнее овсяное печенье", Price: 50.0, Category: "Печенье"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MockPastryRepository) FindAll() ([]entities.Pastry, error) {
|
||||
return r.pastries, nil
|
||||
}
|
||||
|
||||
func (r *MockPastryRepository) FindByID(id int) (*entities.Pastry, error) {
|
||||
for _, pastry := range r.pastries {
|
||||
if pastry.ID == id {
|
||||
return &pastry, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.NewNotFoundError("pastry not found")
|
||||
}
|
||||
|
||||
func (r *MockPastryRepository) FindByCategory(category string) ([]entities.Pastry, error) {
|
||||
var filtered []entities.Pastry
|
||||
for _, pastry := range r.pastries {
|
||||
if pastry.Category == category {
|
||||
filtered = append(filtered, pastry)
|
||||
}
|
||||
}
|
||||
|
||||
if len(filtered) == 0 {
|
||||
return nil, errors.NewNotFoundError("no pastries found for category")
|
||||
}
|
||||
|
||||
return filtered, nil
|
||||
}
|
||||
Reference in New Issue
Block a user