public async Task <ActionResult> RegisterVehiclePosition(VehiclePositionDTO vehiclePosition) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } bool result = await _vehicleService.RecordVehiclePosition(vehiclePosition).ConfigureAwait(false); if (result) { return(StatusCode(201)); } else { return(BadRequest(result)); } } catch (Exception ex) { return(BadRequest(ex)); } }
public async Task <bool> RecordVehiclePosition(VehiclePositionDTO vehiclePosition) { var guidDeviceId = Guid.Parse(vehiclePosition.DeviceID); try { if (vehiclePosition != null) { var vehiclePositionModel = new LocationDetail() { Latitude = vehiclePosition.Latitude, Longitude = vehiclePosition.Longitude, UpdateTime = DateTime.Now, DeviceID = guidDeviceId }; _unitOfWork.LocationDetailRepository.Add(vehiclePositionModel); var result = await _unitOfWork.CommitAsync().ConfigureAwait(false); if (result > 0) { return(true); } } return(false); } catch (System.Exception ex) { _logger.LogError(ex.Message); return(false); } }
public async Task <IHttpActionResult> PutVehiclePosition(VehiclePositionDTO vehiclePosition) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } bool result = await _vehicleService.RecordVehiclePositionAsync(vehiclePosition); if (result) { return(StatusCode(HttpStatusCode.OK)); } else { return(StatusCode(HttpStatusCode.BadRequest)); } } catch (Exception) { return(StatusCode(HttpStatusCode.ExpectationFailed)); } }
public async Task <ActionResult> GetCurrentPosition(string userID, string deviceId) { VehiclePositionDTO vehiclePosition = null; try { if (!string.IsNullOrEmpty(userID) && !string.IsNullOrEmpty(deviceId)) { vehiclePosition = await _vehicleService.GetCurrentPosition(userID, deviceId).ConfigureAwait(false); if (vehiclePosition == null) { return(NotFound()); } // Google Map Api for local name vehiclePosition.Local = await _vehicleService.GetLocation(vehiclePosition.Latitude.ToString(), vehiclePosition.Longitude.ToString()).ConfigureAwait(false); } else { return(BadRequest()); } } catch (Exception ex) { return(BadRequest(ex)); } return(Ok(vehiclePosition)); }
public async Task <IHttpActionResult> GetVehicleCurrentPosition(int userID, int deviceId) { VehiclePositionDTO vehicle = null; try { if (userID != 0 && deviceId != 0) { vehicle = await _vehicleService.GetCurrentVehiclePositionAsync(userID, deviceId); if (vehicle == null) { return(NotFound()); } // Bonus :) Calling Google Map Api for matching localities vehicle.Locality.LocalityName = await _vehicleService.GetMatchingLocality(vehicle.Latitude + "," + vehicle.Longitude); } else { return(StatusCode(HttpStatusCode.BadRequest)); } } catch (Exception ex) { return(StatusCode(HttpStatusCode.ExpectationFailed)); } return(Ok(vehicle)); }
public async Task <bool> RecordVehiclePosition(VehiclePositionDTO vehiclePosition) { bool checkVehicleDevice = await _vehicleRepository.CheckVehicleDeviceviaUser(vehiclePosition.UserID, vehiclePosition.DeviceID).ConfigureAwait(false); if (checkVehicleDevice) { return(await _vehicleRepository.RecordVehiclePosition(vehiclePosition).ConfigureAwait(false)); } return(false); }
public async Task <bool> RecordVehiclePositionAsync(VehiclePositionDTO vehiclePosition) { // Check a device or user cannot update the position of another vehicle int checkUserDevicePosition = await _vehicleRepository.CheckUserWithVehicleDevice(vehiclePosition.UserID, vehiclePosition.DeviceID); if (checkUserDevicePosition > 0) { return(await _vehicleRepository.RecordVehiclePositionAsync(vehiclePosition)); } return(false); }
public async Task <VehiclePositionDTO> GetCurrentPosition(string userID, string deviceId) { var guidDeviceId = Guid.Parse(deviceId); try { //Get userId using email var userResult = await CheckUserExist(userID).ConfigureAwait(false); if (userResult != null) { var resultSet = await _dbContext.Vehicles .Join( _dbContext.VehicleDevices, u => u.VehicleID, x => x.VehicleID, (u, x) => new { VEHICLE = u, VDEVICE = x }) .Join( _dbContext.LocationDetails, u => u.VDEVICE.DeviceID, x => x.DeviceID, (u, x) => new { u.VEHICLE, u.VDEVICE, LOCATIONDETAILS = x }) .Select(s => new { UserID = s.VEHICLE.User.Id, s.VDEVICE.DeviceID, s.LOCATIONDETAILS.Latitude, s.LOCATIONDETAILS.Longitude, s.LOCATIONDETAILS.UpdateTime }) .Where(w => w.UserID == userResult.Id && w.DeviceID == guidDeviceId) .OrderByDescending(o => o.UpdateTime).Take(1).FirstOrDefaultAsync().ConfigureAwait(false); var result = new VehiclePositionDTO() { UserID = resultSet.UserID, DeviceID = resultSet.DeviceID.ToString(), Latitude = resultSet.Latitude, Longitude = resultSet.Longitude, UpdateTime = resultSet.UpdateTime }; return(result); } return(null); } catch (Exception ex) { _logger.LogError(ex.Message); return(null); } }
public async Task <VehiclePositionDTO> GetCurrentPosition(string userID, string deviceID) { VehiclePositionDTO vehiclePosition = null; try { bool checkVehicleDevice = await _vehicleRepository.CheckVehicleDeviceviaUser(userID, deviceID).ConfigureAwait(false); if (checkVehicleDevice) { vehiclePosition = await _vehicleRepository.GetCurrentPosition(userID, deviceID).ConfigureAwait(false); } } catch (Exception) { return(null); } return(vehiclePosition); }
public async Task <VehiclePositionDTO> GetCurrentVehiclePositionAsync(int userID, int deviceID) { VehiclePositionDTO result = null; try { // Check a device or user cannot update the position of another vehicle int CheckUserDevicePosition = await _vehicleRepository.CheckUserWithVehicleDevice(userID, deviceID); if (CheckUserDevicePosition > 0) { result = await _vehicleRepository.GetCurrentVehiclePositionAsync(userID, deviceID); } } catch (Exception) { return(null); } return(result); }
public async Task <bool> RecordVehiclePositionAsync(VehiclePositionDTO vehiclePosition) { if (vehiclePosition != null) { var objVehiclePosition = new Location() { DeviceID = vehiclePosition.DeviceID, Latitude = vehiclePosition.Latitude, Longitude = vehiclePosition.Longitude, UpdateLocationTimeStamp = vehiclePosition.UpdateLocationTimeStamp }; _UnitOfWork.LocationRepository.Add(objVehiclePosition); var result = await _UnitOfWork.CommitAsync(); if (result > 0) { return(true); } } return(false); }
public async Task <VehiclePositionDTO> GetCurrentVehiclePositionAsync(int userID, int deviceId) { var queryObj = await _dbContext.Vehicles .Join( _dbContext.VehicleDevices, v => v.VehicleID, vd => vd.VehicleID, (v, vd) => new { VEHICLE = v, DEVICE = vd }) .Join( _dbContext.Locations, vdd => vdd.DEVICE.DeviceID, loc => loc.DeviceID, (vdd, loc) => new { vdd.VEHICLE, vdd.DEVICE, LOCATION = loc }) .Select(s => new { UserID = s.VEHICLE.UserID, DeviceID = s.DEVICE.DeviceID, Latitude = s.LOCATION.Latitude, Longitude = s.LOCATION.Longitude, UpdateLocationTimeStamp = s.LOCATION.UpdateLocationTimeStamp } ) .Where(w => w.UserID == userID && w.DeviceID == deviceId) .OrderByDescending(o => o.UpdateLocationTimeStamp).Take(1).FirstOrDefaultAsync(); var result = new VehiclePositionDTO() { UserID = queryObj.UserID, DeviceID = queryObj.DeviceID, Latitude = queryObj.Latitude, Longitude = queryObj.Longitude, UpdateLocationTimeStamp = queryObj.UpdateLocationTimeStamp }; return(result); }