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

                _context.Database.ExecuteSqlCommand("DELETE LogSos WHERE Id = @p0", logSos.Id);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                LogError("There is error while updating data: " + ex.Message, ex);
            }
        }
Esempio n. 4
0
 public async Task UpdateAsync(LogSos logSos)
 {
     try
     {
         _cacheManager.Remove(String.Format(KeyForCacheYayYo, logSos.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 <LogSos>()
                     .Local
                     .FirstOrDefault(f => f.Id == logSos.Id);
         if (local != null)
         {
             _context.Entry(local).State = EntityState.Detached;
         }
         _context.Entry(logSos).State = EntityState.Modified;
         await _context.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         LogError("There is error while updating LogSos: " + ex.Message, ex);
     }
 }
Esempio n. 5
0
 public void Update(LogSos logSos)
 {
     try
     {
         _cacheManager.Remove(String.Format(KeyForCacheYayYo, logSos.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 <LogSos>()
                     .Local
                     .FirstOrDefault(f => f.Id == logSos.Id);
         if (local != null)
         {
             _context.Entry(local).State = EntityState.Detached;
         }
         _context.Entry(logSos).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 LogSos: " + ex.Message, ex);
     }
 }
        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));
            }
        }