From 3d5e534f20a3057a46a0f859b08d599d73b78bd6 Mon Sep 17 00:00:00 2001 From: Mukul Mantosh Date: Thu, 12 Sep 2024 20:27:24 +0530 Subject: [PATCH] Add order update feature to the delivery service Created a new endpoint to update order status through a POST request. Implemented backend logic to handle various order statuses and update accordingly in the database. Added necessary data models and context handling for robust operation. --- pkg/database/models/delivery/delivery.go | 5 ++++ pkg/handler/delivery/order.go | 29 ++++++++++++++++++++ pkg/handler/delivery/routes.go | 2 +- pkg/service/delivery/order_placement.go | 34 ++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 pkg/handler/delivery/order.go create mode 100644 pkg/service/delivery/order_placement.go diff --git a/pkg/database/models/delivery/delivery.go b/pkg/database/models/delivery/delivery.go index d967722..1c2cf76 100644 --- a/pkg/database/models/delivery/delivery.go +++ b/pkg/database/models/delivery/delivery.go @@ -41,3 +41,8 @@ type DeliveryLoginParams struct { Phone string `json:"phone"` OTP string `json:"otp"` } + +type DeliveryOrderPlacementParams struct { + OrderID int64 `json:"order_id"` + Status string `json:"status"` +} diff --git a/pkg/handler/delivery/order.go b/pkg/handler/delivery/order.go new file mode 100644 index 0000000..98df806 --- /dev/null +++ b/pkg/handler/delivery/order.go @@ -0,0 +1,29 @@ +package delivery + +import ( + "Go_Food_Delivery/pkg/database/models/delivery" + "context" + "github.com/gin-gonic/gin" + "net/http" + "time" +) + +func (s *DeliveryHandler) updateOrder(c *gin.Context) { + ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second) + defer cancel() + + var deliveryOrder delivery.DeliveryOrderPlacementParams + if err := c.BindJSON(&deliveryOrder); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid request"}) + return + } + + _, err := s.service.OrderPlacement(ctx, deliveryOrder.OrderID, deliveryOrder.Status) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusCreated, gin.H{"message": "Order Updated!"}) + +} diff --git a/pkg/handler/delivery/routes.go b/pkg/handler/delivery/routes.go index 1e05864..d562ac9 100644 --- a/pkg/handler/delivery/routes.go +++ b/pkg/handler/delivery/routes.go @@ -12,6 +12,6 @@ func (s *DeliveryHandler) registerGroup(middleware ...gin.HandlerFunc) gin.IRout func (s *DeliveryHandler) routes() http.Handler { s.router.POST("/add", s.addDeliveryPerson) s.router.POST("/login", s.loginDelivery) - + s.router.POST("/update-order", s.updateOrder) return s.serve.Gin } diff --git a/pkg/service/delivery/order_placement.go b/pkg/service/delivery/order_placement.go new file mode 100644 index 0000000..7e65ed0 --- /dev/null +++ b/pkg/service/delivery/order_placement.go @@ -0,0 +1,34 @@ +package delivery + +import ( + "Go_Food_Delivery/pkg/database" + "Go_Food_Delivery/pkg/database/models/order" + "context" + "errors" +) + +func (deliverSrv *DeliveryService) OrderPlacement(ctx context.Context, orderID int64, deliveryStatus string) (bool, error) { + var orderInfo order.Order + setFilter := database.Filter{"order_status": deliveryStatus} + whereFilter := database.Filter{"order_id": orderID} + + // Check the order is valid or not. + err := deliverSrv.db.Select(ctx, &orderInfo, "order_id", orderID) + if err != nil { + return false, err + } + + switch orderInfo.OrderStatus { + case "in_progress": + _, err := deliverSrv.db.Update(ctx, "orders", setFilter, whereFilter) + if err != nil { + return false, err + } + return true, nil + case "failed", "completed", "cancelled", "on_the_way": + return false, errors.New("this order is invalid or has been already delivered/on_the_way/cancelled") + default: + return false, errors.New("unknown order status") + } + +}