/// <summary>
 /// GET: api/AlertSubscriptionApi
 /// </summary>
 /// <param name="skip"></param>
 /// <param name="take"></param>
 /// <returns></returns>
 public IEnumerable<AlertSubscriptionUVM> Get(int skip = 0, int take = 25)
 {
     using (var dbC = new DatabaseContext())
     {
         var allSubs = from als in dbC.AlertSubscriptions
                       select new AlertSubscriptionUVM
                           {
                               AddressString = als.AddressString,
                               CurrentLocation = als.LastKnownLocation != null ? new LocationUVM()
                                     {
                                         Latitude = als.LastKnownLocation.Latitude,
                                         Longitude = als.LastKnownLocation.Longitude
                                     } : null,
                               Id = als.Id,
                               PhoneNumber = als.PhoneNumber,
                               ValidUntil = als.ValidUntil
                           };
         return allSubs.ToList();
     }
 }
        /// <summary>
        /// POST: api/AlertSubscriptionApi
        /// </summary>
        /// <param name="model"></param>
        public dynamic Post([FromBody]AlertConfigurationUVM model)
        {
            if (ModelState.IsValid)
            {
                using (var dbC = new DatabaseContext())
                {
                    var allAffected = from af in dbC.AlertSubscriptions
                                      select new
                                      {
                                           PhoneNumer = af.PhoneNumber,
                                           ClosestSchoolName = (from school in dbC.Schools orderby school.Location.Distance(af.LastKnownLocation) ascending select school).FirstOrDefault()
                                      };

                    return allAffected.ToList();

                }
            }

            return NotFound();
        }
        /// <summary>
        /// GET: api/SchoolApi
        /// </summary>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <returns></returns>
        public async Task<IEnumerable<SchoolUVM>> Get(int skip = 0, int take = 25)
        {
            using (var dbC = new DatabaseContext())
            {
                var allSchoolsQ = from school in dbC.Schools
                                 select new SchoolUVM
                                     {
                                         Id = school.Id,
                                         Location = school.Location == null ? null : new LocationUVM 
                                            { 
                                                Latitude = school.Location.Latitude, 
                                                Longitude = school.Location.Longitude 
                                            },
                                         Name = school.Name
                                     };

                allSchoolsQ = allSchoolsQ.OrderBy(r => r.Name);
                allSchoolsQ = allSchoolsQ.Skip(skip);
                allSchoolsQ = allSchoolsQ.Take(take);


                return await allSchoolsQ.ToListAsync();
            }
        }
        /// <summary>
        /// GET: api/SchoolApi/Closest
        /// </summary>
        /// <param name="latitude"></param>
        /// <param name="longitude"></param>
        /// <returns></returns>
        public ICollection<SchoolUVM> GetClosest(double latitude, double longitude)
        {
            using (var dbC = new DatabaseContext())
            {
                var pos = DbGeography.FromText(string.Format("POINT({0} {1})", longitude, latitude));
                var school = from s in dbC.Schools 
                             orderby s.Location.Distance(pos) ascending
                             select new SchoolUVM
                             {
                                 Id = s.Id,
                                 Location = s.Location == null ? null : new LocationUVM
                                 {
                                     Latitude = s.Location.Latitude,
                                     Longitude = s.Location.Longitude
                                 },
                                 Name = s.Name
                             };
                return school.Take(5).ToList();

            }
        }
        /// <summary>
        /// POST: api/AlertSubscriptionApi
        /// </summary>
        /// <param name="model"></param>
        public IHttpActionResult Post([FromBody]AlertSubscriptionUVM model)
        {
            if (ModelState.IsValid)
            {
                using (var dbC = new DatabaseContext())
                {
                    dbC.AlertSubscriptions.Add(
                        new AlertSubscription
                        {
                            AddressString = model.AddressString,
                            LastKnownLocation = string.Format(CultureInfo.InvariantCulture, "Point({0} {1})", model.CurrentLocation.Longitude, model.CurrentLocation.Latitude),
                            PhoneNumber = model.PhoneNumber,
                            //ValidUntil = model.ValidUntil
                            ValidUntil = DateTime.UtcNow.AddYears(1)
                        }
                        );

                    dbC.SaveChanges();
                }
            }
            return null;
        }