Exemple #1
0
        public async Task <IActionResult> ApproveFinish()
        {
            var customer = _usersRepository.GetCustomerById(Guid.Parse(User.Claims.FirstOrDefault(c => c.Type == Helpers.Constants.Strings.JwtClaimIdentifiers.CustomerId)?.Value));

            if (customer?.CurrentTrip == null)
            {
                return(NotFound());
            }

            if (customer.CurrentTrip.FinishTime == default(DateTime))
            {
                ModelState.AddModelError(nameof(Trip), "Trip not finished");
                return(BadRequest(ModelState));
            }

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

            // TODO:Money to Driver
            var result = Order.CompleteOrder((ulong)trip.ContractId, new DefaultControllerPattern(),
                                             new User {
                PrivateKey = customer.Identity.PrivateKey
            }, ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var tripHistory = Helpers.ComplexMapping.HistoryFromTrip(trip);

            var addres = await _tripsRepo.AddTripHistory(tripHistory);

            var res = _tripsRepo.RemoveTrip(customer.CurrentTrip.CustomerId);

            if (!res || !addres)
            {
                return(Conflict());
            }

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

            var toReturn = new TripHistoryDto()
            {
                CustomerId = tripHistory.CustomerId,
                DriverId   = tripHistory.DriverId,

                Id         = tripHistory.Id,
                From       = Helpers.Location.PointToPlaceDto(from),
                To         = Helpers.Location.PointToPlaceDto(to),
                FinishTime = tripHistory.FinishTime,
                Price      = tripHistory.Price,
                Distance   = tripHistory.Distance
            };//check if correctly maps from nullable

            return(Ok(toReturn));
        }