Skip to content

Commit

Permalink
Add order update feature to the delivery service
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mukulmantosh committed Sep 12, 2024
1 parent ca5b6c1 commit 3d5e534
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/database/models/delivery/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
29 changes: 29 additions & 0 deletions pkg/handler/delivery/order.go
Original file line number Diff line number Diff line change
@@ -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!"})

}
2 changes: 1 addition & 1 deletion pkg/handler/delivery/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
34 changes: 34 additions & 0 deletions pkg/service/delivery/order_placement.go
Original file line number Diff line number Diff line change
@@ -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")
}

}

0 comments on commit 3d5e534

Please sign in to comment.