-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from quangdangfit/cart
Cart
- Loading branch information
Showing
47 changed files
with
2,644 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ ignore: | |
- "pkg/" | ||
- "vendor/" | ||
- "proto/" | ||
- "internal/server" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dto | ||
|
||
type Cart struct { | ||
ID string `json:"id"` | ||
User *User `json:"user"` | ||
Lines []*CartLineReq `json:"lines"` | ||
} | ||
|
||
type CartLine struct { | ||
Product *Product `json:"product"` | ||
Quantity uint `json:"quantity" validate:"required"` | ||
} | ||
|
||
type CartLineReq struct { | ||
ProductID string `json:"product_id" validate:"required"` | ||
Quantity uint `json:"quantity" validate:"required"` | ||
} | ||
|
||
type AddProductReq struct { | ||
UserID string `json:"user_id" validate:"required"` | ||
Line *CartLineReq `json:"line" validate:"required,dive"` | ||
} | ||
|
||
type RemoveProductReq struct { | ||
UserID string `json:"user_id" validate:"required"` | ||
ProductID string `json:"product_id" validate:"required"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dto | ||
|
||
type Product struct { | ||
ID string `json:"id"` | ||
Code string `json:"code"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Price float64 `json:"price"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package dto | ||
|
||
type User struct { | ||
ID string `json:"id"` | ||
Email string `json:"email"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Cart struct { | ||
ID string `json:"id" gorm:"unique;not null;index;primary_key"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
DeletedAt *time.Time `json:"deleted_at" gorm:"index"` | ||
UserID string `json:"user_id" gorm:"unique;not null;index"` | ||
User *User | ||
Lines []*CartLine `json:"lines"` | ||
} | ||
|
||
type CartLine struct { | ||
ProductID string `json:"product_id"` | ||
Product *Product | ||
Quantity uint `json:"quantity"` | ||
} | ||
|
||
func (cart *Cart) BeforeCreate(tx *gorm.DB) error { | ||
cart.ID = uuid.New().String() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package model | ||
|
||
type Product struct { | ||
ID string `json:"id" gorm:"unique;not null;index;primary_key"` | ||
Code string `json:"code" gorm:"uniqueIndex:idx_product_code,not null"` | ||
Name string `json:"name" gorm:"uniqueIndex:idx_product_name,not null"` | ||
Description string `json:"description"` | ||
Price float64 `json:"price"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package model | ||
|
||
type User struct { | ||
ID string `json:"id" gorm:"unique;not null;index;primary_key"` | ||
Email string `json:"email" gorm:"unique;not null;index:idx_user_email"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/quangdangfit/gocommon/logger" | ||
|
||
"goshop/internal/cart/dto" | ||
"goshop/internal/cart/service" | ||
"goshop/pkg/utils" | ||
pb "goshop/proto/gen/go/cart" | ||
) | ||
|
||
type CartHandler struct { | ||
pb.UnimplementedCartServiceServer | ||
|
||
service service.ICartService | ||
} | ||
|
||
func NewCartHandler(service service.ICartService) *CartHandler { | ||
return &CartHandler{ | ||
service: service, | ||
} | ||
} | ||
|
||
func (h *CartHandler) AddProduct(ctx context.Context, req *pb.AddProductReq) (*pb.AddProductRes, error) { | ||
userID, _ := ctx.Value("userId").(string) | ||
if userID == "" { | ||
return nil, errors.New("unauthorized") | ||
} | ||
|
||
cart, err := h.service.AddProduct(ctx, &dto.AddProductReq{ | ||
UserID: userID, | ||
Line: &dto.CartLineReq{ | ||
ProductID: req.ProductId, | ||
Quantity: uint(req.Quantity), | ||
}, | ||
}) | ||
if err != nil { | ||
logger.Error("Failed to add product ", err) | ||
return nil, err | ||
} | ||
|
||
var res pb.AddProductRes | ||
utils.Copy(&res.Cart, &cart) | ||
return &res, nil | ||
} | ||
|
||
func (h *CartHandler) RemoveProduct(ctx context.Context, req *pb.RemoveProductReq) (*pb.RemoveProductRes, error) { | ||
userID, _ := ctx.Value("userId").(string) | ||
if userID == "" { | ||
return nil, errors.New("unauthorized") | ||
} | ||
|
||
cart, err := h.service.RemoveProduct(ctx, &dto.RemoveProductReq{ | ||
UserID: userID, | ||
ProductID: req.ProductId, | ||
}) | ||
if err != nil { | ||
logger.Error("Failed to remove product ", err) | ||
return nil, err | ||
} | ||
|
||
var res pb.RemoveProductRes | ||
utils.Copy(&res.Cart, &cart) | ||
return &res, nil | ||
} | ||
|
||
func (h *CartHandler) GetCart(ctx context.Context, req *pb.GetCartReq) (*pb.GetCartRes, error) { | ||
userID, _ := ctx.Value("userId").(string) | ||
if userID == "" { | ||
return nil, errors.New("unauthorized") | ||
} | ||
|
||
cart, err := h.service.GetCartByUserID(ctx, userID) | ||
if err != nil { | ||
logger.Error("Failed to get cart ", err) | ||
return nil, err | ||
} | ||
|
||
var res pb.GetCartRes | ||
utils.Copy(&res.Cart, &cart) | ||
return &res, nil | ||
} |
Oops, something went wrong.