예제 #1
0
        public async Task <IActionResult> Search()
        {
            IActionResult result = null;

            try
            {
                using ILifetimeScope scope = _container.BeginLifetimeScope();
                SettingsFactory     settingsFactory = scope.Resolve <SettingsFactory>();
                CoreSettings        settings        = settingsFactory.CreateCore(_settings.Value);
                IPurgeWorkerFactory factory         = scope.Resolve <IPurgeWorkerFactory>();
                IMapper             mapper          = MapperConfigurationFactory.CreateMapper();
                result = Ok(
                    (await factory.GetAll(settings))
                    .Select <IPurgeWorker, PurgeWorker>(innerPurgeWorker => mapper.Map <PurgeWorker>(innerPurgeWorker))
                    );
            }
            catch (System.Exception ex)
            {
                using (ILifetimeScope scope = _container.BeginLifetimeScope())
                {
                    await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value);
                }
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddSingleton(MapperConfigurationFactory.CriarMapeamentos().CreateMapper());

            services.AddTransient <IPedidoService, PedidoService>();
            services.AddTransient <IStatusService, StatusService>();

            services.AddSingleton <IConnectionFactory, SqliteConnectionFactory>(provider =>
                                                                                new SqliteConnectionFactory(Configuration.GetConnectionString("InMemorySqliteConnection")));
            services.AddSingleton <IDatabaseBootstrap, DatabaseBootstrap>();
            services.AddSingleton <IPedidoRepository, PedidoRepository>();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "Api de pedidos e status",
                    Description = "Back-end Challenge Mercado Eletrônico",
                    Contact     = new OpenApiContact
                    {
                        Name  = "Kenzo Yuri",
                        Email = string.Empty,
                        Url   = new Uri("https://github.com/kenzo09"),
                    }
                });

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }
예제 #3
0
        public async Task <IActionResult> GetByEmailAddress(string emailAddress)
        {
            IActionResult result = null;

            using (ILifetimeScope scope = _container.BeginLifetimeScope())
            {
                SettingsFactory     settingsFactory = scope.Resolve <SettingsFactory>();
                CoreSettings        settings        = settingsFactory.CreateAccount(_settings.Value);
                IUserFactory        userFactory     = scope.Resolve <IUserFactory>();
                IEnumerable <IUser> users           = await userFactory.GetByEmailAddress(settings, emailAddress);

                IAccountFactory accountFactory = scope.Resolve <IAccountFactory>();
                ConcurrentBag <Task <IEnumerable <IAccount> > > accounts = new ConcurrentBag <Task <IEnumerable <IAccount> > >();
                users.AsParallel().ForAll(user => accounts.Add(accountFactory.GetByUserId(settings, user.UserId)));
                IMapper mapper = MapperConfigurationFactory.CreateMapper();
                result = Ok(
                    (await Task.WhenAll <IEnumerable <IAccount> >(accounts))
                    .SelectMany(results => results)
                    .Where(a => UserCanAccessAccount(a.AccountId))
                    .Select <IAccount, Account>(innerAccount => mapper.Map <Account>(innerAccount))
                    .ToList()
                    );
            }
            return(result);
        }
예제 #4
0
        public async Task <IActionResult> Update(Guid id, [FromBody] Account account)
        {
            IActionResult result = null;

            try
            {
                if (result == null && account == null)
                {
                    result = BadRequest("Missing account data");
                }
                if (result == null && string.IsNullOrEmpty(account.Name))
                {
                    result = BadRequest("Missing account name");
                }
                if (result == null && Guid.Empty.Equals(id))
                {
                    result = BadRequest("Invalid account id");
                }
                if (result == null && !UserCanAccessAccount(id))
                {
                    result = StatusCode(StatusCodes.Status401Unauthorized);
                }
                if (result == null)
                {
                    using (ILifetimeScope scope = _container.BeginLifetimeScope())
                    {
                        SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                        CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                        IAccountFactory accountFactory  = scope.Resolve <IAccountFactory>();
                        IAccount        innerAccount    = await accountFactory.Get(settings, id);

                        if (innerAccount == null)
                        {
                            result = NotFound();
                        }
                        else
                        {
                            IMapper mapper = MapperConfigurationFactory.CreateMapper();
                            mapper.Map <Account, IAccount>(account, innerAccount);
                            IAccountSaver saver = scope.Resolve <IAccountSaver>();
                            await saver.Update(settings, innerAccount);

                            result = Ok(
                                mapper.Map <Account>(innerAccount)
                                );
                        }
                    }
                }
            }
            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);
        }
예제 #5
0
        public async Task <IActionResult> Update([FromRoute] Guid?id, [FromBody] ClientCredentialRequest client)
        {
            IActionResult result = null;

            try
            {
                if (result == null && !id.HasValue || id.Value.Equals(Guid.Empty))
                {
                    result = BadRequest("Missing client id value");
                }
                if (result == null && client == null)
                {
                    result = BadRequest("Missing client data");
                }
                if (result == null && string.IsNullOrEmpty(client.Name))
                {
                    result = BadRequest("Missing client name value");
                }
                if (result == null && !string.IsNullOrEmpty(client.Secret) && client.Secret.Trim().Length < 16)
                {
                    result = BadRequest("Client secret must be at least 16 characters in lenth");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    IClientFactory  clientFactory   = scope.Resolve <IClientFactory>();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                    IClient         innerClient     = await clientFactory.Get(settings, id.Value);

                    if (innerClient == null)
                    {
                        result = NotFound();
                    }
                    if (result == null && !UserCanAccessAccount(innerClient.AccountId))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    if (result == null)
                    {
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        mapper.Map <Client, IClient>(client, innerClient);
                        IClientSaver saver = scope.Resolve <IClientSaver>();
                        await saver.Update(settings, innerClient, client.Secret);

                        result = Ok(mapper.Map <Client>(innerClient));
                    }
                }
            }
            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);
        }
예제 #6
0
 protected void Application_Start()
 {
     MapperConfigurationFactory.CreateConfiguration();
     GlobalConfiguration.Configure(WebApiConfig.Register);
     AreaRegistration.RegisterAllAreas();
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     BundleConfig.RegisterBundles(BundleTable.Bundles);
 }
예제 #7
0
        public async Task <IActionResult> Update([FromRoute] Guid?domainId, [FromRoute] string code, [FromBody] Dictionary <string, string> lookupData)
        {
            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 lookup code parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings    settings        = settingsFactory.CreateCore(_settings.Value);
                    ILookupFactory  factory         = scope.Resolve <ILookupFactory>();
                    ILookup         innerLookup     = null;
                    Func <CoreSettings, ILookupSaver, ILookup, 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)
                    {
                        innerLookup = await factory.GetByCode(settings, domainId.Value, code);
                    }
                    if (result == null && innerLookup == null)
                    {
                        innerLookup = factory.Create(domainId.Value, code);
                        save        = (sttngs, svr, lkup) => svr.Create(sttngs, lkup);
                    }
                    if (result == null && innerLookup != null)
                    {
                        innerLookup.Data = lookupData;
                        await save(settings, scope.Resolve <ILookupSaver>(), innerLookup);

                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(
                            mapper.Map <Lookup>(innerLookup)
                            );
                    }
                }
            }
            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);
        }
예제 #8
0
        public async Task <IActionResult> Update([FromRoute] Guid?id, [FromBody] Domain domain)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing domain id value");
                }
                if (result == null && domain == null)
                {
                    result = BadRequest("Missing domain data");
                }
                if (result == null && string.IsNullOrEmpty(domain.Name))
                {
                    result = BadRequest("Missing domain name value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                    IDomainFactory  domainFactory   = scope.Resolve <IDomainFactory>();
                    IDomain         innerDomain     = await domainFactory.Get(settings, id.Value);

                    if (result == null && innerDomain == null)
                    {
                        result = NotFound();
                    }
                    if (result == null && !UserCanAccessAccount(innerDomain.AccountId))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    if (result == null)
                    {
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        mapper.Map <Domain, IDomain>(domain, innerDomain);
                        IDomainSaver saver = scope.Resolve <IDomainSaver>();
                        await saver.Update(settings, innerDomain);

                        result = Ok(mapper.Map <Domain>(innerDomain));
                    }
                }
            }
            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);
        }
예제 #9
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 lookup code parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    ILookupFactory  factory         = scope.Resolve <ILookupFactory>();
                    if (!(await VerifyDomainAccount(domainId.Value, settingsFactory, _settings.Value, scope.Resolve <IDomainService>())))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    else
                    {
                        ILookup lookup = await factory.GetByCode(settingsFactory.CreateCore(_settings.Value), domainId.Value, code);

                        if (lookup == null)
                        {
                            result = NotFound();
                        }
                        else
                        {
                            IMapper mapper = MapperConfigurationFactory.CreateMapper();
                            result = Ok(
                                (await lookup.GetHistory(settingsFactory.CreateCore(_settings.Value)))
                                .Select <ILookupHistory, LookupHistory>(hist => mapper.Map <LookupHistory>(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);
        }
예제 #10
0
        public async Task <IActionResult> UpdateStatus([FromRoute] Guid?id, [FromBody] Dictionary <string, object> patch)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing id parameter value");
                }
                if (result == null && patch == null)
                {
                    result = BadRequest("Missing patch data");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory     settingsFactory  = scope.Resolve <SettingsFactory>();
                    CoreSettings        settings         = settingsFactory.CreateCore(_settings.Value);
                    IPurgeWorkerFactory factory          = scope.Resolve <IPurgeWorkerFactory>();
                    IPurgeWorker        innerPurgeWorker = await factory.Get(settings, id.Value);

                    if (innerPurgeWorker == null)
                    {
                        result = NotFound();
                    }
                    else
                    {
                        if (patch.ContainsKey("Status"))
                        {
                            innerPurgeWorker.Status = (PurgeWorkerStatus)Convert.ChangeType(patch["Status"], typeof(short));
                        }
                        IPurgeWorkerSaver saver = scope.Resolve <IPurgeWorkerSaver>();
                        await saver.Update(settings, innerPurgeWorker);

                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(mapper.Map <PurgeWorker>(innerPurgeWorker));
                    }
                }
            }
            catch (System.Exception ex)
            {
                using (ILifetimeScope scope = _container.BeginLifetimeScope())
                {
                    await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value);
                }
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
예제 #11
0
        public async Task <IActionResult> Get([FromRoute] Guid?domainId, [FromRoute] long?id)
        {
            IActionResult result = null;

            try
            {
                if (result == null && !id.HasValue)
                {
                    result = BadRequest("Missing exception id parameter value");
                }
                if (result == null && (!domainId.HasValue || domainId.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing domain id prameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory   settingsFactory  = scope.Resolve <SettingsFactory>();
                    CoreSettings      settings         = settingsFactory.CreateCore(_settings.Value);
                    IExceptionFactory exceptionFactory = scope.Resolve <IExceptionFactory>();
                    IException        exception        = await exceptionFactory.Get(settings, id.Value);

                    if (exception != null && !exception.DomainId.Equals(domainId.Value))
                    {
                        exception = null;
                    }
                    if (result == null && !(await VerifyDomainAccount(domainId.Value, settingsFactory, _settings.Value, scope.Resolve <IDomainService>())))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    if (result == null && exception == null)
                    {
                        result = NotFound();
                    }
                    if (result == null && exception != null)
                    {
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(
                            await Map(exception, settings, mapper)
                            );
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
예제 #12
0
        public async Task <IActionResult> Search([FromRoute] Guid?domainId, [FromQuery] DateTime?maxTimestamp = null, [FromQuery] string eventCode = null)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!domainId.HasValue || domainId.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing domain id parameter value");
                }
                if (result == null && !maxTimestamp.HasValue)
                {
                    result = BadRequest("Missing max timestamp parameter value");
                }
                if (result == null && string.IsNullOrEmpty(eventCode))
                {
                    result = BadRequest("Missing event code parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    if (!(await VerifyDomainAccount(domainId.Value, settingsFactory, _settings.Value, scope.Resolve <IDomainService>())))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    else
                    {
                        CoreSettings   settings      = settingsFactory.CreateCore(_settings.Value);
                        IMetricFactory metricFactory = scope.Resolve <IMetricFactory>();
                        IMapper        mapper        = MapperConfigurationFactory.CreateMapper();
                        result = Ok(
                            (await metricFactory.GetTopBeforeTimestamp(settings, domainId.Value, eventCode, maxTimestamp.Value))
                            .Select <IMetric, LogModels.Metric>(innerMetric => mapper.Map <LogModels.Metric>(innerMetric))
                            );
                    }
                }
            }
            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);
        }
예제 #13
0
        public void ConfigureServices(IServiceCollection serviceCollection)
        {
            serviceCollection.AddKsLogging();

            this.AddKsI18N(serviceCollection);

            serviceCollection.AddCommonInfrastructure(this.Configuration);

            serviceCollection.AddPersistenceInfrastructure(this.Configuration);

            MapperConfigurationFactory.GetConfiguration <KsMapsterConfiguration>().Flexible().IgnoreCase();

            serviceCollection.AddTransient <IMapper, KsMapster>();

            this.AddEmailNotifications(serviceCollection);

            serviceCollection.AddTransient <IInitialDataSeedService, InitialDataSeedService>();

            this.AddSwaggerDocumentation(serviceCollection);

            this.AddIdentity(serviceCollection);

            this.AddAuthentication(serviceCollection);

            this.AddAuthorization(serviceCollection);

            serviceCollection.AddErrorHandling();

            this.AddCors(serviceCollection);

            this.AddMvcCore(serviceCollection);

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var loggerFactory = serviceProvider.GetService <ILoggerFactory>();

            loggerFactory.AddDebug();

            using (var scope = serviceCollection.BuildServiceProvider().CreateScope())
            {
                var dbContext         = scope.ServiceProvider.GetService <MainDbContext>();
                var pendingMigrations = dbContext.Database.GetPendingMigrations();
                if (pendingMigrations.Count() > 0)
                {
                    dbContext.Database.Migrate();
                }
            }
        }
예제 #14
0
        public async Task <IActionResult> GetAccountDomain([FromRoute] Guid?id)
        {
            IActionResult result = null;

            try
            {
                if (result == null && !id.HasValue || id.Value.Equals(Guid.Empty))
                {
                    result = BadRequest("Missing domain id value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                    IDomainFactory  domainFactory   = scope.Resolve <IDomainFactory>();
                    IDomain         innerDomain     = await domainFactory.Get(settings, id.Value);

                    if (innerDomain != null && !UserCanAccessAccount(innerDomain.AccountId))
                    {
                        innerDomain = null;
                    }
                    if (innerDomain == null)
                    {
                        result = NotFound();
                    }
                    else
                    {
                        IMapper         mapper         = MapperConfigurationFactory.CreateMapper();
                        AccountDomain   accountDomain  = mapper.Map <AccountDomain>(innerDomain);
                        IAccountFactory accountFactory = scope.Resolve <IAccountFactory>();
                        accountDomain.Account = mapper.Map <Account>(await accountFactory.Get(settings, innerDomain.AccountId));
                        result = Ok(accountDomain);
                    }
                }
            }
            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);
        }
예제 #15
0
        public async Task <IActionResult> Get([FromRoute] Guid?id)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing invitation id parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory        settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings           settings        = settingsFactory.CreateAccount(_settings.Value);
                    IUserInvitationFactory factory         = scope.Resolve <IUserInvitationFactory>();
                    IUserInvitation        innerInvitation = await factory.Get(settings, id.Value);

                    if (innerInvitation != null && !(await UserCanAccessInvitation(settings, innerInvitation)))
                    {
                        innerInvitation = null;
                    }
                    if (innerInvitation == null)
                    {
                        result = NotFound();
                    }
                    else
                    {
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(
                            await Map(mapper, settings, innerInvitation)
                            );
                    }
                }
            }
            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);
        }
예제 #16
0
        public async Task <IActionResult> GetByAccountId([FromRoute] Guid?id, [FromQuery] bool deleted = false)
        {
            IActionResult result = null;

            try
            {
                if (result == null && !id.HasValue || id.Value.Equals(Guid.Empty))
                {
                    result = BadRequest("Missing account id value");
                }
                if (result == null && !UserCanAccessAccount(id.Value))
                {
                    result = StatusCode(StatusCodes.Status401Unauthorized);
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory       settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings          settings        = settingsFactory.CreateAccount(_settings.Value);
                    IDomainFactory        domainFactory   = scope.Resolve <IDomainFactory>();
                    IEnumerable <IDomain> domains;
                    if (deleted)
                    {
                        domains = await domainFactory.GetDeletedByAccountId(settings, id.Value);
                    }
                    else
                    {
                        domains = await domainFactory.GetByAccountId(settings, id.Value);
                    }
                    IMapper mapper = MapperConfigurationFactory.CreateMapper();
                    result = Ok(
                        domains.Select <IDomain, Domain>(d => mapper.Map <Domain>(d))
                        );
                }
            }
            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);
        }
예제 #17
0
        public async Task <IActionResult> Get([FromRoute] Guid?id)
        {
            IActionResult result = null;

            try
            {
                if (result == null && !id.HasValue || id.Value.Equals(Guid.Empty))
                {
                    result = BadRequest("Missing client id value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                    IClientFactory  clientFactory   = scope.Resolve <IClientFactory>();
                    IClient         client          = await clientFactory.Get(settings, id.Value);

                    if (client == null)
                    {
                        result = NotFound();
                    }
                    if (result == null && !UserCanAccessAccount(client.AccountId))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    if (result == null)
                    {
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(
                            mapper.Map <Client>(client)
                            );
                    }
                }
            }
            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);
        }
예제 #18
0
        public async Task <IActionResult> Create([FromBody] Account account)
        {
            IActionResult result = null;

            try
            {
                if (result == null && account == null)
                {
                    result = BadRequest("Missing account data");
                }
                if (result == null && string.IsNullOrEmpty(account.Name))
                {
                    result = BadRequest("Missing account name");
                }
                if (result == null)
                {
                    using (ILifetimeScope scope = _container.BeginLifetimeScope())
                    {
                        SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                        CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                        IUser           user            = await GetUser(scope.Resolve <IUserFactory>(), settings);

                        IAccountFactory accountFactory = scope.Resolve <IAccountFactory>();
                        IAccount        innerAccount   = accountFactory.Create();
                        IMapper         mapper         = MapperConfigurationFactory.CreateMapper();
                        mapper.Map <Account, IAccount>(account, innerAccount);
                        IAccountSaver saver = scope.Resolve <IAccountSaver>();
                        await saver.Create(settings, user.UserId, innerAccount);

                        result = Ok(
                            mapper.Map <Account>(innerAccount)
                            );
                    }
                }
            }
            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);
        }
예제 #19
0
        public async Task <IActionResult> Create([FromBody] LogModels.Metric metric)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!metric.DomainId.HasValue || metric.DomainId.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing domain guid value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    if (!(await VerifyDomainAccountWriteAccess(metric.DomainId.Value, settingsFactory, _settings.Value, scope.Resolve <IDomainService>())))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    if (result == null)
                    {
                        CoreSettings   settings    = settingsFactory.CreateCore(_settings.Value);
                        IMetricFactory factory     = scope.Resolve <IMetricFactory>();
                        IMetric        innerMetric = factory.Create(metric.DomainId.Value, metric.CreateTimestamp, metric.EventCode);
                        IMapper        mapper      = MapperConfigurationFactory.CreateMapper();
                        mapper.Map <LogModels.Metric, IMetric>(metric, innerMetric);
                        IMetricSaver saver = scope.Resolve <IMetricSaver>();
                        await saver.Create(settings, innerMetric);

                        result = Ok(mapper.Map <LogModels.Metric>(innerMetric));
                    }
                }
            }
            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);
        }
예제 #20
0
        public async Task <IActionResult> Search([FromRoute] Guid?domainId, [FromQuery] DateTime?maxTimestamp = null)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!domainId.HasValue || domainId.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing domain id prameter value");
                }
                if (result == null && !maxTimestamp.HasValue)
                {
                    result = BadRequest("Missing max timestamp parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    if (!(await VerifyDomainAccount(domainId.Value, settingsFactory, _settings.Value, scope.Resolve <IDomainService>())))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    else
                    {
                        CoreSettings      settings         = settingsFactory.CreateCore(_settings.Value);
                        IExceptionFactory exceptionFactory = scope.Resolve <IExceptionFactory>();
                        IMapper           mapper           = MapperConfigurationFactory.CreateMapper();
                        return(Ok(
                                   await Task.WhenAll <LogModels.Exception>(
                                       (await exceptionFactory.GetTopBeforeTimestamp(settings, domainId.Value, maxTimestamp.Value))
                                       .Select <IException, Task <LogModels.Exception> >(async innerException => await Map(innerException, settings, mapper))
                                       )));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
예제 #21
0
        public async Task <IActionResult> GetAll()
        {
            IActionResult result = null;

            using (ILifetimeScope scope = _container.BeginLifetimeScope())
            {
                SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                IUser           user            = await GetUser(scope.Resolve <IUserFactory>(), settings);

                IAccountFactory        accountFactory = scope.Resolve <IAccountFactory>();
                IEnumerable <IAccount> accounts       = await accountFactory.GetByUserId(settings, user.UserId);

                IMapper mapper = MapperConfigurationFactory.CreateMapper();
                result = Ok(
                    accounts.Select <IAccount, Account>(innerAccount => mapper.Map <Account>(innerAccount))
                    );
            }
            return(result);
        }
예제 #22
0
        public async Task <IActionResult> Create([FromBody] LogModels.Exception exception)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!exception.DomainId.HasValue || exception.DomainId.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing domain guid value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    if (!(await VerifyDomainAccountWriteAccess(exception.DomainId.Value, settingsFactory, _settings.Value, scope.Resolve <IDomainService>())))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    if (result == null)
                    {
                        CoreSettings      settings       = settingsFactory.CreateCore(_settings.Value);
                        IExceptionFactory factory        = scope.Resolve <IExceptionFactory>();
                        IMapper           mapper         = MapperConfigurationFactory.CreateMapper();
                        List <IException> allExceptions  = new List <IException>();
                        IException        innerException = Map(exception, exception.DomainId.Value, exception.CreateTimestamp, factory, mapper, allExceptions);
                        IExceptionSaver   saver          = scope.Resolve <IExceptionSaver>();
                        await saver.Create(settings, allExceptions.ToArray());

                        result = Ok(
                            await Map(innerException, settings, mapper)
                            );
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
예제 #23
0
        public async Task <IActionResult> GetUsers([FromRoute] Guid?accountId)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!accountId.HasValue || Guid.Empty.Equals(accountId.Value)))
                {
                    result = BadRequest("Missing account id parameter value");
                }
                if (result == null && !UserCanAccessAccount(accountId.Value))
                {
                    result = StatusCode(StatusCodes.Status401Unauthorized);
                }
                if (result == null)
                {
                    using (ILifetimeScope scope = _container.BeginLifetimeScope())
                    {
                        SettingsFactory     settingsFactory = scope.Resolve <SettingsFactory>();
                        CoreSettings        settings        = settingsFactory.CreateAccount(_settings.Value);
                        IUserFactory        userFactory     = scope.Resolve <IUserFactory>();
                        IEnumerable <IUser> innerUsers      = await userFactory.GetByAccountId(settings, accountId.Value);

                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(
                            innerUsers.Select <IUser, User>(innerUser => mapper.Map <User>(innerUser))
                            );
                    }
                }
            }
            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);
        }
예제 #24
0
        public async Task <IActionResult> Get([FromRoute] Guid?id)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing id parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory     settingsFactory  = scope.Resolve <SettingsFactory>();
                    CoreSettings        settings         = settingsFactory.CreateCore(_settings.Value);
                    IPurgeWorkerFactory factory          = scope.Resolve <IPurgeWorkerFactory>();
                    IPurgeWorker        innerPurgeWorker = await factory.Get(settings, id.Value);

                    if (innerPurgeWorker == null)
                    {
                        result = NotFound();
                    }
                    else
                    {
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(mapper.Map <PurgeWorker>(innerPurgeWorker));
                    }
                }
            }
            catch (System.Exception ex)
            {
                using (ILifetimeScope scope = _container.BeginLifetimeScope())
                {
                    await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value);
                }
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
예제 #25
0
        public void RegisterTypes(ContainerBuilder builder)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var services = assembly.GetExportedTypes()
                           .Where(x => x.FullName.EndsWith("Service") && x.IsInterface == false)
                           .ToList();

            services.ForEach(x =>
            {
                builder.RegisterType(x)
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();
            });

            builder.Register(x => MapperConfigurationFactory.CreateConfiguration(assembly))
            .As <MapperConfiguration>()
            .SingleInstance();

            builder.Register(x => { return(x.Resolve <MapperConfiguration>().CreateMapper()); })
            .As <IMapper>()
            .SingleInstance();
        }
예제 #26
0
        public async Task <IActionResult> Create([FromRoute] Guid?accountId, [FromBody] UserInvitation userInvitation)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!accountId.HasValue || accountId.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing account id parameter value");
                }
                if (result == null && !UserCanAccessAccount(accountId.Value))
                {
                    result = StatusCode(StatusCodes.Status401Unauthorized);
                }
                if (result == null && userInvitation == null)
                {
                    result = BadRequest("Missing user invitation body");
                }
                if (result == null && string.IsNullOrEmpty(userInvitation.EmailAddress))
                {
                    result = BadRequest("Missing user email address value");
                }
                if (result == null && !userInvitation.ExpirationTimestamp.HasValue)
                {
                    userInvitation.ExpirationTimestamp = DateTime.UtcNow.AddDays(7);
                }
                if (result == null && userInvitation.ExpirationTimestamp.HasValue && userInvitation.ExpirationTimestamp.Value.ToUniversalTime() <= DateTime.UtcNow)
                {
                    result = BadRequest("Invalid expiration timestamp in the past");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                    IAccountFactory accountFactory  = scope.Resolve <IAccountFactory>();
                    IAccount        account         = await accountFactory.Get(settings, accountId.Value);

                    if (account == null)
                    {
                        result = NotFound();
                    }
                    else
                    {
                        IEmailAddress emailAddress = await GetEmailAddress(
                            settings,
                            scope.Resolve <IEmailAddressFactory>(),
                            scope.Resolve <IEmailAddressSaver>(),
                            userInvitation.EmailAddress
                            );

                        IUserInvitationFactory invitationFactory = scope.Resolve <IUserInvitationFactory>();
                        IUserInvitation        innerInvitation   = invitationFactory.Create(account, emailAddress);
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        mapper.Map <UserInvitation, IUserInvitation>(userInvitation, innerInvitation);
                        innerInvitation.Status = UserInvitationStatus.Created;
                        IUserInvitationSaver saver = scope.Resolve <IUserInvitationSaver>();
                        await saver.Create(settings, innerInvitation);

                        result = Ok(await Map(mapper, settings, innerInvitation));
                    }
                }
            }
            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);
        }
 public void Setup()
 {
     sut = new MapperConfigurationFactory();
 }
예제 #28
0
        public async Task <IActionResult> Update([FromRoute] Guid?id, [FromBody] UserInvitation userInvitation)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing id parameter value");
                }
                if (result == null && userInvitation == null)
                {
                    result = BadRequest("Missing user invitation body");
                }
                if (result == null && !userInvitation.ExpirationTimestamp.HasValue)
                {
                    result = BadRequest("Missing expiration timestamp value");
                }
                if (result == null && !userInvitation.Status.HasValue)
                {
                    result = BadRequest("Missing status value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory        settingsFactory   = scope.Resolve <SettingsFactory>();
                    CoreSettings           settings          = settingsFactory.CreateAccount(_settings.Value);
                    IAccountFactory        accountFactory    = scope.Resolve <IAccountFactory>();
                    IUserInvitationFactory invitationFactory = scope.Resolve <IUserInvitationFactory>();
                    IUserInvitation        innerInvitation   = await invitationFactory.Get(settings, id.Value);

                    if (innerInvitation != null && !(await UserCanAccessInvitation(settings, innerInvitation)))
                    {
                        innerInvitation = null;
                    }
                    if (innerInvitation == null)
                    {
                        result = NotFound();
                    }
                    else
                    {
                        if (DateTime.UtcNow < innerInvitation.ExpirationTimestamp &&
                            innerInvitation.Status != UserInvitationStatus.Cancelled &&
                            userInvitation.Status == (short)UserInvitationStatus.Completed &&
                            !UserTokenHasAccount(innerInvitation.AccountId))
                        {
                            await AddAccountUser(scope.Resolve <IUserFactory>(), scope.Resolve <IAccountSaver>(), settings, innerInvitation.AccountId);
                        }
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        mapper.Map <UserInvitation, IUserInvitation>(userInvitation, innerInvitation);
                        IUserInvitationSaver saver = scope.Resolve <IUserInvitationSaver>();
                        await saver.Update(settings, innerInvitation);

                        result = Ok(await Map(mapper, settings, innerInvitation));
                    }
                }
            }
            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);
        }
예제 #29
0
        public async Task <IActionResult> UpdateDeleted([FromRoute] Guid?id, [FromBody] Dictionary <string, string> patch)
        {
            bool          deleted = default;
            IActionResult result  = null;

            try
            {
                if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing domain id value");
                }
                if (result == null && patch == null)
                {
                    result = BadRequest("Missing patch data");
                }
                if (result == null && !patch.ContainsKey("Deleted"))
                {
                    result = BadRequest("Missing deleted patch value");
                }
                if (result == null && !bool.TryParse(patch["Deleted"], out deleted))
                {
                    result = BadRequest("Invalid deleted patch value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                    CoreSettings    settings        = settingsFactory.CreateAccount(_settings.Value);
                    IDomainFactory  domainFactory   = scope.Resolve <IDomainFactory>();
                    IDomain         innerDomain;
                    if (!deleted)
                    {
                        innerDomain = await domainFactory.GetDeleted(settings, id.Value);
                    }
                    else
                    {
                        innerDomain = await domainFactory.Get(settings, id.Value);
                    }
                    if (result == null && innerDomain == null)
                    {
                        result = NotFound();
                    }
                    if (result == null && !UserCanAccessAccount(innerDomain.AccountId))
                    {
                        result = StatusCode(StatusCodes.Status401Unauthorized);
                    }
                    if (result == null)
                    {
                        innerDomain.Deleted = deleted;
                        IDomainSaver saver = scope.Resolve <IDomainSaver>();
                        await saver.Update(settings, innerDomain);

                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(mapper.Map <Domain>(innerDomain));
                    }
                }
            }
            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);
        }