Skip to content

Commit

Permalink
Add NATS integration and order placement refinements
Browse files Browse the repository at this point in the history
Introduced a new NATS package for message publishing and subscribing. Modified the order placement function to include user ID and ensure cart items are removed post-order placement. Updated dependencies to include NATS-related packages.
  • Loading branch information
mukulmantosh committed Sep 17, 2024
1 parent c90c3c8 commit fcf11fa
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
3 changes: 3 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 9 additions & 4 deletions pkg/handler/cart/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cart

import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
Expand All @@ -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!"})

}
Expand Down
36 changes: 36 additions & 0 deletions pkg/nats/nats_server.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 10 additions & 6 deletions pkg/service/cart_order/place_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit fcf11fa

Please sign in to comment.