cart

package module
v0.0.0-...-119882a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2019 License: MIT Imports: 11 Imported by: 0

README

Cart

Photo

©Roberto Badillo

Go Report Card GoDoc

Why

Hey, people want to buy products.

Building this microservice to scale better.

How

Cut a piece from the monolith and serve chilled.

API documentation

Have to be generated. So I have generated a client with GoDoc. Client documentation

Package structure

The package structure is simple for a reason. Currently, this is a straightforward service, so almost everything is in a single package, where business logic, data storage, and API code is located in separate files. Later if service will become more complex, there will be a need for better granularity and code isolation. But for now, I feel like this is the right balance.

Future structure

In the future packages structure will be derived from domains, and not from the functionality that code provides. An example would be:

  • user
  • billing
  • auth

Not:

  • controller
  • service
  • dao

@rakyll Style guideline for Go packages

Other microservices

Carts rely on other microservices.

  • AuthZ: to check that the caller is allowed to access/modify Cart state.
  • Billing: to get prices.
  • Business Analytics: to submit Cart updates details and make conclusions.
  • Products: to get product details.

Some microservices rely on Cart.

  • Orders: to retrieve Cart state.
  • Users: to retrieve Cart(s) for User profile.
Testing

To run all tests: go test github.com/cooldryplace/cart/....

To skip integration tests: go test --short github.com/cooldryplace/cart/....

Missing Parts

The current integration test does not cover cases when storage or other dependency fails.

Metrics

Currently, only API and client metrics are implemented. This already allows measuring availability and latency. In the future, I can add more specific metrics that will not be used in SLO implementations but will be in dashboards. To pinpoint the root cause of a problem during incidents. So we alert based on SLIs, look at the dashboard, and know where and why it happens.

Tracing

Adding spans here and there will help to identify bottlenecks. I use Opencensus with Stackdriver exporter for this.

Limitations

The current DB schema does not allow us to shard data. FKs and autoincrement are in the way. The suggested next step is to handle constraints in the application code. Start using UUIDs.

Documentation

Overview

Package cart provides shopping cart functionality. It exposes gRPC API and uses SQL DB as storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cart

type Cart struct {
	ID        int64
	UserID    int64
	Items     []LineItem
	CreatedAt time.Time
	UpdatedAt time.Time
}

Cart holds LineItems for User.

type Carts

type Carts struct {
	// contains filtered or unexported fields
}

Carts contains all business logic realated to this microservice.

func New

func New(s storage) *Carts

New builds and returns new instance of Carts that is ready for use.

func (*Carts) AddProduct

func (c *Carts) AddProduct(ctx context.Context, cartID, productID int64, quantity uint32) error

AddProduct to a Cart.

func (*Carts) Cart

func (c *Carts) Cart(ctx context.Context, id int64) (Cart, error)

Cart returns Cart with provided ID.

func (*Carts) Create

func (c *Carts) Create(ctx context.Context, userID int64) (Cart, error)

Create Cart for a User.

func (*Carts) Delete

func (c *Carts) Delete(ctx context.Context, cartID int64) error

Delete Cart by ID.

func (*Carts) DeleteProduct

func (c *Carts) DeleteProduct(ctx context.Context, cartID, productID int64) error

DeleteProduct in a Cart.

func (*Carts) Empty

func (c *Carts) Empty(ctx context.Context, cartID int64) error

Empty Cart removes all previously added items.

type LineItem

type LineItem struct {
	ProductID int64
	Quantity  uint32
	CreatedAt time.Time
	UpdatedAt time.Time
}

LineItem represents single SKU and quantity.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server implements protobuf Carts service.

func NewServer

func NewServer(c *Carts) *Server

NewServer returns Carts gRPC server.

func (*Server) AddProduct

func (s *Server) AddProduct(ctx context.Context, req *proto.AddProductRequest) (*empty.Empty, error)

AddProduct to a Cart.

func (*Server) CreateCart

func (s *Server) CreateCart(ctx context.Context, req *proto.CartCreateRequest) (*proto.CartResponse, error)

CreateCart for a User.

func (*Server) DelProduct

func (s *Server) DelProduct(ctx context.Context, req *proto.DelProductRequest) (*empty.Empty, error)

DelProduct removes product from a Cart.

func (*Server) DeleteCart

func (s *Server) DeleteCart(ctx context.Context, req *proto.CartDeleteRequest) (*empty.Empty, error)

DeleteCart with the matching ID.

func (*Server) EmptyCart

func (s *Server) EmptyCart(ctx context.Context, req *proto.EmptyCartRequest) (*empty.Empty, error)

EmptyCart deletes all LineItems from a Cart.

func (*Server) GetCart

func (s *Server) GetCart(ctx context.Context, req *proto.CartRequest) (*proto.CartResponse, error)

GetCart returns current Cart state.

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

func NewStorage

func NewStorage(db *sql.DB) *Storage

func (*Storage) AddProduct

func (s *Storage) AddProduct(ctx context.Context, cartID, productID int64, quantity uint32) error

func (*Storage) CartByID

func (s *Storage) CartByID(ctx context.Context, id int64) (Cart, error)

func (*Storage) CreateCart

func (s *Storage) CreateCart(ctx context.Context, cart Cart) (Cart, error)

func (*Storage) DeleteCart

func (s *Storage) DeleteCart(ctx context.Context, cartID int64) error

func (*Storage) DeleteLineItems

func (s *Storage) DeleteLineItems(ctx context.Context, cartID int64) error

func (*Storage) DeleteProduct

func (s *Storage) DeleteProduct(ctx context.Context, cartID, productID int64) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL