Skip to content

rossi1/product-sorter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Product Sorting Service

This project implements a flexible and extensible sorting system for products using the Strategy Pattern in Go.

It allows A/B testing of different product sorting strategies — e.g. by price or by sales-per-view ratio — to determine which performs best on different parts of the website.

Features

  • Sort products by price or sales/view ratio
  • Easily extendable by registering new sort strategies
  • Fully compliant with the Open/Closed Principle
  • Strategy registry for dynamic resolution
  • Clean, idiomatic Go

Example Strategies

  • price: Sorts products by ascending price
  • sales_per_view: Sorts products by sales_count ÷ views_count in descending order

Sample Output

Sorted by Sales/View Ratio: ID: 2, Name: Zebra Table, Ratio: 0.0918, Price: $44.49 ID: 3, Name: Coffee Table, Ratio: 0.0520, Price: $10.00 ID: 1, Name: Alabaster Table, Ratio: 0.0438, Price: $12.99

Sorted by Price: ID: 3, Name: Coffee Table, Price: $10.00 ID: 1, Name: Alabaster Table, Price: $12.99 ID: 2, Name: Zebra Table, Price: $44.49

Usage

go run main.go

Testing

go test -v

Add New Strategy

  1. Implement SortStrategy interface
  2. Register it in the registry:
registry.SetStrategy("created_at", SortByCreatedAt{})
  1. Use it:
strategy, err := registry.GetStrategy("created_at")
if err != nil {
    fmt.Println(err)
    return
}
sortedProducts := strategy.Sort(products)

Usage (Makefile)

Build

make build

Run

make run

Test

make test

Docker Build

make docker-build

Docker Run

make docker-run

Clean

make clean

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors