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:
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