public async Task <bool> AddGeolocation(GeolocationModel geolocationModel)
 {
     try
     {
         using (GeolocationsContext ctx = new GeolocationsContext())
         {
             ctx.Geolocations.Add(geolocationModel);
             return(await ctx.SaveChangesAsync() > 0);
         }
     }
     catch (Exception ex)
     {
         _logger.Error("Saving geolocation error " + ex.Message);
         throw ex;
     }
 }
 public async Task <GeolocationModel> GetGeolocationByGuid(Guid guid)
 {
     try
     {
         using (GeolocationsContext ctx = new GeolocationsContext())
         {
             return(await ctx.Geolocations
                    .Include(x => x.Location.Languages)
                    .SingleOrDefaultAsync(x => x.Id == guid));
         }
     }
     catch (Exception ex)
     {
         _logger.Error("Getting geolocation error" + ex.Message);
         throw ex;
     }
 }
        public async Task <bool> DeleteGeolocation(Guid geolocationModelGuid)
        {
            using (GeolocationsContext ctx = new GeolocationsContext())
            {
                try
                {
                    var geolocationToDelete = await ctx.Geolocations.SingleOrDefaultAsync(x => x.Id == geolocationModelGuid);

                    ctx.Geolocations.Remove(geolocationToDelete);
                    return(await ctx.SaveChangesAsync() > 0);
                }
                catch (Exception ex)
                {
                    _logger.Error("Deleting geolocation error" + ex.Message);
                    throw ex;
                }
            }
        }
        public async Task <List <GeolocationModel> > GetGeolocations(string ipOrHost,
                                                                     DateTime?dateTimeFrom,
                                                                     DateTime?dateTimeTo,
                                                                     int startIndex,
                                                                     int limitQuery)
        {
            try
            {
                using (GeolocationsContext ctx = new GeolocationsContext())
                {
                    var query = ctx.Geolocations.AsQueryable();
                    if (!string.IsNullOrEmpty(ipOrHost))
                    {
                        query = query.Where(x => x.Ip == ipOrHost || x.Host.Contains(ipOrHost));
                    }

                    if (dateTimeFrom.HasValue)
                    {
                        query = query.Where(x => x.RequestDateTime >= dateTimeFrom);
                    }

                    if (dateTimeTo.HasValue)
                    {
                        query = query.Where(x => x.RequestDateTime <= dateTimeTo);
                    }

                    query = query.OrderByDescending(x => x.RequestDateTime).Skip(startIndex).Take(limitQuery);
                    return(await query.Include(x => x.Location.Languages).ToListAsync());
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Getting geolocations history error" + ex.Message);
                throw ex;
            }
        }