Skip to content

Commit

Permalink
Add delivery person functionality
Browse files Browse the repository at this point in the history
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
mukulmantosh committed Sep 11, 2024
1 parent 69d0a94 commit 565ebb9
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"Go_Food_Delivery/pkg/database"
"Go_Food_Delivery/pkg/handler"
crt "Go_Food_Delivery/pkg/handler/cart"
delv "Go_Food_Delivery/pkg/handler/delivery"
"Go_Food_Delivery/pkg/handler/restaurant"
revw "Go_Food_Delivery/pkg/handler/review"
"Go_Food_Delivery/pkg/handler/user"
"Go_Food_Delivery/pkg/service/cart"
"Go_Food_Delivery/pkg/service/delivery"
restro "Go_Food_Delivery/pkg/service/restaurant"
"Go_Food_Delivery/pkg/service/review"
usr "Go_Food_Delivery/pkg/service/user"
Expand Down Expand Up @@ -57,6 +59,9 @@ func main() {
cartService := cart.NewCartService(db, env)
crt.NewCartHandler(s, "/cart", cartService, middlewares, validate)

// Delivery
deliveryService := delivery.NewDeliveryService(db, env)
delv.NewDeliveryHandler(s, "/delivery", deliveryService, nil, validate)
log.Fatal(s.Run())

}
10 changes: 10 additions & 0 deletions pkg/abstract/delivery/delivery.go
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)
}
2 changes: 2 additions & 0 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"Go_Food_Delivery/pkg/database/models/cart"
"Go_Food_Delivery/pkg/database/models/delivery"
"Go_Food_Delivery/pkg/database/models/order"
"Go_Food_Delivery/pkg/database/models/restaurant"
"Go_Food_Delivery/pkg/database/models/review"
Expand Down Expand Up @@ -199,6 +200,7 @@ func (d *DB) Migrate() error {
(*order.OrderItems)(nil),
(*cart.Cart)(nil),
(*cart.CartItems)(nil),
(*delivery.DeliveryPerson)(nil),
}

for _, model := range models {
Expand Down
16 changes: 16 additions & 0 deletions pkg/database/models/delivery/delivery.go
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
}
30 changes: 30 additions & 0 deletions pkg/handler/delivery/delivery.go
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!"})

}
16 changes: 16 additions & 0 deletions pkg/handler/delivery/routes.go
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
}
38 changes: 38 additions & 0 deletions pkg/handler/delivery/service.go
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() {

}
14 changes: 14 additions & 0 deletions pkg/service/delivery/add_delivery_person.go
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
}
12 changes: 12 additions & 0 deletions pkg/service/delivery/service.go
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}
}

0 comments on commit 565ebb9

Please sign in to comment.