initial commit
This commit is contained in:
6
internal/entity/category.go
Normal file
6
internal/entity/category.go
Normal file
@ -0,0 +1,6 @@
|
||||
package entity
|
||||
|
||||
type Category struct {
|
||||
ID int64
|
||||
Name string
|
||||
}
|
||||
11
internal/entity/error.go
Normal file
11
internal/entity/error.go
Normal file
@ -0,0 +1,11 @@
|
||||
package entity
|
||||
|
||||
type SimpleError string
|
||||
|
||||
func (err SimpleError) Error() string {
|
||||
return string(err)
|
||||
}
|
||||
|
||||
const (
|
||||
ErrNotFound SimpleError = "not found"
|
||||
)
|
||||
122
internal/entity/gooditem.go
Normal file
122
internal/entity/gooditem.go
Normal file
@ -0,0 +1,122 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type GoodsItem struct {
|
||||
Articul string `json:"sku"`
|
||||
Photo string `json:"photo"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Category string `json:"category"`
|
||||
Type string `json:"type"`
|
||||
Producer string `json:"producer"`
|
||||
Pack int `json:"pack"`
|
||||
Step int `json:"step"`
|
||||
Price float64 `json:"price"`
|
||||
TariffPrice float64 `json:"tariff_price"`
|
||||
Cart int64 `json:"cart"`
|
||||
Stock int `json:"stock"`
|
||||
}
|
||||
|
||||
type GoodsItemRaw struct {
|
||||
SKU string // or articul
|
||||
Photo string
|
||||
Certificate string
|
||||
Configurator []string
|
||||
Name []string // first element is name, second is description
|
||||
Category string
|
||||
Type string
|
||||
Producer string
|
||||
Storage string // ?
|
||||
Pack string // like "20 шт."
|
||||
Step string
|
||||
Price string // float actually
|
||||
TariffPrice string // float
|
||||
SpecialOffers []any
|
||||
Cart float64
|
||||
Other string
|
||||
}
|
||||
|
||||
type MappedGoodsRemnants map[int]GoodsRemnant
|
||||
type GoodsRemnant [4]int32
|
||||
|
||||
func ExtractProductIDs(items []GoodsItem) (out []int) {
|
||||
out = make([]int, 0, len(items))
|
||||
for _, item := range items {
|
||||
out = append(out, int(item.Cart))
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func MakeGoodsItem(
|
||||
gi GoodsItemRaw,
|
||||
remnants MappedGoodsRemnants,
|
||||
) (out GoodsItem, err error) {
|
||||
var name, desc string
|
||||
var pack, step int
|
||||
var price, tariffPrice float64
|
||||
|
||||
if len(gi.Name) >= 2 {
|
||||
name = gi.Name[0]
|
||||
desc = gi.Name[1]
|
||||
}
|
||||
|
||||
fixSpace := func(in string) string {
|
||||
return strings.ReplaceAll(in, " ", "")
|
||||
}
|
||||
|
||||
price, err = strconv.ParseFloat(fixSpace(gi.Price), 64)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("parsing price: %w", err)
|
||||
}
|
||||
tariffPrice, err = strconv.ParseFloat(fixSpace(gi.TariffPrice), 64)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("parsing tariff_price: %w", err)
|
||||
}
|
||||
|
||||
getDigits := func(in string) string {
|
||||
var sb strings.Builder
|
||||
sb.Grow(len(in))
|
||||
for _, c := range in {
|
||||
if unicode.IsDigit(c) {
|
||||
sb.WriteRune(c)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
const countTemplate = "%d"
|
||||
_, err = fmt.Sscanf(getDigits(gi.Pack), countTemplate, &pack)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("getting pack count (%s): %w", gi.Pack, err)
|
||||
}
|
||||
_, err = fmt.Sscanf(getDigits(gi.Step), countTemplate, &step)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("getting step count (%s): %w", gi.Step, err)
|
||||
}
|
||||
|
||||
return GoodsItem{
|
||||
Articul: gi.SKU,
|
||||
Photo: gi.Photo,
|
||||
Name: name,
|
||||
Description: desc,
|
||||
Category: gi.Category,
|
||||
Type: gi.Type,
|
||||
Producer: gi.Producer,
|
||||
Pack: pack,
|
||||
Step: step,
|
||||
Price: price,
|
||||
TariffPrice: tariffPrice,
|
||||
Cart: int64(gi.Cart),
|
||||
Stock: int(remnants[int(gi.Cart)][0]),
|
||||
}, nil
|
||||
}
|
||||
23
internal/entity/repository.go
Normal file
23
internal/entity/repository.go
Normal file
@ -0,0 +1,23 @@
|
||||
package entity
|
||||
|
||||
import "context"
|
||||
|
||||
type GoodsItemRepository interface {
|
||||
ListIter(context.Context, int) (<-chan GoodsItem, error)
|
||||
List(context.Context) ([]GoodsItem, error)
|
||||
Get(context.Context, string) (GoodsItem, error)
|
||||
GetByCart(context.Context, int64) (GoodsItem, error)
|
||||
|
||||
UpsertMany(context.Context, ...GoodsItem) ([]GoodsItem, error)
|
||||
}
|
||||
|
||||
type CategoryRepository interface {
|
||||
List(context.Context) ([]Category, error)
|
||||
Get(context.Context, int64) (Category, error)
|
||||
|
||||
Create(ctx context.Context, name string) (Category, error)
|
||||
}
|
||||
|
||||
type Mapper interface {
|
||||
CategoryNameToID(context.Context, string) (int64, error)
|
||||
}
|
||||
Reference in New Issue
Block a user