Exemplo n.º 1
0
        public override IViewComponentResult Invoke(object data = null)
        {
            var r = R;

            try
            {
                UpdatesResponseModel updates = null;
                //do we need to fetch?
                if (_systemSettings.LatestUpdatesFetched.IsNullEmptyOrWhiteSpace() || DateTime.UtcNow.Subtract(_systemSettings.LatestFetchedOn).TotalHours >=
                    _systemSettings.UpdateFetchIntervalInHours)
                {
                    //we need to
                    updates = _requestProvider.Get <UpdatesResponseModel>(UpdatesFetchUrl, new NameValueCollection()
                    {
                        { "storeDomain", _generalSettings.StoreDomain },
                        { "storeName", _generalSettings.StoreName }
                    });
                    if (updates != null)
                    {
                        if (!updates.Success && _systemSettings.LatestUpdatesFetched.IsNullEmptyOrWhiteSpace())
                        {
                            return(R.Fail.ComponentResult);
                        }
                        _systemSettings.LatestUpdatesFetched = _dataSerializer.Serialize(updates);
                        _systemSettings.LatestFetchedOn      = DateTime.UtcNow;
                        _settingService.Save(_systemSettings, CurrentStore.Id);
                    }
                }

                updates = updates ?? (_systemSettings.LatestUpdatesFetched.IsNullEmptyOrWhiteSpace()
                              ? null
                              : _dataSerializer.DeserializeAs <UpdatesResponseModel>(_systemSettings.LatestUpdatesFetched));

                if (updates != null)
                {
                    r.With("evencartUpdates", updates.FeedItems);
                }
                return(r.Success.ComponentResult);
            }
            catch
            {
                return(r.Success.ComponentResult);
            }
        }