-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
ca5b6c1
commit 3d5e534
Showing
4 changed files
with
69 additions
and
1 deletion.
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
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!"}) | ||
|
||
} |
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 |
---|---|---|
@@ -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") | ||
} | ||
|
||
} |