mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 14:03:45 +03:00
[hw-1] implement cart service
This commit is contained in:
104
cart/internal/app/app.go
Normal file
104
cart/internal/app/app.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"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"
|
||||
"route256/cart/internal/infra/config"
|
||||
"route256/cart/internal/infra/http/middlewares"
|
||||
"route256/cart/internal/infra/http/round_trippers"
|
||||
)
|
||||
|
||||
const (
|
||||
productsRetryAttemptsDefault = 3
|
||||
productsInitialDelaySecDefault = 1
|
||||
)
|
||||
|
||||
type App struct {
|
||||
config *config.Config
|
||||
server http.Server
|
||||
}
|
||||
|
||||
func NewApp(configPath string) (*App, error) {
|
||||
c, err := config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to load config: %w", err)
|
||||
}
|
||||
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
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)
|
||||
}
|
||||
|
||||
zerolog.SetGlobalLevel(level)
|
||||
}
|
||||
|
||||
log.WithLevel(zerolog.GlobalLevel()).Msgf("using logging level=`%s`", zerolog.GlobalLevel().String())
|
||||
|
||||
app := &App{
|
||||
config: c,
|
||||
}
|
||||
app.server.Handler = app.bootstrapHandlers()
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func (app *App) ListenAndServe() error {
|
||||
address := fmt.Sprintf("%s:%s", app.config.Service.Host, app.config.Service.Port)
|
||||
|
||||
l, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info().Msgf("Serving cart at http://%s", l.Addr())
|
||||
|
||||
return app.server.Serve(l)
|
||||
}
|
||||
|
||||
func (app *App) bootstrapHandlers() http.Handler {
|
||||
transport := http.DefaultTransport
|
||||
transport = round_trippers.NewLogRoundTripper(transport)
|
||||
transport = round_trippers.NewRetryRoundTripper(transport, productsRetryAttemptsDefault, productsInitialDelaySecDefault)
|
||||
|
||||
httpClient := http.Client{
|
||||
Transport: transport,
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
productService := product_service.NewProductService(
|
||||
httpClient,
|
||||
app.config.ProductService.Token,
|
||||
fmt.Sprintf("%s:%s", app.config.ProductService.Host, app.config.ProductService.Port),
|
||||
)
|
||||
|
||||
const userCartCap = 100
|
||||
cartRepository := repository.NewInMemoryRepository(userCartCap)
|
||||
cartService := service.NewCartService(cartRepository, productService)
|
||||
|
||||
s := server.NewServer(cartService)
|
||||
|
||||
mx := http.NewServeMux()
|
||||
mx.HandleFunc("POST /user/{user_id}/cart/{sku_id}", s.AddItemHandler)
|
||||
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)
|
||||
|
||||
h := middlewares.NewTimerMiddleware(mx)
|
||||
|
||||
return h
|
||||
}
|
||||
96
cart/internal/app/server/add_item_handler.go
Normal file
96
cart/internal/app/server/add_item_handler.go
Normal file
@@ -0,0 +1,96 @@
|
||||
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 CreateReviewRequest struct {
|
||||
Count uint32 `json:"count" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
func (s *Server) AddItemHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var request CreateReviewRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
makeErrorResponse(w, err, http.StatusBadRequest)
|
||||
|
||||
log.Trace().Err(err).Msg("failed decoding request")
|
||||
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
if request.Count <= 0 {
|
||||
err := fmt.Errorf("count must be greater than 0")
|
||||
|
||||
makeErrorResponse(w, err, http.StatusBadRequest)
|
||||
|
||||
log.Trace().Err(err).Msg("body validation failed")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
strSku := r.PathValue("sku_id")
|
||||
sku, err := strconv.ParseInt(strSku, 10, 64)
|
||||
if err != nil || sku <= 0 {
|
||||
if err == nil {
|
||||
err = fmt.Errorf("sku_id must be greater than 0")
|
||||
}
|
||||
|
||||
makeErrorResponse(w, err, http.StatusBadRequest)
|
||||
|
||||
log.Trace().Err(err).Msgf("sku_id=`%s`", strUserID)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
item := &model.Item{
|
||||
Product: &model.Product{
|
||||
Name: "",
|
||||
Price: 0,
|
||||
Sku: entity.Sku(sku),
|
||||
},
|
||||
Count: request.Count,
|
||||
}
|
||||
|
||||
uid := entity.UID(userID)
|
||||
|
||||
if err := s.cartService.AddItem(r.Context(), uid, item); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, model.ErrProductNotFound):
|
||||
makeErrorResponse(w, err, http.StatusPreconditionFailed)
|
||||
|
||||
log.Trace().Err(err).Msgf("product does not exist")
|
||||
default:
|
||||
makeErrorResponse(w, err, http.StatusInternalServerError)
|
||||
|
||||
log.Trace().Err(err).Msgf("unknown error")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
37
cart/internal/app/server/delete_cart_handler.go
Normal file
37
cart/internal/app/server/delete_cart_handler.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"route256/cart/internal/domain/entity"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s *Server) DeleteItemsByUserIDHandler(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
|
||||
}
|
||||
|
||||
if err := s.cartService.DeleteItemsByUserID(r.Context(), entity.UID(userID)); err != nil {
|
||||
makeErrorResponse(w, err, http.StatusBadRequest)
|
||||
|
||||
log.Trace().Err(err).Msgf("cartService.DeleteItemFromCart failed for uid=%d: %d", userID, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
55
cart/internal/app/server/delete_item_handler.go
Normal file
55
cart/internal/app/server/delete_item_handler.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"route256/cart/internal/domain/entity"
|
||||
"route256/cart/internal/domain/model"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s *Server) DeleteItemHandler(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
|
||||
}
|
||||
|
||||
strSku := r.PathValue("sku_id")
|
||||
sku, err := strconv.ParseInt(strSku, 10, 64)
|
||||
if err != nil || sku <= 0 {
|
||||
if err == nil {
|
||||
err = fmt.Errorf("sku_id must be greater than 0")
|
||||
}
|
||||
|
||||
makeErrorResponse(w, err, http.StatusBadRequest)
|
||||
|
||||
log.Trace().Err(err).Msgf("sku_id=`%s`", strUserID)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = s.cartService.DeleteItem(r.Context(), entity.UID(userID), entity.Sku(sku))
|
||||
switch {
|
||||
case errors.Is(err, model.ErrCartNotFound), errors.Is(err, model.ErrItemNotFoundInCart), err == nil:
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
default:
|
||||
makeErrorResponse(w, err, http.StatusBadRequest)
|
||||
|
||||
log.Trace().Err(err).Msgf("cartService.DeleteItemFromCart failed for uid=%d sku=%d: %d", userID, sku, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
24
cart/internal/app/server/error_response.go
Normal file
24
cart/internal/app/server/error_response.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func makeErrorResponse(w http.ResponseWriter, err error, statusCode int) {
|
||||
type ErrorMessage struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
errResponse := &ErrorMessage{Message: err.Error()}
|
||||
if errE := json.NewEncoder(w).Encode(errResponse); errE != nil {
|
||||
log.Err(errE).Send()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
70
cart/internal/app/server/get_cart_handler.go
Normal file
70
cart/internal/app/server/get_cart_handler.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"route256/cart/internal/domain/entity"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type CartItem struct {
|
||||
Sku int64 `json:"sku"`
|
||||
Name string `json:"name"`
|
||||
Count uint32 `json:"count"`
|
||||
Price uint32 `json:"price"`
|
||||
}
|
||||
|
||||
type GetUserCartResponse struct {
|
||||
Items []CartItem `json:"items"`
|
||||
TotalPrice uint32 `json:"total_price"`
|
||||
}
|
||||
|
||||
func (s *Server) GetItemsByUserIDHandler(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
|
||||
}
|
||||
|
||||
cart, err := s.cartService.GetItemsByUserID(r.Context(), entity.UID(userID))
|
||||
if err != nil {
|
||||
makeErrorResponse(w, err, http.StatusNotFound)
|
||||
|
||||
log.Trace().Err(err).Msgf("cartService.GetItemsByUserID: %d", http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
response := GetUserCartResponse{
|
||||
Items: make([]CartItem, len(cart.Items)),
|
||||
TotalPrice: cart.TotalPrice,
|
||||
}
|
||||
for i, v := range cart.Items {
|
||||
response.Items[i] = CartItem{
|
||||
Sku: int64(v.Product.Sku),
|
||||
Name: v.Product.Name,
|
||||
Count: v.Count,
|
||||
Price: uint32(v.Product.Price),
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
makeErrorResponse(w, err, http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
25
cart/internal/app/server/server.go
Normal file
25
cart/internal/app/server/server.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"route256/cart/internal/domain/entity"
|
||||
"route256/cart/internal/domain/model"
|
||||
)
|
||||
|
||||
type CartService interface {
|
||||
AddItem(ctx context.Context, userID entity.UID, item *model.Item) error
|
||||
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
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
cartService CartService
|
||||
}
|
||||
|
||||
func NewServer(cartService CartService) *Server {
|
||||
return &Server{
|
||||
cartService: cartService,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user