예제 #1
0
        public async Task <T> GetEntityAsync <T>(IApiContext apiContext, object id, string listName)
        {
            var entityResource = new EntityResource(apiContext);
            var listFQN        = ValidateListName(listName);

            try
            {
                var jobject = await entityResource.GetEntityAsync(listFQN, id.ToString());

                if (jobject == null)
                {
                    return(default(T));
                }
                return(jobject.ToObject <T>(SerializerSettings));
            }
            catch (AggregateException ae)
            {
                if (ae.InnerException.GetType() == typeof(ApiException))
                {
                    throw;
                }
                var aex = (ApiException)ae.InnerException;
                _logger.Error(aex.Message, aex);
                throw aex;
            }

            return(default(T));
        }
        public async Task AddUpdateExtensionLinkAsync(int tenantId, SubnavLink subnavLink, LinkType?type = null)
        {
            var apiContext = new ApiContext(tenantId);

            subnavLink.AppId = await GetAppId(apiContext);

            var location = subnavLink.ParentId.ToString();

            if (type.HasValue)
            {
                if (_newAdmingMappings.ContainsKey(location) && type == LinkType.Menu)
                {
                    location = _newAdmingMappings[location];
                }
                subnavLink.Location = string.Format("{0}{1}", location.ToLower(), type.ToString().ToLower());
                if (!subnavLink.DisplayMode.HasValue)
                {
                    subnavLink.DisplayMode = DisplayMode.Modal;
                }
            }


            var entityResource     = new EntityResource(apiContext);
            var tenantSettingsJobj = await entityResource.GetEntityAsync("tenantadminsettings@mozu", "global");

            TenantAdminSettings tenantSettings = null;

            if (tenantSettingsJobj != null)
            {
                tenantSettings = tenantSettingsJobj.ToObject <TenantAdminSettings>();
            }

            if (tenantSettings != null && tenantSettings.EnableBetaAdmin)
            {
                //validate combo
                if (type.HasValue && type.Value == LinkType.Menu && !_validBurgerMenus.Contains(location))
                {
                    throw new Exception("Invalid Parent option for Menu type. Valid options are " + _validBurgerMenus.Aggregate((x, y) => x + "," + y));
                }
                if (type.HasValue && (type.Value == LinkType.Edit || type.Value == LinkType.Index) && !_validGridEditItems.Contains(location))
                {
                    throw new Exception("Invalid Parent option for " + type.ToString() + " type. Valid options are " + _validGridEditItems.Aggregate((x, y) => x + "," + y));
                }

                subnavLink.ParentId = null;
            }

            await AddUpdateSubNavLink(apiContext, subnavLink);
        }
예제 #3
0
        public async Task UpdateEvent(int tenantId, AubEvent aubEvent)
        {
            try
            {
                _apiContext = new ApiContext(tenantId);

                var entityResource = new EntityResource(_apiContext);

                var entity = await entityResource.GetEntityAsync(_listFullName, aubEvent.Id);

                if (entity != null)
                {
                    await entityResource.UpdateEntityAsync(JObject.FromObject(aubEvent), _listFullName, aubEvent.Id);
                }
                else
                {
                    await AddEvent(tenantId, aubEvent);
                }
            }
            catch (ApiException ex)
            {
                _logger.Error(ex.Message, ex);
            }
        }
예제 #4
0
        public void AddEvent()
        {
            var eventItem = new EventItem
            {
                EntityId = "567",
                EventId = "def",
                Id = "PK123",
                Topic = "order.opened",
                QueuedDateTime = DateTime.Now,
                Status = EventStatus.Pending.ToString()
            };


            var listFullName = $"EventQueue@{_appSetting.Namespace}";

            try
            {
                var entityResource = new EntityResource(_apiContext);

                var entity = entityResource.GetEntityAsync(listFullName, eventItem.Id).Result;
                if (entity == null)
                {

                    var result = entityResource.InsertEntityAsync(JObject.FromObject(eventItem), listFullName).Result;
                }
            }
            catch (ApiException ex)
            {
                if (ex.ErrorCode.Trim() == "ITEM_ALREADY_EXISTS")
                {
                    throw new Exception("Item exists");
                }
                throw;
            }

        }
예제 #5
0
        public async Task <bool> AddEvent(int tenantId, AubEvent aubEvent)
        {
            if (!aubEvent.IsApproved())
            {
                return(false);
            }
            var correlationId = String.Empty;
            var errorCode     = String.Empty;

            try
            {
                _apiContext = new ApiContext(tenantId);
                var entityResource = new EntityResource(_apiContext);

                var entity = await entityResource.GetEntityAsync(_listFullName, aubEvent.Id);

                if (entity == null)
                {
                    //    var aubEvnt = entity.ToObject<AubEvent>();

                    //    if (aubEvnt.Status == EventStatus.Processed.ToString()) return false;

                    //    aubEvnt.Status = EventStatus.Pending.ToString();
                    //    aubEvnt.QueuedDateTime = DateTime.UtcNow;
                    //    await entityResource.UpdateEntityAsync(JObject.FromObject(aubEvnt), _listFullName, aubEvnt.Id);
                    //}
                    //else
                    //{
                    await entityResource.InsertEntityAsync(JObject.FromObject(aubEvent), _listFullName);
                }
            }
            catch (AggregateException ex)
            {
                ex.Handle(e =>
                {
                    var apiEx = e as ApiException;
                    if (apiEx != null)
                    {
                        if (apiEx.ErrorCode.Trim() == "ITEM_ALREADY_EXISTS")
                        {
                            return(true);
                        }
                        _capturedException = ExceptionDispatchInfo.Capture(apiEx);
                        correlationId      = apiEx.CorrelationId;
                        errorCode          = apiEx.ErrorCode;
                    }
                    else
                    {
                        _capturedException = ExceptionDispatchInfo.Capture(e);
                    }
                    return(true);
                });
            }
            catch (ApiException ex)
            {
                if (ex.ErrorCode.Trim() == "ITEM_ALREADY_EXISTS")
                {
                    return(false);
                }
                _capturedException = ExceptionDispatchInfo.Capture(ex);
                correlationId      = ex.CorrelationId;
                errorCode          = ex.ErrorCode;
            }
            catch (Exception ex)
            {
                _capturedException = ExceptionDispatchInfo.Capture(ex);
            }

            if (_capturedException != null && _capturedException.SourceException.InnerException != null)
            {
                var message = String.Format("{0}. CorrId: {1}, ErrorCode: {2}",
                                            _capturedException.SourceException.Message, correlationId,
                                            errorCode);
                if (_capturedException.SourceException.InnerException != null)
                {
                    _logger.Error(message, _capturedException.SourceException);
                }
            }
            return(false);
        }