-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit implements the addition of delivery person functionalities. It includes handler, service, and database model integration for creating new delivery person entries via the API. Routing has been updated to support this new functionality.
- Loading branch information
1 parent
69d0a94
commit 565ebb9
Showing
9 changed files
with
143 additions
and
0 deletions.
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,10 @@ | ||
package delivery | ||
|
||
import ( | ||
"Go_Food_Delivery/pkg/database/models/delivery" | ||
"context" | ||
) | ||
|
||
type DeliveryPerson interface { | ||
AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (*delivery.DeliveryPerson, error) | ||
} |
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,16 @@ | ||
package delivery | ||
|
||
import ( | ||
"Go_Food_Delivery/pkg/database/models/utils" | ||
"github.com/uptrace/bun" | ||
) | ||
|
||
type DeliveryPerson struct { | ||
bun.BaseModel `bun:"table:delivery_person"` | ||
DeliveryPersonID int64 `bun:",pk,autoincrement" json:"delivery_person_id"` | ||
Name string `bun:"name,notnull" json:"name"` | ||
Phone string `bun:"phone,notnull" json:"phone"` | ||
VehicleDetails string `bun:"vehicle_details,notnull" json:"vehicle_details"` | ||
Status string `bun:"status,notnull" json:"status"` | ||
utils.Timestamp | ||
} |
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,30 @@ | ||
package delivery | ||
|
||
import ( | ||
"Go_Food_Delivery/pkg/database/models/delivery" | ||
"context" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func (s *DeliveryHandler) addDeliveryPerson(c *gin.Context) { | ||
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second) | ||
defer cancel() | ||
|
||
var deliverPerson delivery.DeliveryPerson | ||
|
||
if err := c.BindJSON(&deliverPerson); err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid request"}) | ||
return | ||
} | ||
|
||
_, err := s.service.AddDeliveryPerson(ctx, &deliverPerson) | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, gin.H{"message": "Delivery Person Added Successfully!"}) | ||
|
||
} |
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,16 @@ | ||
package delivery | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
func (s *DeliveryHandler) registerGroup(middleware ...gin.HandlerFunc) gin.IRoutes { | ||
return s.serve.Gin.Group(s.group).Use(middleware...) | ||
} | ||
|
||
func (s *DeliveryHandler) routes() http.Handler { | ||
s.router.POST("/add", s.addDeliveryPerson) | ||
|
||
return s.serve.Gin | ||
} |
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,38 @@ | ||
package delivery | ||
|
||
import ( | ||
"Go_Food_Delivery/pkg/handler" | ||
"Go_Food_Delivery/pkg/service/delivery" | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/validator/v10" | ||
) | ||
|
||
type DeliveryHandler struct { | ||
serve *handler.Server | ||
group string | ||
router gin.IRoutes | ||
service *delivery.DeliveryService | ||
middleware []gin.HandlerFunc | ||
validate *validator.Validate | ||
} | ||
|
||
func NewDeliveryHandler(s *handler.Server, groupName string, | ||
service *delivery.DeliveryService, middleware []gin.HandlerFunc, | ||
validate *validator.Validate) { | ||
|
||
cartHandler := &DeliveryHandler{ | ||
s, | ||
groupName, | ||
nil, | ||
service, | ||
middleware, | ||
validate, | ||
} | ||
cartHandler.router = cartHandler.registerGroup(middleware...) | ||
cartHandler.routes() | ||
cartHandler.registerValidator() | ||
} | ||
|
||
func (s *DeliveryHandler) registerValidator() { | ||
|
||
} |
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,14 @@ | ||
package delivery | ||
|
||
import ( | ||
"Go_Food_Delivery/pkg/database/models/delivery" | ||
"context" | ||
) | ||
|
||
func (deliverSrv *DeliveryService) AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (*delivery.DeliveryPerson, error) { | ||
_, err := deliverSrv.db.Insert(ctx, deliveryPerson) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return deliveryPerson, nil | ||
} |
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,12 @@ | ||
package delivery | ||
|
||
import "Go_Food_Delivery/pkg/database" | ||
|
||
type DeliveryService struct { | ||
db database.Database | ||
env string | ||
} | ||
|
||
func NewDeliveryService(db database.Database, env string) *DeliveryService { | ||
return &DeliveryService{db, env} | ||
} |