Exemplo n.º 1
0
        public async Task <IActionResult> CreateTripForCustomer([FromBody] TripCreationDto tripCreationDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var customer = _usersRepository.GetCustomerById(Guid.Parse(User.Claims.FirstOrDefault(c => c.Type == Helpers.Constants.Strings.JwtClaimIdentifiers.CustomerId)?.Value));

            //if (customer.CurrentTrip != null)
            //    _tripsRepo.RemoveTrip(customer.Id);

            if (customer.CurrentTrip != null)
            {
                return(BadRequest("Already has trip"));
            }

            var tripEntity = new Trip()
            {
                CreationTime = DateTime.UtcNow,
                CustomerId   = customer.Id,
                From         = Helpers.Location.pointFromLatLng(tripCreationDto.From.Latitude, tripCreationDto.From.Longitude),
                To           = Helpers.Location.pointFromLatLng(tripCreationDto.To.Latitude, tripCreationDto.To.Longitude)
            };

            #region CalcLength

            var directions = await _googleMapsService.GetDirections(tripCreationDto.From.Latitude,
                                                                    tripCreationDto.From.Longitude,
                                                                    tripCreationDto.To.Latitude,
                                                                    tripCreationDto.To.Longitude);

            double length = 0;

            foreach (var r in directions.Routes)
            {
                foreach (var l in r.Legs)
                {
                    length += l.Distance.Value;
                }
            }

            if (Math.Abs(length) < double.Epsilon)
            {
                return(BadRequest("No route to destination"));
            }

            length /= 1000;

            tripEntity.Distance = length;

            tripEntity.Price = (long)Helpers.Price.getPriceInTokens(_configuration, tripEntity.Distance);;
            #endregion

            var contract = new Contract()
            {
                FromLatitude  = tripCreationDto.From.Latitude,
                FromLongitude = tripCreationDto.From.Longitude,
                ToLatitude    = tripCreationDto.To.Latitude,
                ToLongitude   = tripCreationDto.To.Longitude,
                TokenValue    = tripEntity.Price
            };

            _tripsRepo.AddContract(contract);

            var res = Payment.Create((ulong)contract.Id, new CreatePaymentPattern()
            {
                Value = (ulong)contract.TokenValue
            }, new User {
                PrivateKey = customer.Identity.PrivateKey
            }, ModelState);
            //swap
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }



            tripEntity.ContractId = contract.Id;

            _tripsRepo.InsertTrip(tripEntity, tripCreationDto.From.Latitude,
                                  tripCreationDto.From.Longitude,
                                  tripCreationDto.To.Latitude, tripCreationDto.To.Longitude);

            #region Responce

            var trip = _tripsRepo.GetTrip(customer.Id);

            var tripStatusDto = Mapper.Map <TripStatusDto>(trip);

            var from = trip.From;
            var to   = trip.To;

            tripStatusDto.From = Taxi.Helpers.Location.PointToPlaceDto(from);

            tripStatusDto.To = Taxi.Helpers.Location.PointToPlaceDto(to);

            return(Ok(tripStatusDto));

            #endregion
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateTripForCustomerInfo([FromBody] TripCreationDto tripCreationDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var customer = _usersRepository.GetCustomerById(Guid.Parse(User.Claims.FirstOrDefault(c => c.Type == Helpers.Constants.Strings.JwtClaimIdentifiers.CustomerId)?.Value));

            //if (customer.CurrentTrip != null)
            //    _tripsRepo.RemoveTrip(customer.Id);

            var tripEntity = new Trip()
            {
                // CreationTime = DateTime.UtcNow,
                CustomerId = customer.Id,
                From       = Helpers.Location.pointFromLatLng(tripCreationDto.From.Latitude, tripCreationDto.From.Longitude),
                To         = Helpers.Location.pointFromLatLng(tripCreationDto.To.Latitude, tripCreationDto.To.Longitude)
            };

            var directions = await _googleMapsService.GetDirections(tripCreationDto.From.Latitude,
                                                                    tripCreationDto.From.Longitude,
                                                                    tripCreationDto.To.Latitude,
                                                                    tripCreationDto.To.Longitude);

            double length = 0;

            foreach (var r in directions.Routes)
            {
                foreach (var l in r.Legs)
                {
                    length += l.Distance.Value;
                }
            }

            if (Math.Abs(length) < double.Epsilon)
            {
                return(BadRequest("No route to destination"));
            }

            length /= 1000;


            tripEntity.Distance = length;
            tripEntity.Price    = (long)Helpers.Price.getPriceInTokens(_configuration, tripEntity.Distance);
            #region Responce

            var tripStatusDto = Mapper.Map <TripStatusDto>(tripEntity);

            tripStatusDto.From = new PlaceDto()
            {
                Latitude  = tripCreationDto.From.Latitude,
                Longitude = tripCreationDto.From.Longitude
            };

            tripStatusDto.To = new PlaceDto()
            {
                Latitude  = tripCreationDto.To.Latitude,
                Longitude = tripCreationDto.To.Longitude
            };

            return(Ok(tripStatusDto));


            #endregion
        }