Documentation ¶
Overview ¶
Package routes exposes features for creating & reading routes
Index ¶
- Variables
- func Init()
- type Controller
- func (controller *Controller) CreateRoute(ctx context.Context, req *routesgrpc.CreateRouteRequest) (resp *routesgrpc.CreateRouteResponse, err error)
- func (controller *Controller) GetRoutesByDriver(ctx context.Context, req *routesgrpc.GetRoutesByDriverRequest) (*routesgrpc.GetRoutesByDriverResponse, error)
- func (controller *Controller) ReadRoute(ctx context.Context, req *routesgrpc.ReadRouteRequest) (*routesgrpc.ReadRouteResponse, error)
- type Route
- type RouteService
- type Service
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ValidationError error
ValidationError represents a Bongo validation error
Functions ¶
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 ¶
func (controller *Controller) GetRoutesByDriver(ctx context.Context, req *routesgrpc.GetRoutesByDriverRequest) (*routesgrpc.GetRoutesByDriverResponse, error)
GetRoutesByDriver will call the inner service that will have all the business logic to serve this gRPC call
func (*Controller) ReadRoute ¶
func (controller *Controller) ReadRoute(ctx context.Context, req *routesgrpc.ReadRouteRequest) (*routesgrpc.ReadRouteResponse, error)
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
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 ¶
Service is the base struct that holds db connection and interfaces the given service
func NewService ¶
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 ¶
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 ¶
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 ¶
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