diff --git a/cmd/api/main.go b/cmd/api/main.go index d2f6169..e8a8aaf 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -35,6 +35,9 @@ func main() { log.Fatalf("Error migrating database: %s", err) } + // Connect NATS + //natServer, err := nats.NewNATS() + s := handler.NewServer(db) // Initialize Validator diff --git a/go.mod b/go.mod index 30c29f8..be4d2ae 100644 --- a/go.mod +++ b/go.mod @@ -72,6 +72,9 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/morikuni/aec v1.0.0 // indirect + github.com/nats-io/nats.go v1.37.0 // indirect + github.com/nats-io/nkeys v0.4.7 // indirect + github.com/nats-io/nuid v1.0.1 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0 // indirect diff --git a/go.sum b/go.sum index e64dc40..e535b46 100644 --- a/go.sum +++ b/go.sum @@ -147,6 +147,12 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE= +github.com/nats-io/nats.go v1.37.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8= +github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI= +github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc= +github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= +github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= diff --git a/pkg/handler/cart/order.go b/pkg/handler/cart/order.go index 0298687..c3717ac 100644 --- a/pkg/handler/cart/order.go +++ b/pkg/handler/cart/order.go @@ -2,7 +2,6 @@ package cart import ( "context" - "fmt" "github.com/gin-gonic/gin" "net/http" "strconv" @@ -19,13 +18,19 @@ func (s *CartHandler) PlaceNewOrder(c *gin.Context) { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } - - order, err := s.service.PlaceOrder(ctx, cartInfo.CartID) + // Place a new order. + _, err = s.service.PlaceOrder(ctx, cartInfo.CartID, userID) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + // Remove all items from the cart after placing order. + err = s.service.RemoveItemsFromCart(ctx, cartInfo.CartID) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } - fmt.Println(order) + c.JSON(http.StatusCreated, gin.H{"message": "Order placed!"}) } diff --git a/pkg/nats/nats_server.go b/pkg/nats/nats_server.go new file mode 100644 index 0000000..ee9245a --- /dev/null +++ b/pkg/nats/nats_server.go @@ -0,0 +1,36 @@ +package nats + +import ( + "github.com/nats-io/nats.go" + "log" +) + +type NATS struct { + conn *nats.Conn +} + +func NewNATS() (*NATS, error) { + nc, err := nats.Connect(nats.DefaultURL, nats.Name("food-delivery-nats")) + if err != nil { + log.Fatalf("Error: %s", err) + } + return &NATS{conn: nc}, err +} + +func (n *NATS) Publish(channel string, message []byte) error { + err := n.conn.Publish(channel, message) + if err != nil { + return err + } + return nil +} + +func (n *NATS) Subscribe(channel string) error { + _, err := n.conn.Subscribe(channel, func(msg *nats.Msg) { + log.Printf("Received a message: %s", string(msg.Data)) + }) + if err != nil { + return err + } + return nil +} diff --git a/pkg/service/cart_order/place_order.go b/pkg/service/cart_order/place_order.go index c0dddab..ddc2621 100644 --- a/pkg/service/cart_order/place_order.go +++ b/pkg/service/cart_order/place_order.go @@ -8,7 +8,7 @@ import ( "errors" ) -func (cartSrv *CartService) PlaceOrder(ctx context.Context, cartId int64) (*order.Order, error) { +func (cartSrv *CartService) PlaceOrder(ctx context.Context, cartId int64, userId int64) (*order.Order, error) { var cartItems []cart.CartItems var newOrder order.Order var newOrderItems []order.OrderItems @@ -26,7 +26,7 @@ func (cartSrv *CartService) PlaceOrder(ctx context.Context, cartId int64) (*orde } // Creating a new order. - newOrder.UserID = 1 + newOrder.UserID = userId newOrder.OrderStatus = "pending" newOrder.TotalAmount = orderTotal newOrder.DeliveryAddress = "New Delhi" @@ -56,12 +56,16 @@ func (cartSrv *CartService) PlaceOrder(ctx context.Context, cartId int64) (*orde return nil, err } + return &newOrder, nil + +} + +func (cartSrv *CartService) RemoveItemsFromCart(ctx context.Context, cartId int64) error { //remove all items from the cart. filter := database.Filter{"cart_id": cartId} - - _, err = cartSrv.db.Delete(ctx, "cart_items", filter) + _, err := cartSrv.db.Delete(ctx, "cart_items", filter) if err != nil { - return nil, errors.New("failed to delete cart items") + return errors.New("failed to delete cart items") } - return nil, err + return nil }