Skip to content

refactor: flatten product catalog package #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions backend/cart/bounded_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (
"github.com/bkielbasa/go-ecommerce/backend/internal/application"
"github.com/bkielbasa/go-ecommerce/backend/internal/https"
"github.com/bkielbasa/go-ecommerce/backend/internal/observability"
pcApp "github.com/bkielbasa/go-ecommerce/backend/productcatalog/app"
pcDomain "github.com/bkielbasa/go-ecommerce/backend/productcatalog/domain"
"github.com/bkielbasa/go-ecommerce/backend/productcatalog"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)

func New(db *sql.DB, logger logrus.FieldLogger, pc pcApp.ProductService) application.BoundedContext {
func New(db *sql.DB, logger logrus.FieldLogger, pc productcatalog.ProductService) application.BoundedContext {
storage := adapter.NewPostgres(db)

return &boundedContext{
Expand All @@ -29,13 +28,13 @@ func New(db *sql.DB, logger logrus.FieldLogger, pc pcApp.ProductService) applica
// transformProductCatalog is part of Anti-Corruption Layer that prevents leaking
// productcatalog's types into the cart
type transformProductCatalog struct {
pc pcApp.ProductService
pc productcatalog.ProductService
}

func (tpc transformProductCatalog) Find(ctx context.Context, productID string) (domain.Product, error) {
p, err := tpc.pc.Find(ctx, productID)

if errors.Is(err, pcDomain.ErrProductNotFound) {
if errors.Is(err, productcatalog.ErrProductNotFound) {
return domain.Product{}, domain.ErrProductNotFound
}

Expand Down
7 changes: 3 additions & 4 deletions backend/cmd/cli/rootcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"fmt"
"os"

"github.com/bkielbasa/go-ecommerce/backend/productcatalog/adapter"
"github.com/bkielbasa/go-ecommerce/backend/productcatalog/app"
"github.com/bkielbasa/go-ecommerce/backend/productcatalog"
"github.com/spf13/cobra"
)

Expand All @@ -19,8 +18,8 @@ var rootCmd = &cobra.Command{
}

func Execute(db *sql.DB) {
storage := adapter.NewPostgres(db)
appServ := app.NewProductService(storage)
storage := productcatalog.NewPostgres(db)
appServ := productcatalog.NewProductService(storage)
rootCmd.AddCommand(newProductCatalogCmd(appServ))
rootCmd.AddCommand(newSeedsCmd(appServ))

Expand Down
54 changes: 27 additions & 27 deletions backend/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 additions & 27 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/port.product"
"$ref": "#/definitions/productcatalog.httpProduct"
}
}
},
Expand All @@ -222,17 +222,6 @@
}
},
"definitions": {
"cart_port.price": {
"type": "object",
"properties": {
"amount": {
"type": "number"
},
"currency": {
"type": "string"
}
}
},
"https.ErrorResponse": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -280,22 +269,13 @@
}
}
},
"port.product": {
"port.price": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"price": {
"$ref": "#/definitions/productcatalog_port.price"
"amount": {
"type": "number"
},
"thumbnail": {
"currency": {
"type": "string"
}
}
Expand All @@ -310,7 +290,7 @@
"type": "string"
},
"price": {
"$ref": "#/definitions/cart_port.price"
"$ref": "#/definitions/port.price"
},
"quantity": {
"type": "integer"
Expand All @@ -328,7 +308,27 @@
}
}
},
"productcatalog_port.price": {
"productcatalog.httpProduct": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"price": {
"$ref": "#/definitions/productcatalog.price"
},
"thumbnail": {
"type": "string"
}
}
},
"productcatalog.price": {
"type": "object",
"properties": {
"amount": {
Expand Down
40 changes: 20 additions & 20 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
basePath: /api/v1
definitions:
cart_port.price:
properties:
amount:
type: number
currency:
type: string
type: object
https.ErrorResponse:
properties:
detail:
Expand Down Expand Up @@ -37,17 +30,11 @@ definitions:
username:
type: string
type: object
port.product:
port.price:
properties:
description:
type: string
id:
type: string
name:
type: string
price:
$ref: '#/definitions/productcatalog_port.price'
thumbnail:
amount:
type: number
currency:
type: string
type: object
port.showCartItemResponse:
Expand All @@ -57,7 +44,7 @@ definitions:
name:
type: string
price:
$ref: '#/definitions/cart_port.price'
$ref: '#/definitions/port.price'
quantity:
type: integer
type: object
Expand All @@ -68,7 +55,20 @@ definitions:
$ref: '#/definitions/port.showCartItemResponse'
type: array
type: object
productcatalog_port.price:
productcatalog.httpProduct:
properties:
description:
type: string
id:
type: string
name:
type: string
price:
$ref: '#/definitions/productcatalog.price'
thumbnail:
type: string
type: object
productcatalog.price:
properties:
amount:
type: number
Expand Down Expand Up @@ -204,7 +204,7 @@ paths:
description: OK
schema:
items:
$ref: '#/definitions/port.product'
$ref: '#/definitions/productcatalog.httpProduct'
type: array
"404":
description: Not Found
Expand Down
38 changes: 0 additions & 38 deletions backend/productcatalog/adapter/inmemory.go

This file was deleted.

36 changes: 36 additions & 0 deletions backend/productcatalog/adapter_inmemory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package productcatalog

import (
"context"
)

type inMemory struct {
products []Product
}

func NewInMemory() *inMemory {
return &inMemory{}
}

func (im *inMemory) Add(ctx context.Context, p Product) error {
im.products = append(im.products, p)

return nil
}

func (im *inMemory) All(ctx context.Context) ([]Product, error) {
return im.products, nil
}

func (im *inMemory) Find(ctx context.Context, id string) (Product, error) {
for _, p := range im.products {
if string(p.ID()) == id {
return p, nil
}
}
return Product{}, ErrProductNotFound
}

func (im *inMemory) Reserve(ctx context.Context, name string) error {
return nil
}
Loading