Ejemplo n.º 1
0
 public async Task CreateAsync(LogSosGeolocation logSosGeolocation)
 {
     try
     {
         _dbSet.Add(logSosGeolocation);
         await _context.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         LogError("There is error while updating SosGeolocation: " + ex.Message, ex);
     }
 }
Ejemplo n.º 2
0
 public void Create(LogSosGeolocation logSosGeolocation)
 {
     try
     {
         _dbSet.Add(logSosGeolocation);
         _context.SaveChanges();
     }
     catch (Exception ex)
     {
         LogError("There is error while updating SosGeolocation: " + ex.Message, ex);
     }
 }
Ejemplo n.º 3
0
        public void Delete(LogSosGeolocation logSosGeolocation)
        {
            try
            {
                _cacheManager.Remove(String.Format(KeyForCacheYayYo, logSosGeolocation.Id));

                _context.Database.ExecuteSqlCommand("DELETE SosGeolocation WHERE Id = @p0", logSosGeolocation.Id);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                LogError("There is error while updating SosGeolocation: " + ex.Message, ex);
            }
        }
Ejemplo n.º 4
0
 public async Task UpdateAsync(LogSosGeolocation logSosGeolocation)
 {
     try
     {
         _cacheManager.Remove(String.Format(KeyForCacheYayYo, logSosGeolocation.Id));
         //ref:http://patrickdesjardins.com/blog/entity-framework-ef-modifying-an-instance-that-is-already-in-the-context
         //A way to do it is to navigate inside the local of the DbSet to see if this one is there. If the entity is present, than you detach
         var local = _context.Set <LogSosGeolocation>()
                     .Local
                     .FirstOrDefault(f => f.Id == logSosGeolocation.Id);
         if (local != null)
         {
             _context.Entry(local).State = EntityState.Detached;
         }
         _context.Entry(logSosGeolocation).State = EntityState.Modified;
         await _context.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         LogError("There is error while updating SosGeolocation: " + ex.Message, ex);
     }
 }
Ejemplo n.º 5
0
        public async Task <IHttpActionResult> InsertListSosGeoLocation(CreateListSosGeoLocationModel model)
        {
            try
            {
                //validate
                if (!ModelState.IsValid)
                {
                    foreach (var key in ModelState.Keys.Where(key => ModelState[key].Errors.Count > 0))
                    {
                        return(BadRequest(ModelState[key].Errors[0].ErrorMessage));
                    }
                }
                //find LogSOS info
                var yayYoId = model.YayYoId;
                var logSos  = await _logSosService.GetLastestLogSosAsync(yayYoId);

                if (logSos != null)
                {
                    var list = new List <LogSosGeolocation>();
                    foreach (var location in model.Locations)
                    {
                        var createdOnUtc   = DateTimeOffset.FromUnixTimeSeconds(location.Timestamp).DateTime;
                        var sosGeoLocation = new LogSosGeolocation
                        {
                            LogSosId     = logSos.Id,
                            Location     = location.Location,
                            CreatedOnUtc = createdOnUtc
                        };
                        list.Add(sosGeoLocation);
                    }
                    await _sosGeoLocationService.InsertRangeAsync(list);
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 6
0
 public void Update(LogSosGeolocation logSosGeolocation)
 {
     try
     {
         _cacheManager.Remove(String.Format(KeyForCacheYayYo, logSosGeolocation.Id));
         //ref:http://patrickdesjardins.com/blog/entity-framework-ef-modifying-an-instance-that-is-already-in-the-context
         //A way to do it is to navigate inside the local of the DbSet to see if this one is there. If the entity is present, than you detach
         var local = _context.Set <LogSosGeolocation>()
                     .Local
                     .FirstOrDefault(f => f.Id == logSosGeolocation.Id);
         if (local != null)
         {
             _context.Entry(local).State = EntityState.Detached;
         }
         _context.Entry(logSosGeolocation).State = EntityState.Modified;
         _context.SaveChanges();
     }
     catch (Exception ex)
     {
         //Log the error (uncomment dex variable name and add a line here to write a log.
         //Trace.TraceError("There is error while updating data: " + dex.InnerException);
         LogError("There is error while updating SosGeolocation: " + ex.Message, ex);
     }
 }
Ejemplo n.º 7
0
        public async Task <IHttpActionResult> ActiveSos(ActiveSosModel model)
        {
            //validate model
            if (!ModelState.IsValid)
            {
                foreach (var key in ModelState.Keys.Where(key => ModelState[key].Errors.Count > 0))
                {
                    return(BadRequest(ModelState[key].Errors[0].ErrorMessage));
                }
            }
            //find safety settings
            var safetySetting = _safetySettingService.GetByYayYoId(model.YayYoId);

            try
            {
                if (safetySetting == null)
                {
                    return(BadRequest("User does not exist"));
                }
                //update safety status
                safetySetting.Active       = true;
                safetySetting.UpdatedOnUtc = DateTime.UtcNow;
                await _safetySettingService.UpdateAsync(safetySetting);

                //create new SOS ID
                var logSos = new LogSos
                {
                    YayYoId         = model.YayYoId,
                    SafetySettingId = safetySetting.Id
                };

                await _logSosService.CreateAsync(logSos);

                //get ride book info from YayYo
                var rideBookRequest = new GetLastestRideBookRequestModel {
                    yayYo_id = model.YayYoId
                };
                var rideBookReponse = _yayYoService.GetRideBook(rideBookRequest);
                if (rideBookReponse == null)
                {
                    return(BadRequest("Error when get GetRideBook info"));
                }

                //add ride info to ride Information
                var rideBook = new LogRideInformation
                {
                    DriverName          = rideBookReponse.driver_name,
                    CarMake             = rideBookReponse.car_make,
                    CarModel            = rideBookReponse.car_model,
                    CarColor            = rideBookReponse.car_color,
                    CarLicense          = rideBookReponse.car_license,
                    YayYoId             = model.YayYoId,
                    LocationPickup      = rideBookReponse.pickup_location,
                    TimeEta             = rideBookReponse.time_eta,
                    TimePickup          = rideBookReponse.time_pickup,
                    LocationDestination = rideBookReponse.destination_location,
                    LogSosId            = logSos.Id
                };
                await _rideInformationService.CreateAsync(rideBook);

                //add SosGeolocation info
                var location = new LogSosGeolocation
                {
                    LogSosId = logSos.Id,
                    Location = model.GeoLocation
                };
                await _sosGeolocationService.CreateAsync(location);

                //send SMS
                var contacts = await _contactService.GetBySafetySettingIdAsync(safetySetting.Id);

                foreach (var contact in contacts)
                {
                    var contactInfo = new SmsRequestModel
                    {
                        FirstName           = safetySetting.FirstName,
                        RiderPickupLocation = rideBook.LocationPickup,
                        DestinationAddress  = rideBook.LocationDestination,
                        RideScheduledTime   = rideBook.TimePickup ?? DateTime.MinValue,
                        GeolocationAddress  = model.GeoLocation,
                        ContactPhoneNumber  = contact.Phone
                    };
                    _yayYoService.SendSms(contactInfo);
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }