Ejemplo n.º 1
0
        public async Task <IActionResult> Update([FromRoute] Guid?domainId, [FromRoute] string code, [FromBody] dynamic itemData)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!domainId.HasValue || Guid.Empty.Equals(domainId.Value)))
                {
                    result = BadRequest("Missing domain id parameter value");
                }
                if (result == null && string.IsNullOrEmpty(code))
                {
                    result = BadRequest("Missing item code parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings    settings        = settingsFactory.CreateCore(_settings.Value);
                    IItemFactory    factory         = scope.Resolve <IItemFactory>();
                    IItem           innerItem       = null;
                    Func <CoreSettings, IItemSaver, IItem, Task> save = (sttngs, svr, lkup) => svr.Update(sttngs, lkup);
                    if (!(await VerifyDomainAccount(domainId.Value, settingsFactory, _settings.Value, scope.Resolve <IDomainService>())))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    if (result == null)
                    {
                        innerItem = await factory.GetByCode(settings, domainId.Value, code);
                    }
                    if (result == null && innerItem == null)
                    {
                        innerItem = factory.Create(domainId.Value, code);
                        save      = (sttngs, svr, lkup) => svr.Create(sttngs, lkup);
                    }
                    if (result == null && innerItem != null)
                    {
                        innerItem.Data = itemData;
                        await save(settings, scope.Resolve <IItemSaver>(), innerItem);

                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(
                            mapper.Map <Item>(innerItem)
                            );
                    }
                }
            }
            catch (Exception ex)
            {
                using (ILifetimeScope scope = _container.BeginLifetimeScope())
                {
                    await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value);
                }
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetHistoryByCode([FromRoute] Guid?domainId, [FromRoute] string code)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!domainId.HasValue || Guid.Empty.Equals(domainId.Value)))
                {
                    result = BadRequest("Missing domain id parameter value");
                }
                if (result == null && string.IsNullOrEmpty(code))
                {
                    result = BadRequest("Missing item code parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    IItemFactory    factory         = scope.Resolve <IItemFactory>();
                    if (!(await VerifyDomainAccount(domainId.Value, settingsFactory, _settings.Value, scope.Resolve <IDomainService>())))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    else
                    {
                        IItem item = await factory.GetByCode(settingsFactory.CreateCore(_settings.Value), domainId.Value, code);

                        if (item == null)
                        {
                            result = NotFound();
                        }
                        else
                        {
                            IMapper mapper = MapperConfigurationFactory.CreateMapper();
                            result = Ok(
                                (await item.GetHistory(settingsFactory.CreateCore(_settings.Value)))
                                .Select <IItemHistory, ItemHistory>(hist => mapper.Map <ItemHistory>(hist))
                                );
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (ILifetimeScope scope = _container.BeginLifetimeScope())
                {
                    await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value);
                }
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }