mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 22:13:44 +03:00
[hw-3] loms service
This commit is contained in:
@@ -9,14 +9,19 @@ import (
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
"route256/cart/internal/app/server"
|
||||
"route256/cart/internal/domain/cart/repository"
|
||||
"route256/cart/internal/domain/cart/service"
|
||||
product_service "route256/cart/internal/domain/products/service"
|
||||
loms_service "route256/cart/internal/clients/loms"
|
||||
product_service "route256/cart/internal/clients/products"
|
||||
"route256/cart/internal/domain/repository"
|
||||
"route256/cart/internal/domain/service"
|
||||
"route256/cart/internal/infra/config"
|
||||
"route256/cart/internal/infra/http/middlewares"
|
||||
"route256/cart/internal/infra/http/round_trippers"
|
||||
|
||||
pbLOMS "route256/pkg/api/loms/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -39,9 +44,9 @@ func NewApp(configPath string) (*App, error) {
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
|
||||
if c.Service.LogLevel != "" {
|
||||
level, err := zerolog.ParseLevel(c.Service.LogLevel)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unknown log level `%s` provided: %w", c.Service.LogLevel, err)
|
||||
level, logErr := zerolog.ParseLevel(c.Service.LogLevel)
|
||||
if logErr != nil {
|
||||
return nil, fmt.Errorf("unknown log level `%s` provided: %w", c.Service.LogLevel, logErr)
|
||||
}
|
||||
|
||||
zerolog.SetGlobalLevel(level)
|
||||
@@ -52,7 +57,13 @@ func NewApp(configPath string) (*App, error) {
|
||||
app := &App{
|
||||
config: c,
|
||||
}
|
||||
app.server.Handler = app.BootstrapHandlers(app.setupCartService())
|
||||
|
||||
service, err := app.setupCartService()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
app.server.Handler = app.BootstrapHandlers(service)
|
||||
|
||||
return app, nil
|
||||
}
|
||||
@@ -70,7 +81,8 @@ func (app *App) ListenAndServe() error {
|
||||
return app.server.Serve(l)
|
||||
}
|
||||
|
||||
func (app *App) setupCartService() *service.CartService {
|
||||
func (app *App) setupCartService() (*service.CartService, error) {
|
||||
// Product service client
|
||||
transport := http.DefaultTransport
|
||||
transport = round_trippers.NewLogRoundTripper(transport)
|
||||
transport = round_trippers.NewRetryRoundTripper(transport, productsRetryAttemptsDefault, productsInitialDelaySecDefault)
|
||||
@@ -86,10 +98,21 @@ func (app *App) setupCartService() *service.CartService {
|
||||
fmt.Sprintf("%s:%s", app.config.ProductService.Host, app.config.ProductService.Port),
|
||||
)
|
||||
|
||||
// LOMS service client
|
||||
conn, err := grpc.NewClient(
|
||||
fmt.Sprintf("%s:%s", app.config.LomsService.Host, app.config.LomsService.Port),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("grpc.NewClient: %w", err)
|
||||
}
|
||||
|
||||
lomsClient := pbLOMS.NewLOMSClient(conn)
|
||||
lomsService := loms_service.NewLomsService(lomsClient)
|
||||
|
||||
const userCartCap = 100
|
||||
cartRepository := repository.NewInMemoryRepository(userCartCap)
|
||||
|
||||
return service.NewCartService(cartRepository, productService)
|
||||
return service.NewCartService(cartRepository, productService, lomsService), nil
|
||||
}
|
||||
|
||||
func (app *App) BootstrapHandlers(cartService *service.CartService) http.Handler {
|
||||
@@ -97,6 +120,7 @@ func (app *App) BootstrapHandlers(cartService *service.CartService) http.Handler
|
||||
|
||||
mx := http.NewServeMux()
|
||||
mx.HandleFunc("POST /user/{user_id}/cart/{sku_id}", s.AddItemHandler)
|
||||
mx.HandleFunc("POST /checkout/{user_id}", s.CheckoutHandler)
|
||||
mx.HandleFunc("GET /user/{user_id}/cart", s.GetItemsByUserIDHandler)
|
||||
mx.HandleFunc("DELETE /user/{user_id}/cart/{sku_id}", s.DeleteItemHandler)
|
||||
mx.HandleFunc("DELETE /user/{user_id}/cart", s.DeleteItemsByUserIDHandler)
|
||||
|
||||
@@ -13,12 +13,12 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type CreateReviewRequest struct {
|
||||
type AddItemRequest struct {
|
||||
Count uint32 `json:"count" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
func (s *Server) AddItemHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var request CreateReviewRequest
|
||||
var request AddItemRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
makeErrorResponse(w, err, http.StatusBadRequest)
|
||||
|
||||
@@ -79,7 +79,7 @@ func (s *Server) AddItemHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if err := s.cartService.AddItem(r.Context(), uid, item); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, model.ErrProductNotFound):
|
||||
case errors.Is(err, model.ErrProductNotFound), errors.Is(err, model.ErrNotEnoughStocks):
|
||||
makeErrorResponse(w, err, http.StatusPreconditionFailed)
|
||||
|
||||
log.Trace().Err(err).Msgf("product does not exist")
|
||||
|
||||
58
cart/internal/app/server/checkout_handler.go
Normal file
58
cart/internal/app/server/checkout_handler.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"route256/cart/internal/domain/entity"
|
||||
"route256/cart/internal/domain/model"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type CheckoutResponse struct {
|
||||
OrderID int64 `json:"order_id"`
|
||||
}
|
||||
|
||||
func (s *Server) CheckoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
strUserID := r.PathValue("user_id")
|
||||
userID, err := strconv.ParseInt(strUserID, 10, 64)
|
||||
if err != nil || userID <= 0 {
|
||||
if err == nil {
|
||||
err = fmt.Errorf("user_id must be greater than 0")
|
||||
}
|
||||
|
||||
makeErrorResponse(w, err, http.StatusBadRequest)
|
||||
|
||||
log.Trace().Err(err).Msgf("user_id=`%s`", strUserID)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
orderID, err := s.cartService.CheckoutUserCart(r.Context(), entity.UID(userID))
|
||||
switch {
|
||||
case errors.Is(err, model.ErrCartNotFound):
|
||||
makeErrorResponse(w, err, http.StatusNotFound)
|
||||
|
||||
return
|
||||
case err != nil:
|
||||
makeErrorResponse(w, err, http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
resp := CheckoutResponse{
|
||||
OrderID: orderID,
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
makeErrorResponse(w, err, http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ type CartService interface {
|
||||
GetItemsByUserID(ctx context.Context, userID entity.UID) (*model.Cart, error)
|
||||
DeleteItem(ctx context.Context, userID entity.UID, sku entity.Sku) error
|
||||
DeleteItemsByUserID(ctx context.Context, userID entity.UID) error
|
||||
CheckoutUserCart(ctx context.Context, userID entity.UID) (int64, error)
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
|
||||
Reference in New Issue
Block a user