public async Task AddVehicle(VehicleRequest model) { var vehicle = _vehicleRepository.FindOneByCondition(x => x.VehicleNumber == model.VehicleNumber); //Vehicle already existed if (vehicle != null) { if (vehicle.IsActive) { throw new DuplicateVehicleException(ErrorCode.E101, model.VehicleNumber); } vehicle.IsActive = true; vehicle.UpdatedDate = DateTime.UtcNow; _vehicleRepository.Update(vehicle); await _vehicleRepository.SaveAsync(); } else { vehicle = new Vehicle() { VehicleNumber = model.VehicleNumber, IsActive = true, CreatedDate = DateTime.UtcNow }; _vehicleRepository.Create(vehicle); await _vehicleRepository.SaveAsync(); } }
public async Task UpdateLocation(VehicleLocationRequest model) { var vehicle = _vehicleRepository.FindOneByCondition(x => x.VehicleNumber == model.VehicleNumber && x.IsActive); if (vehicle == null) { throw new VehicleNotFoundException(ErrorCode.E102, model.VehicleNumber); } //Check session was created or not var session = _trackingSessionRepository.FindOneByCondition(x => x.CreatedDate >= DateTime.UtcNow.Date && x.VehicleId == vehicle.Id); if (session == null) { session = new TrackingSession() { VehicleId = vehicle.Id, CreatedDate = DateTime.UtcNow, TrackingRemark = DateTime.UtcNow.ToString("d") }; _trackingSessionRepository.Create(session); await _trackingSessionRepository.SaveAsync(); } //Update Location with this session var location = new TrackingHistory() { TrackingSessionId = session.Id, CreatedDate = DateTime.UtcNow, Lat = model.Latitude, Lon = model.Longitude }; _trackingHistoryRepository.Create(location); await _trackingHistoryRepository.SaveAsync(); }