Beispiel #1
0
        /// <summary>
        /// Sets the temporary data message and state
        /// </summary>
        /// <param name="tempData">Temporary data dictionary to use</param>
        /// <param name="state">State to set</param>
        /// <param name="message">Message to set</param>
        public static void SetTemporaryMessage(ITempDataDictionary tempData, MessageState state, string message)
        {
            if (tempData == null)
            {
                throw new ArgumentNullException(nameof(tempData));
            }

            if (tempData.ContainsKey("MessageState"))
            {
                tempData["MessageState"] = state;
            }
            else
            {
                tempData.Add("MessageState", state);
            }

            if (tempData.ContainsKey("Message"))
            {
                tempData["Message"] = message;
            }
            else
            {
                tempData.Add("Message", message);
            }
        }
Beispiel #2
0
        public static IHtmlContent ShowNotification <TModel>(
            this IHtmlHelper <TModel> helper,
            ITempDataDictionary tempData)
        {
            var stringBuilder = new StringBuilder();

            if (tempData.ContainsKey("notification"))
            {
                var message = tempData.ContainsKey("noty_message") ? tempData["noty_message"] : null;
                var type    = tempData.ContainsKey("noty_type") ? tempData["noty_type"] : "information";

                stringBuilder.AppendLine("<script>");
                stringBuilder.AppendLine("$(document).ready(function() {");
                stringBuilder.AppendLine("new Noty({");
                stringBuilder.AppendLine($"type: '{type}',");
                stringBuilder.AppendLine($"text: '{message}',");
                stringBuilder.AppendLine("layout: 'bottomCenter',");
                stringBuilder.AppendLine("theme: 'relax',");
                stringBuilder.AppendLine("timeout: 3000");
                stringBuilder.AppendLine("}).show();");
                stringBuilder.AppendLine("});");
                stringBuilder.AppendLine("</script>");

                tempData.Remove("notification");
            }

            return(new HtmlString(stringBuilder.ToString()));
        }
Beispiel #3
0
 private static void AddMessage(ITempDataDictionary data, string type, string message)
 {
     if (!data.ContainsKey(NotificationData))
     {
         data.Add(NotificationData, $"{type}|{message}");
     }
 }
Beispiel #4
0
 public static void CreateAlertTempData(this ITempDataDictionary tempData)
 {
     if (!tempData.ContainsKey(Alerts))
     {
         tempData[Alerts] = "";
     }
 }
        public static T Get <T>(this ITempDataDictionary tempData, string key)
        {
            if (tempData.ContainsKey(key))
            {
                var v = tempData[key];

                if (Reflect.OnTypes.IsSimple(typeof(T)))
                {
                    var obj = Convert.ChangeType(v, typeof(T));
                    return((T)obj);
                }

                if (v is T)
                {
                    return((T)v);
                }

                if (v is string && typeof(T) != typeof(string))
                {
                    var obj = JsonConvert.DeserializeObject <T>((string)v);
                    return(obj);
                }
            }

            return(default(T));
        }
Beispiel #6
0
        public static Cart FILLTEMPDATA(WebshopDbContext context, string user, ITempDataDictionary TempData, bool filltemp)
        {
            Cart cart = context.Carts.Include(x => x.Items).ThenInclude(x => x.Product).FirstOrDefault(x => x.Buyer == user);

            if (cart == null)
            {
                cart       = new Cart();
                cart.Buyer = user;
                context.Carts.Add(cart);
            }
            if (filltemp)
            {
                int price = cart.Items.Sum(x => x.Count * x.Product.Price);
                int count = cart.Items.Sum(x => x.Count);
                if (TempData.ContainsKey("Count"))
                {
                    TempData["Count"]     = count;
                    TempData["CartPrice"] = price;
                }
                else
                {
                    TempData.Add("Count", count);
                    TempData.Add("CartPrice", price);
                }
            }

            return(cart);
        }
        private bool CreateNotification(ExceptionContext context, out ITempDataDictionary tempData)
        {
            tempData = _tempDataDictionaryFactory.GetTempData(context.HttpContext);
            CreateNotification(NotificationHelpers.AlertType.Error, tempData, context.Exception.Message);

            return(!tempData.ContainsKey(NotificationHelpers.NotificationKey));
        }
        public static bool TryLoadData <T>(
            this ITempDataDictionary tempData,
            out T data)
        {
            data = default(T);

            string entryKey = typeof(T).Name;

            if (tempData.ContainsKey(entryKey))
            {
                // get value from temp data
                string value = tempData[entryKey] as string;

                // decode data
                byte[] bytes = Convert.FromBase64String(value);

                //decompress
                value = Decompress(bytes);

                // deserialize
                data = JsonConvert.DeserializeObject <T>(value);

                // remove entry from temp data
                tempData.Remove(entryKey);

                return(true);
            }

            return(false);
        }
        public IEnumerable <string> GetAllMessages(bool peek = false)
        {
            object messages;

            if (!peek)
            {
                if (_tempData.ContainsKey(MessageQueryKey))
                {
                    messages = _tempData[MessageQueryKey];
                }
                else
                {
                    return(Enumerable.Empty <string>());
                }
            }
            else
            {
                messages = _tempData.Peek(MessageQueryKey);
            }

            if (messages == default)
            {
                return(Enumerable.Empty <string>());
            }

            if (messages is IEnumerable <string> enumerable)
            {
                return(enumerable);
            }

            throw InvalidType(messages);
        }
        protected void CreateNotification(NotificationHelpers.AlertType type, ITempDataDictionary tempData, string message, string title = "")
        {
            var toast = new NotificationHelpers.Alert
            {
                Type    = type,
                Message = message,
                Title   = title
            };

            var alerts = new List <NotificationHelpers.Alert>();

            if (tempData.ContainsKey(NotificationHelpers.NotificationKey))
            {
                alerts = JsonConvert.DeserializeObject <List <NotificationHelpers.Alert> >(tempData[NotificationHelpers.NotificationKey].ToString());
                tempData.Remove(NotificationHelpers.NotificationKey);
            }

            alerts.Add(toast);

            var settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            };

            var alertJson = JsonConvert.SerializeObject(alerts, settings);

            tempData.Add(NotificationHelpers.NotificationKey, alertJson);
        }
        public static bool TryGetAlertList(this ITempDataDictionary tempdata, out List <Alert> alerts)
        {
            alerts = null;

            return(tempdata.ContainsKey(AlertKey) &&
                   tempdata[AlertKey] is string listString &&
                   listString.TryParseJson(out alerts));
        }
Beispiel #12
0
 public static void Add <T>(this ITempDataDictionary tempData, string key, T value) where T : class
 {
     if (tempData.ContainsKey(key))
     {
         tempData.Remove(key);
     }
     tempData[key] = JsonSerializer.Serialize(value);
 }
 public static T Get <T>(this ITempDataDictionary self, string key) where T : class
 {
     if (!self.ContainsKey(key))
     {
         return(null);
     }
     return(JsonConvert.DeserializeObject <T>(self[key] as string));
 }
 public static IList <Alert> GetAlerts(this ITempDataDictionary tempData)
 {
     if (!tempData.ContainsKey(AlertsKey))
     {
         tempData.Set(AlertsKey, new List <Alert>());
     }
     return(tempData.Get <List <Alert> >(AlertsKey));
 }
Beispiel #15
0
        private static List <Alert> GetAlerts(this ITempDataDictionary tempData)
        {
            if (!tempData.ContainsKey(Alerts))
            {
                tempData.Put(Alerts, new List <Alert>());
            }

            return(tempData.Get <List <Alert> >(Alerts));
        }
Beispiel #16
0
 public static T Get <T>(this ITempDataDictionary tempData, string key) where T : class
 {
     if (tempData.ContainsKey(key))
     {
         string value = tempData[key].ToString();
         return(JsonConvert.DeserializeObject <T>(value));
     }
     return(null);
 }
        public static string GetViewModelProp(this ITempDataDictionary tempData)
        {
            if (!tempData.ContainsKey(JSON_VIEW_MODEL_PROP))
            {
                tempData[JSON_VIEW_MODEL_PROP] = null;
            }

            return((string)tempData[JSON_VIEW_MODEL_PROP]);
        }
Beispiel #18
0
        private void Message(Enumerations.Enumerations.AjaxFlashMessageType type, string message)
        {
            if (_tempData.ContainsKey(Key))
            {
                _tempData.Remove(Key);
            }

            _tempData.Add(Key, string.Format(AlertTemplate, type.ToString().ToLower(), message));
        }
        public static List <Alert> GetAlerts(this ITempDataDictionary tempData)
        {
            if (!tempData.ContainsKey(AlertsKey))
            {
                return(new List <Alert>());
            }

            return(JsonConvert.DeserializeObject <List <Alert> >(tempData[AlertsKey].ToString()));
        }
        public static List <Alert> GetAlerts(this ITempDataDictionary tempData)
        {
            if (!tempData.ContainsKey(Alerts))
            {
                tempData[Alerts] = new List <Alert>();
            }

            return((List <Alert>)tempData[Alerts]);
        }
        public static string ShowErrorMessage(this ITempDataDictionary tempData)
        {
            if (tempData.ContainsKey(GlobalConstants.ErrorMessageKey))
            {
                return(tempData[GlobalConstants.ErrorMessageKey].ToString());
            }

            return("");
        }
Beispiel #22
0
        public static List <Alert> DeserializeAlerts(this ITempDataDictionary tempData, string alertKeyName)
        {
            var alerts = new List <Alert>();

            if (tempData.ContainsKey(alertKeyName))
            {
                alerts = JsonConvert.DeserializeObject <List <Alert> >(tempData[alertKeyName].ToString());
            }
            return(alerts);
        }
Beispiel #23
0
        public static T Peek <T>(this ITempDataDictionary tempData, string key) where T : class
        {
            object o = null;

            if (tempData.ContainsKey(key))
            {
                o = tempData.Peek(key);
            }
            return(o == null ? null : JsonConvert.DeserializeObject <T>((string)o));
        }
        internal static void AddToastNotification(this ITempDataDictionary tempData, string type, string message)
        {
            var notifications = tempData.ContainsKey(TempDataKey)
                ? (List <KeyValuePair <string, string> >)tempData[TempDataKey]
                : new List <KeyValuePair <string, string> >();

            notifications.Add(new KeyValuePair <string, string>(type, message));

            tempData[TempDataKey] = notifications;
        }
Beispiel #25
0
 public static List <AlertModel> GetAlerts(ITempDataDictionary tempData)
 {
     return(tempData.ContainsKey(_tempDataKey)
         ? JsonConvert.DeserializeObject <List <AlertModel> >((string)tempData[_tempDataKey])
         : new List <AlertModel>());
     //когато напишат да може да се сериализират други неща, освен прости типове
     //return tempData.ContainsKey(_tempDataKey)
     //    ? (List<AlertModel>)tempData[_tempDataKey]
     //    : new List<AlertModel>();
 }
Beispiel #26
0
        public static List <Alert> GetAlerts(this ITempDataDictionary tempData)
        {
            if (tempData.ContainsKey(Alert.TempDataKey))
            {
                string       json   = (string)tempData[Alert.TempDataKey];
                List <Alert> alerts = JsonConvert.DeserializeObject <List <Alert> >(json);
                return(alerts);
            }

            return(new List <Alert>());
        }
Beispiel #27
0
        public static void AddAlert(this ITempDataDictionary tempData, Alert alert)
        {
            if (!tempData.ContainsKey(Alerts))
            {
                tempData.Set(Alerts, new List <Alert>());
            }
            var alerts = tempData.Get <List <Alert> >(Alerts);

            alerts.Add(alert);
            tempData.Set(Alerts, alerts);
        }
        public static T Get <T>(this ITempDataDictionary tempData, string key)
        {
            if (!tempData.ContainsKey(key))
            {
                return(default(T));
            }

            var value = tempData[key] as string;

            return(value == null ? default(T) : JsonConvert.DeserializeObject <T>(value));
        }
Beispiel #29
0
        public static T GetValue <T>(this ITempDataDictionary tempData, string key)
        {
            if (!tempData.ContainsKey(key))
            {
                return(default(T));
            }

            var value = tempData[key];

            return((T)value);
        }
 /// <summary>
 /// Creates error message which is sent with redirect
 /// </summary>
 /// <param name="tempData"></param>
 /// <param name="message"></param>
 public static void AddErrorMessage(this ITempDataDictionary tempData, string message)
 {
     if (tempData.ContainsKey("ErrorMessage"))
     {
         tempData["ErrorMessage"] = message;
     }
     else
     {
         tempData.Add("ErrorMessage", message);
     }
 }