Beispiel #1
0
        public Model.UserSubscription UpdateUserSubscription(Model.UserSubscription subscription)
        {
            var dataModel = new OCM.Core.Data.OCMEntities();

            Core.Data.UserSubscription update = new Core.Data.UserSubscription();
            if (subscription.ID > 0)
            {
                //edit
                update = dataModel.UserSubscriptions.FirstOrDefault(u => u.ID == subscription.ID && u.UserID == subscription.UserID);
            }
            else
            {
                //new item
                update.User             = dataModel.Users.FirstOrDefault(u => u.ID == subscription.UserID);
                update.DateCreated      = DateTime.UtcNow;
                update.DateLastNotified = update.DateCreated;
            }
            if (update != null)
            {
                update.Title     = subscription.Title;
                update.IsEnabled = subscription.IsEnabled;
                if (subscription.CountryID != null)
                {
                    update.Country = dataModel.Countries.FirstOrDefault(c => c.ID == subscription.CountryID);
                }
                else
                {
                    update.Country   = null;
                    update.CountryID = null;
                }

                update.DistanceKM = subscription.DistanceKM;

                if (subscription.FilterSettings != null)
                {
                    update.FilterSettings = JsonConvert.SerializeObject(subscription.FilterSettings);
                }
                else
                {
                    update.FilterSettings = null;
                }

                update.Latitude  = subscription.Latitude;
                update.Longitude = subscription.Longitude;
                update.NotificationFrequencyMins       = subscription.NotificationFrequencyMins;
                update.NotifyComments                  = subscription.NotifyComments;
                update.NotifyEmergencyChargingRequests = subscription.NotifyEmergencyChargingRequests;
                update.NotifyGeneralChargingRequests   = subscription.NotifyGeneralChargingRequests;
                update.NotifyMedia        = subscription.NotifyMedia;
                update.NotifyPOIAdditions = subscription.NotifyPOIAdditions;
                update.NotifyPOIEdits     = subscription.NotifyPOIEdits;
                update.NotifyPOIUpdates   = subscription.NotifyPOIUpdates;
            }

            if (update.ID == 0)
            {
                dataModel.UserSubscriptions.Add(update);
            }
            dataModel.SaveChanges();

            return(OCM.API.Common.Model.Extensions.UserSubscription.FromDataModel(update, true));
        }
        public ActionResult SubscriptionEdit(UserSubscription userSubscription)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(Request["CountryCode"]))
                {
                    //TEMP fix country id passed as ISO
                    var country = new ReferenceDataManager().GetCountryByISO(Request["CountryCode"]);
                    userSubscription.CountryID = country.ID;
                }
                else
                {
                    userSubscription.Country = null;
                    userSubscription.CountryID = null;
                }
                userSubscription = new UserSubscriptionManager().UpdateUserSubscription(userSubscription);
                return RedirectToAction("Subscriptions", "Profile");
            }

            PopulateSubscriptionEditorViewBag(userSubscription);
            return View(userSubscription);
        }
        private void PopulateSubscriptionEditorViewBag(UserSubscription subscription)
        {
            var refDataManager = new ReferenceDataManager();
            var coreRefData = refDataManager.GetCoreReferenceData();

            var allCountries = coreRefData.Countries;

            allCountries.Insert(0, new Country { ID = 0, ISOCode = "", Title = "(All Countries)" });

            ViewBag.CountryList = new SelectList(allCountries, "ISOCode", "Title", (subscription.Country != null ? subscription.Country.ISOCode : null));

            var NotificationFrequencyOptions = new[]{
                new { ID = 5, Title = "Every 5 Minutes"},
                new { ID = 30, Title = "Every 30 Minutes"},
                new { ID = 60, Title = "Every Hour"},
                new { ID = 60*12, Title = "Every 12 Hours"},
                new { ID = 60*24, Title = "Every Day"},
                new { ID = 60*24*7, Title = "Every Week"},
                new { ID = 60*24*7*31, Title = "Every Month"},
            };

            ViewBag.NotificationFrequencyOptions = new SelectList(NotificationFrequencyOptions, "ID", "Title", subscription.NotificationFrequencyMins);

            //var matchingItems = new UserSubscriptionManager().GetAllSubscriptionMatches();
            //ViewBag.MatchingItems = matchingItems;

            ViewBag.CountryExtendedInfo = JsonConvert.SerializeObject(OCM.Core.Data.CacheManager.GetExtendedCountryInfo());

            if (subscription.FilterSettings == null) subscription.FilterSettings = new UserSubscriptionFilter();
            ViewBag.OperatorList = new MultiSelectList(refDataManager.GetOperators(subscription.CountryID), "ID", "Title", subscription.FilterSettings.OperatorIDs);
            ViewBag.LevelList = new MultiSelectList(coreRefData.ChargerTypes, "ID", "Title", subscription.FilterSettings.LevelIDs);
            ViewBag.ConnectionTypeList = new MultiSelectList(coreRefData.ConnectionTypes, "ID", "Title", subscription.FilterSettings.ConnectionTypeIDs);
            ViewBag.StatusTypeList = new MultiSelectList(coreRefData.StatusTypes, "ID", "Title", subscription.FilterSettings.StatusTypeIDs);
            ViewBag.UsageTypeList = new MultiSelectList(coreRefData.UsageTypes, "ID", "Title", subscription.FilterSettings.UsageTypeIDs);
        }
        public ActionResult SubscriptionEdit(int? id)
        {
            var subscription = new UserSubscription();
            var userId = int.Parse(Session["UserID"].ToString());
            if (id != null)
            {
                subscription = new UserSubscriptionManager().GetUserSubscription(userId, (int)id);
            }
            else
            {
                LocationLookupResult locationGuess = PerformLocationGuess(true);

                subscription.UserID = userId;
                subscription.NotificationFrequencyMins = 60 * 24 * 7;//default to 1 week
                subscription.DistanceKM = 100;

                if (locationGuess != null && locationGuess.Country_Code != null)
                {
                    subscription.CountryID = locationGuess.CountryID;
                    subscription.Country = new Country { ID = (int)locationGuess.CountryID, Title = locationGuess.Country_Name, ISOCode = locationGuess.Country_Code };
                }
                else
                {
                    //default to UK
                    subscription.CountryID = 1;
                    subscription.Country = new ReferenceDataManager().GetCountryByISO("GB");
                }
                subscription.Latitude = null;
                subscription.Longitude = null;
                subscription.IsEnabled = true;
                subscription.NotifyComments = true;
                subscription.NotifyPOIAdditions = true;
                subscription.NotifyPOIUpdates = true;

                if (locationGuess != null && locationGuess.SuccessfulLookup)
                {
                    if (locationGuess.CountryID != null) subscription.CountryID = locationGuess.CountryID;
                    if (locationGuess.Latitude != null && locationGuess.Longitude != null)
                    {
                        subscription.Latitude = locationGuess.Latitude;
                        subscription.Longitude = locationGuess.Longitude;
                    }
                }

                subscription.Title = "Charging Locations Near Me";
            }

            PopulateSubscriptionEditorViewBag(subscription);
            return View("SubscriptionEdit", subscription);
        }