add PATCH update delivery status by id
This commit is contained in:
@@ -38,6 +38,7 @@ func main() {
|
||||
r.GET("/api/deliveries/count", h.GetDeliveryCount)
|
||||
r.POST("/api/deliveries", h.CreateDelivery)
|
||||
r.PATCH("/api/deliveries/:id", h.UpdateDelivery)
|
||||
r.PATCH("/api/deliveries/:id/status", h.UpdateDeliveryStatus)
|
||||
r.DELETE("/api/deliveries/:id", h.DeleteDelivery)
|
||||
|
||||
r.Run(":8080")
|
||||
|
||||
@@ -16,4 +16,7 @@ DELETE FROM deliveries WHERE id = $1;
|
||||
UPDATE deliveries SET date = $1, pickup_location = $2, product_name = $3, address = $4, phone = $5, additional_phone = $6, has_elevator = $7, comment = $8, updated_at = NOW() WHERE id = $9;
|
||||
|
||||
-- name: GetDeliveryCount :many
|
||||
SELECT COUNT(*) as count, date FROM deliveries WHERE date >= DATE_TRUNC('month', CURRENT_DATE) GROUP BY date;
|
||||
SELECT COUNT(*) as count, date FROM deliveries WHERE date >= DATE_TRUNC('month', CURRENT_DATE) GROUP BY date;
|
||||
|
||||
-- name: UpdateDeliveryStatus :exec
|
||||
UPDATE deliveries SET status = $1, updated_at = NOW() WHERE id = $2;
|
||||
|
||||
@@ -17,6 +17,7 @@ type Querier interface {
|
||||
GetDeliveryByID(ctx context.Context, id pgtype.UUID) (Delivery, error)
|
||||
GetDeliveryCount(ctx context.Context) ([]GetDeliveryCountRow, error)
|
||||
UpdateDelivery(ctx context.Context, arg UpdateDeliveryParams) error
|
||||
UpdateDeliveryStatus(ctx context.Context, arg UpdateDeliveryStatusParams) error
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
|
||||
@@ -186,3 +186,17 @@ func (q *Queries) UpdateDelivery(ctx context.Context, arg UpdateDeliveryParams)
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateDeliveryStatus = `-- name: UpdateDeliveryStatus :exec
|
||||
UPDATE deliveries SET status = $1, updated_at = NOW() WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateDeliveryStatusParams struct {
|
||||
Status string `db:"status" json:"status"`
|
||||
ID pgtype.UUID `db:"id" json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateDeliveryStatus(ctx context.Context, arg UpdateDeliveryStatusParams) error {
|
||||
_, err := q.db.Exec(ctx, updateDeliveryStatus, arg.Status, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -160,6 +160,47 @@ func (h *Handler) UpdateDelivery(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Delivery updated"})
|
||||
}
|
||||
|
||||
// PATCH /api/deliveries/:id/status
|
||||
func (h *Handler) UpdateDeliveryStatus(c *gin.Context) {
|
||||
var req struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"})
|
||||
return
|
||||
}
|
||||
|
||||
parsedID, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid UUID format", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
status := req.Status
|
||||
if status == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Status is required"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.queries.UpdateDeliveryStatus(c.Request.Context(), sqlc.UpdateDeliveryStatusParams{
|
||||
ID: pgtype.UUID{Bytes: parsedID, Valid: true},
|
||||
Status: status,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update delivery status", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Delivery status updated"})
|
||||
}
|
||||
|
||||
// DELETE /api/deliveries/:id
|
||||
func (h *Handler) DeleteDelivery(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
Reference in New Issue
Block a user