routes

package
v0.0.0-...-2d3cc3e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2021 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package routes exposes features for creating & reading routes

Index

Examples

Constants

This section is empty.

Variables

View Source
var ValidationError error

ValidationError represents a Bongo validation error

Functions

func Init

func Init()

Init will initialize the gRPC server for this service

Types

type Controller

type Controller struct {
	RouteService
}

Controller holds the service and implements the controller interface

func NewRoutesController

func NewRoutesController(service RouteService) *Controller

NewRoutesController will create a new value of type *Controller

Example
routeService := NewService(&database.Connection{}, &routific.Routific{})
controller := NewRoutesController(routeService)
fmt.Printf("%T", controller)
Output:

*routes.Controller

func (*Controller) CreateRoute

func (controller *Controller) CreateRoute(ctx context.Context, req *routesgrpc.CreateRouteRequest) (resp *routesgrpc.CreateRouteResponse, err error)

CreateRoute will call the inner service that will have all the business logic to serve this gRPC call and will be in charge of constructing Requests and Responses

func (*Controller) GetRoutesByDriver

GetRoutesByDriver will call the inner service that will have all the business logic to serve this gRPC call

func (*Controller) ReadRoute

ReadRoute will call the inner service that will have all the business logic to serve this gRPC call

type Route

type Route struct {
	bongo.DocumentBase `bson:",inline"`
	DriverID           bson.ObjectId `bson:"driver_id" json:"driver_id"`
	CustomerID         bson.ObjectId `bson:"customer_id" json:"customer_id"`
	OrderID            bson.ObjectId `bson:"order_id" json:"order_id"`
	Lat                float64
	Lng                float64
	Solution           routific.VehicleRoutingResponse
}

Route is the struct that defines a Route entity

func (*Route) Validate

func (r *Route) Validate(*bongo.Collection) []error

Validate is a hook that will get triggered by bongo, this method will handle validation

type RouteService

type RouteService interface {
	UpdateOrCreateRoute(r *Route) []error
	ReadRoute(ID bson.ObjectId, r *Route) error
	GetRoutesByDriver(driverID bson.ObjectId) ([]Route, error)
	CreateInRoutific(r *Route, current routific.CurrentRoute) error
}

RouteService defines the interface for the driver service

type Service

type Service struct {
	database.Bongo
	routific.Service
}

Service is the base struct that holds db connection and interfaces the given service

func NewService

func NewService(conn database.Bongo, routificAPI routific.Service) *Service

NewService will handle the creation of a new service

Example
conn := database.Connect()
routific := &routific.Routific{}
newService := NewService(conn, routific)
fmt.Printf("%T", newService)
Output:

*routes.Service

func (*Service) CreateInRoutific

func (s *Service) CreateInRoutific(r *Route, current routific.CurrentRoute) error

CreateInRoutific will send a HTTP request to Routific API's which will return a valid solution for that route+driver

Example
conn := database.Connect()
routificAPI := &routific.MockService{}
service := NewService(conn, routificAPI)
route, currentRoute, destinationRoute, responseBody := MockForRoutificService()
routificAPI.On("GetVehicleRoute", route.DriverID, currentRoute, destinationRoute).Return(responseBody, nil).Once()
service.CreateInRoutific(route, currentRoute)
fmt.Println(route.Solution)
tearDown("routes")
Output:

{success 31.983334 map[vehicle_1:[{1 Location name 1} {2 Location name 2}]]}

func (*Service) GetRoutesByDriver

func (s *Service) GetRoutesByDriver(driverID bson.ObjectId) ([]Route, error)

GetRoutesByDriver will try to return all available routes for a specific driver

Example
conn := database.Connect()
routificAPI := &routific.Routific{}
service := NewService(conn, routificAPI)
route := &Route{
	DriverID:   bson.NewObjectId(),
	CustomerID: bson.NewObjectId(),
	OrderID:    bson.NewObjectId(),
	Lat:        34.567,
	Lng:        -56.1234,
	Solution:   routific.VehicleRoutingResponse{},
}
service.UpdateOrCreateRoute(route)
routes, _ := service.GetRoutesByDriver(route.DriverID)
firstRoute := routes[0]
fmt.Println(firstRoute.Lat)
Output:

34.567

func (*Service) ReadRoute

func (s *Service) ReadRoute(ID bson.ObjectId, r *Route) error

ReadRoute will try to return a Document from mongodb according to the given bson.ObjectId

Example
conn := database.Connect()
routificAPI := &routific.Routific{}
service := NewService(conn, routificAPI)
exampleSolution := routific.VehicleRoutingResponse{
	Status:          "success",
	TotalTravelTime: 31.983334,
	Solution:        make(map[string][]routific.LocationResponse),
}
exampleSolution.Solution["vehicle_1"] = []routific.LocationResponse{
	{
		LocationID:   "1",
		LocationName: "Location name 1",
	},
	{
		LocationID:   "2",
		LocationName: "Location name 2",
	},
}
createRoute := &Route{
	DriverID:   bson.NewObjectId(),
	CustomerID: bson.NewObjectId(),
	OrderID:    bson.NewObjectId(),
	Lat:        34.567,
	Lng:        -56.1234,
	Solution:   exampleSolution,
}
readRoute := &Route{}
service.UpdateOrCreateRoute(createRoute)
service.ReadRoute(createRoute.Id, readRoute)
tearDown("routes")
fmt.Println(readRoute.Solution)
Output:

{success 31.983334 map[vehicle_1:[{1 Location name 1} {2 Location name 2}]]}

func (*Service) UpdateOrCreateRoute

func (s *Service) UpdateOrCreateRoute(r *Route) []error

UpdateOrCreateRoute tries to persist a given Route struct. It'll check for any validation errors and return if any

Example
conn := database.Connect()
routificAPI := &routific.Routific{}
service := NewService(conn, routificAPI)
route := &Route{
	DriverID:   bson.NewObjectId(),
	CustomerID: bson.NewObjectId(),
	OrderID:    bson.NewObjectId(),
	Lat:        34.567,
	Lng:        -56.1234,
	Solution:   routific.VehicleRoutingResponse{},
}
service.UpdateOrCreateRoute(route)
fmt.Printf("%T", route.Id)
tearDown("routes")
Output:

bson.ObjectId

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL