public async Task <RideDTO> EndRide(Ride ride, RideDTO currentRide) { if (ride != null && ride.RideStatus != null && ride.RideStatus.Code != Core.Constant.RideStatus.Waiting && currentRide != null && currentRide.Destination != null) { using (var trans = this.UnitOfWork.DBContext.Database.BeginTransaction()) { LocationLagLon destination = await this.UnitOfWork.LocationLagLonRepository.Create(currentRide.Destination.ConvertToEntity()); await this.UnitOfWork.SaveAsync(); ride.DestinationId = destination.Id; await this.UnitOfWork.RideRepository.Update(ride); await this.UnitOfWork.SaveAsync(); trans.Commit(); return(await ChangeStatusToWaitingForPayment(ride.Id)); } } else { return(null); } }
public override async Task <RideDTO> CreateAsync(RideDTO dtoObject) { using (var trans = this.UnitOfWork.DBContext.Database.BeginTransaction()) { Customer customer = await this.UnitOfWork.CustomerRepository.GetByUserId(this.requestInfo.UserId); if (customer == null) { customer = await this.UnitOfWork.CustomerRepository.Create(new Customer() { UserId = this.requestInfo.UserId }); } IEnumerable <Ride> rideEntities = await this.Repository.GetByCustomerId(customer.Id); if (rideEntities.Where(x => x.IsActive).Count() == 0) { LocationLagLon sourceEntity = null; LocationLagLon destinationEntity = null; Distance rideDistance = null; if (dtoObject.Source != null) { sourceEntity = await this.UnitOfWork.LocationLagLonRepository.Create(dtoObject.Source.ConvertToEntity()); } if (dtoObject.Destination != null) { destinationEntity = await this.UnitOfWork.LocationLagLonRepository.Create(dtoObject.Destination.ConvertToEntity()); } if (dtoObject.Source != null && dtoObject.Destination != null) { DistanceUnit distanceUnit = await this.UnitOfWork.DistanceUnitRepository.GetByCode(Core.Constant.DistanceUnit.Kilometer); if (distanceUnit != null) { rideDistance = await this.UnitOfWork.DistanceRepository.Create(new Distance() { UnitId = distanceUnit.Id, TotalDistance = (await CalculateDistance(dtoObject.Destination.Latitude, dtoObject.Destination.Longitude, dtoObject.Source.Latitude, dtoObject.Source.Longitude)) }); } } await this.UnitOfWork.SaveAsync(); RideStatus rideStatus = await this.UnitOfWork.RideStatusRepository.GetByCode(Core.Constant.RideStatus.Waiting); dtoObject.SourceId = sourceEntity.Id; dtoObject.DestinationId = destinationEntity.Id; dtoObject.RideDistanceId = rideDistance.Id; dtoObject.CustomerId = customer.Id; dtoObject.IsActive = true; if (rideStatus != null) { dtoObject.RideStatusId = rideStatus.Id; } dtoObject = await base.CreateAsync(dtoObject); trans.Commit(); return(dtoObject); } else { return(await this.CustomerHeartBeatAsync(dtoObject)); } } }