mirror of
https://github.com/hexolan/stocklet.git
synced 2026-03-26 11:41:18 +00:00
chore: initial commit
This commit is contained in:
39
internal/svc/product/api/gateway.go
Normal file
39
internal/svc/product/api/gateway.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/product/v1"
|
||||
"github.com/hexolan/stocklet/internal/pkg/serve"
|
||||
"github.com/hexolan/stocklet/internal/svc/product"
|
||||
)
|
||||
|
||||
func PrepareGateway(cfg *product.ServiceConfig) *runtime.ServeMux {
|
||||
mux, clientOpts := serve.NewGatewayServeBase(&cfg.Shared)
|
||||
|
||||
ctx := context.Background()
|
||||
err := pb.RegisterProductServiceHandlerFromEndpoint(ctx, mux, serve.GetAddrToGrpc("localhost"), clientOpts)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("failed to register endpoint for gateway")
|
||||
}
|
||||
|
||||
return mux
|
||||
}
|
||||
30
internal/svc/product/api/grpc.go
Normal file
30
internal/svc/product/api/grpc.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/product/v1"
|
||||
"github.com/hexolan/stocklet/internal/pkg/serve"
|
||||
"github.com/hexolan/stocklet/internal/svc/product"
|
||||
)
|
||||
|
||||
func PrepareGrpc(cfg *product.ServiceConfig, svc *product.ProductService) *grpc.Server {
|
||||
svr := serve.NewGrpcServeBase(&cfg.Shared)
|
||||
pb.RegisterProductServiceServer(svr, svc)
|
||||
return svr
|
||||
}
|
||||
42
internal/svc/product/config.go
Normal file
42
internal/svc/product/config.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package product
|
||||
|
||||
import (
|
||||
"github.com/hexolan/stocklet/internal/pkg/config"
|
||||
)
|
||||
|
||||
// Order Service Configuration
|
||||
type ServiceConfig struct {
|
||||
// Core Configuration
|
||||
Shared config.SharedConfig
|
||||
|
||||
// Dynamically loaded configuration
|
||||
Postgres config.PostgresConfig
|
||||
Kafka config.KafkaConfig
|
||||
}
|
||||
|
||||
// load the base service configuration
|
||||
func NewServiceConfig() (*ServiceConfig, error) {
|
||||
cfg := ServiceConfig{}
|
||||
|
||||
// Load the core configuration
|
||||
if err := cfg.Shared.Load(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
114
internal/svc/product/controller/kafka.go
Normal file
114
internal/svc/product/controller/kafka.go
Normal file
@@ -0,0 +1,114 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/twmb/franz-go/pkg/kgo"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
eventpb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/product/v1"
|
||||
"github.com/hexolan/stocklet/internal/svc/product"
|
||||
)
|
||||
|
||||
type kafkaController struct {
|
||||
cl *kgo.Client
|
||||
|
||||
svc pb.ProductServiceServer
|
||||
|
||||
ctx context.Context
|
||||
ctxCancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewKafkaController(cl *kgo.Client) product.ConsumerController {
|
||||
// Create a cancellable context for the consumer
|
||||
ctx, ctxCancel := context.WithCancel(context.Background())
|
||||
|
||||
// Ensure the required Kafka topics exist
|
||||
err := messaging.EnsureKafkaTopics(
|
||||
cl,
|
||||
|
||||
messaging.Product_State_Created_Topic,
|
||||
messaging.Product_State_Deleted_Topic,
|
||||
messaging.Product_Attribute_Price_Topic,
|
||||
messaging.Product_PriceQuotation_Topic,
|
||||
|
||||
messaging.Order_State_Created_Topic,
|
||||
)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("kafka: raised attempting to ensure svc topics")
|
||||
}
|
||||
|
||||
// Add the consumption topics
|
||||
cl.AddConsumeTopics(
|
||||
messaging.Order_State_Created_Topic,
|
||||
)
|
||||
|
||||
return &kafkaController{cl: cl, ctx: ctx, ctxCancel: ctxCancel}
|
||||
}
|
||||
|
||||
func (c *kafkaController) Attach(svc pb.ProductServiceServer) {
|
||||
c.svc = svc
|
||||
}
|
||||
|
||||
func (c *kafkaController) Start() {
|
||||
if c.svc == nil {
|
||||
log.Panic().Msg("consumer: no service interface attached")
|
||||
}
|
||||
|
||||
for {
|
||||
fetches := c.cl.PollFetches(c.ctx)
|
||||
if errs := fetches.Errors(); len(errs) > 0 {
|
||||
log.Panic().Any("kafka-errs", errs).Msg("consumer: unrecoverable kafka errors")
|
||||
}
|
||||
|
||||
fetches.EachTopic(func(ft kgo.FetchTopic) {
|
||||
switch ft.Topic {
|
||||
case messaging.Order_State_Created_Topic:
|
||||
c.consumeOrderCreatedEventTopic(ft)
|
||||
default:
|
||||
log.Warn().Str("topic", ft.Topic).Msg("consumer: recieved records from unexpected topic")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *kafkaController) Stop() {
|
||||
// Cancel the consumer context
|
||||
c.ctxCancel()
|
||||
}
|
||||
|
||||
func (c *kafkaController) consumeOrderCreatedEventTopic(ft kgo.FetchTopic) {
|
||||
log.Info().Str("topic", ft.Topic).Msg("consumer: recieved records from topic")
|
||||
|
||||
// Process each message from the topic
|
||||
ft.EachRecord(func(record *kgo.Record) {
|
||||
// Unmarshal the event
|
||||
var event eventpb.OrderCreatedEvent
|
||||
err := proto.Unmarshal(record.Value, &event)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("consumer: failed to unmarshal event")
|
||||
}
|
||||
|
||||
// Process the event
|
||||
ctx := context.Background()
|
||||
c.svc.ProcessOrderCreatedEvent(ctx, &event)
|
||||
})
|
||||
}
|
||||
294
internal/svc/product/controller/postgres.go
Normal file
294
internal/svc/product/controller/postgres.go
Normal file
@@ -0,0 +1,294 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"golang.org/x/exp/maps"
|
||||
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/errors"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/product/v1"
|
||||
"github.com/hexolan/stocklet/internal/svc/product"
|
||||
)
|
||||
|
||||
const pgProductBaseQuery string = "SELECT id, name, description, price, created_at, updated_at FROM products"
|
||||
|
||||
type postgresController struct {
|
||||
cl *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPostgresController(cl *pgxpool.Pool) product.StorageController {
|
||||
return postgresController{cl: cl}
|
||||
}
|
||||
|
||||
func (c postgresController) GetProduct(ctx context.Context, productId string) (*pb.Product, error) {
|
||||
return c.getProduct(ctx, nil, productId)
|
||||
}
|
||||
|
||||
func (c postgresController) getProduct(ctx context.Context, tx *pgx.Tx, productId string) (*pb.Product, error) {
|
||||
// Determine if a db transaction is being used
|
||||
var row pgx.Row
|
||||
const query = pgProductBaseQuery + " WHERE id=$1"
|
||||
if tx == nil {
|
||||
row = c.cl.QueryRow(ctx, query, productId)
|
||||
} else {
|
||||
row = (*tx).QueryRow(ctx, query, productId)
|
||||
}
|
||||
|
||||
// Scan row to protobuf obj
|
||||
product, err := scanRowToProduct(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return product, nil
|
||||
}
|
||||
|
||||
// todo: implementing pagination mechanism
|
||||
func (c postgresController) GetProducts(ctx context.Context) ([]*pb.Product, error) {
|
||||
rows, err := c.cl.Query(ctx, pgProductBaseQuery+" LIMIT 10")
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeService, "query error", err)
|
||||
}
|
||||
|
||||
products := []*pb.Product{}
|
||||
for rows.Next() {
|
||||
productObj, err := scanRowToProduct(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
products = append(products, productObj)
|
||||
}
|
||||
|
||||
if rows.Err() != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeService, "error whilst scanning rows", rows.Err())
|
||||
}
|
||||
|
||||
return products, nil
|
||||
}
|
||||
|
||||
// Update a product price.
|
||||
func (c postgresController) UpdateProductPrice(ctx context.Context, productId string, price float32) (*pb.Product, error) {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Update product price
|
||||
_, err = tx.Exec(ctx, "UPDATE products SET price=$1 WHERE id=$2", price, productId)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to update product price", err)
|
||||
}
|
||||
|
||||
// Get updated product
|
||||
productObj, err := c.getProduct(ctx, &tx, productId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := product.PrepareProductPriceUpdatedEvent(productObj)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", productObj.Id, evtTopic, evt)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return productObj, nil
|
||||
}
|
||||
|
||||
// Delete a product by its specified id.
|
||||
func (c postgresController) DeleteProduct(ctx context.Context, productId string) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Get product
|
||||
productObj, err := c.getProduct(ctx, &tx, productId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete product
|
||||
_, err = tx.Exec(ctx, "DELETE FROM products WHERE id=$1", productId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to delete product", err)
|
||||
}
|
||||
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := product.PrepareProductDeletedEvent(productObj)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", productObj.Id, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) PriceOrderProducts(ctx context.Context, orderId string, customerId string, productQuantities map[string]int32) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Get prices of all specified products (in productQuantities)
|
||||
productIds := maps.Keys(productQuantities)
|
||||
statement, args, err := goqu.Dialect("postgres").From("products").Select("id", "price").Where(goqu.C("id").In(productIds)).Prepared(true).ToSQL()
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to build statement", err)
|
||||
}
|
||||
|
||||
rows, err := tx.Query(ctx, statement, args...)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to fetch price quotes", err)
|
||||
}
|
||||
|
||||
var productPrices map[string]float32
|
||||
for rows.Next() {
|
||||
var productId string
|
||||
var productPrice float32
|
||||
err := rows.Scan(&productId, &productPrice)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeNotFound, "failed to fetch price quotes: error whilst scanning row", err)
|
||||
}
|
||||
|
||||
productPrices[productId] = productPrice
|
||||
}
|
||||
|
||||
if rows.Err() != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to fetch price quotes: error whilst scanning rows", rows.Err())
|
||||
}
|
||||
|
||||
// Calculate total price
|
||||
// Also ensuring that all items in the itemQuantities have a fetched price
|
||||
var totalPrice float32
|
||||
for productId, quantity := range productQuantities {
|
||||
productPrice, ok := productPrices[productId]
|
||||
if !ok {
|
||||
// Prepare and dispatch failure product pricing event
|
||||
evt, evtTopic, err := product.PrepareProductPriceQuoteEvent_Unavaliable(orderId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = c.cl.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", orderId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Add to total price
|
||||
totalPrice += productPrice * float32(quantity)
|
||||
}
|
||||
|
||||
// Prepare and dispatch succesful product pricing event
|
||||
evt, evtTopic, err := product.PrepareProductPriceQuoteEvent_Avaliable(
|
||||
orderId,
|
||||
productQuantities,
|
||||
productPrices,
|
||||
totalPrice,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", orderId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Scan a postgres row to a protobuf object
|
||||
func scanRowToProduct(row pgx.Row) (*pb.Product, error) {
|
||||
var productObj pb.Product
|
||||
|
||||
// Temporary variables that require conversion
|
||||
var tmpCreatedAt pgtype.Timestamp
|
||||
var tmpUpdatedAt pgtype.Timestamp
|
||||
|
||||
err := row.Scan(
|
||||
&productObj.Id,
|
||||
&productObj.Name,
|
||||
&productObj.Description,
|
||||
&productObj.Price,
|
||||
&tmpCreatedAt,
|
||||
&tmpUpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeNotFound, "product not found", err)
|
||||
} else {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to scan object from database", err)
|
||||
}
|
||||
}
|
||||
|
||||
// convert postgres timestamps to unix format
|
||||
if tmpCreatedAt.Valid {
|
||||
productObj.CreatedAt = tmpCreatedAt.Time.Unix()
|
||||
} else {
|
||||
return nil, errors.NewServiceError(errors.ErrCodeUnknown, "failed to scan object from database (timestamp conversion)")
|
||||
}
|
||||
|
||||
if tmpUpdatedAt.Valid {
|
||||
unixUpdated := tmpUpdatedAt.Time.Unix()
|
||||
productObj.UpdatedAt = &unixUpdated
|
||||
}
|
||||
|
||||
return &productObj, nil
|
||||
}
|
||||
86
internal/svc/product/event.go
Normal file
86
internal/svc/product/event.go
Normal file
@@ -0,0 +1,86 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package product
|
||||
|
||||
import (
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
eventspb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/product/v1"
|
||||
)
|
||||
|
||||
func PrepareProductCreatedEvent(product *pb.Product) ([]byte, string, error) {
|
||||
topic := messaging.Product_State_Created_Topic
|
||||
event := &eventspb.ProductCreatedEvent{
|
||||
Revision: 1,
|
||||
|
||||
ProductId: product.Id,
|
||||
Name: product.Name,
|
||||
Description: product.Description,
|
||||
Price: product.Price,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareProductPriceUpdatedEvent(product *pb.Product) ([]byte, string, error) {
|
||||
topic := messaging.Product_Attribute_Price_Topic
|
||||
event := &eventspb.ProductPriceUpdatedEvent{
|
||||
Revision: 1,
|
||||
|
||||
ProductId: product.Id,
|
||||
Price: product.Price,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareProductDeletedEvent(product *pb.Product) ([]byte, string, error) {
|
||||
topic := messaging.Product_State_Deleted_Topic
|
||||
event := &eventspb.ProductDeletedEvent{
|
||||
Revision: 1,
|
||||
|
||||
ProductId: product.Id,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareProductPriceQuoteEvent_Avaliable(orderId string, productQuantities map[string]int32, productPrices map[string]float32, totalPrice float32) ([]byte, string, error) {
|
||||
topic := messaging.Product_PriceQuotation_Topic
|
||||
event := &eventspb.ProductPriceQuoteEvent{
|
||||
Revision: 1,
|
||||
|
||||
Type: eventspb.ProductPriceQuoteEvent_TYPE_AVALIABLE,
|
||||
OrderId: orderId,
|
||||
ProductQuantities: productQuantities,
|
||||
ProductPrices: productPrices,
|
||||
TotalPrice: totalPrice,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareProductPriceQuoteEvent_Unavaliable(orderId string) ([]byte, string, error) {
|
||||
topic := messaging.Product_PriceQuotation_Topic
|
||||
event := &eventspb.ProductPriceQuoteEvent{
|
||||
Revision: 1,
|
||||
|
||||
Type: eventspb.ProductPriceQuoteEvent_TYPE_UNAVALIABLE,
|
||||
OrderId: orderId,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
116
internal/svc/product/product.go
Normal file
116
internal/svc/product/product.go
Normal file
@@ -0,0 +1,116 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package product
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/bufbuild/protovalidate-go"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/errors"
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
commonpb "github.com/hexolan/stocklet/internal/pkg/protogen/common/v1"
|
||||
eventpb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/product/v1"
|
||||
)
|
||||
|
||||
// Interface for the service
|
||||
type ProductService struct {
|
||||
pb.UnimplementedProductServiceServer
|
||||
|
||||
store StorageController
|
||||
pbVal *protovalidate.Validator
|
||||
}
|
||||
|
||||
// Interface for database methods
|
||||
// Flexibility for implementing seperate controllers for different databases (e.g. Postgres, MongoDB, etc)
|
||||
type StorageController interface {
|
||||
GetProduct(ctx context.Context, productId string) (*pb.Product, error)
|
||||
GetProducts(ctx context.Context) ([]*pb.Product, error)
|
||||
|
||||
UpdateProductPrice(ctx context.Context, productId string, price float32) (*pb.Product, error)
|
||||
DeleteProduct(ctx context.Context, productId string) error
|
||||
|
||||
PriceOrderProducts(ctx context.Context, orderId string, customerId string, productQuantities map[string]int32) error
|
||||
}
|
||||
|
||||
// Interface for event consumption
|
||||
// Flexibility for seperate controllers for different messaging systems (e.g. Kafka, NATS, etc)
|
||||
type ConsumerController interface {
|
||||
messaging.ConsumerController
|
||||
|
||||
Attach(svc pb.ProductServiceServer)
|
||||
}
|
||||
|
||||
// Create the product service
|
||||
func NewProductService(cfg *ServiceConfig, store StorageController) *ProductService {
|
||||
// Initialise the protobuf validator
|
||||
pbVal, err := protovalidate.New()
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("failed to initialise protobuf validator")
|
||||
}
|
||||
|
||||
// Initialise the service
|
||||
return &ProductService{
|
||||
store: store,
|
||||
pbVal: pbVal,
|
||||
}
|
||||
}
|
||||
|
||||
func (svc ProductService) ServiceInfo(ctx context.Context, req *commonpb.ServiceInfoRequest) (*commonpb.ServiceInfoResponse, error) {
|
||||
return &commonpb.ServiceInfoResponse{
|
||||
Name: "product",
|
||||
Source: "https://github.com/hexolan/stocklet",
|
||||
SourceLicense: "AGPL-3.0",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (svc ProductService) ViewProduct(ctx context.Context, req *pb.ViewProductRequest) (*pb.ViewProductResponse, error) {
|
||||
// Validate the request args
|
||||
if err := svc.pbVal.Validate(req); err != nil {
|
||||
// Provide the validation error to the user.
|
||||
return nil, errors.NewServiceError(errors.ErrCodeInvalidArgument, "invalid request: "+err.Error())
|
||||
}
|
||||
|
||||
// Get product from DB
|
||||
product, err := svc.store.GetProduct(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.ViewProductResponse{Product: product}, nil
|
||||
}
|
||||
|
||||
func (svc ProductService) ViewProducts(ctx context.Context, req *pb.ViewProductsRequest) (*pb.ViewProductsResponse, error) {
|
||||
// todo: pagination mechanism
|
||||
products, err := svc.store.GetProducts(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.ViewProductsResponse{Products: products}, nil
|
||||
}
|
||||
|
||||
func (svc ProductService) ProcessOrderCreatedEvent(ctx context.Context, req *eventpb.OrderCreatedEvent) (*emptypb.Empty, error) {
|
||||
err := svc.store.PriceOrderProducts(ctx, req.OrderId, req.CustomerId, req.ItemQuantities)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user