Example #1
0
        public async Task SeedAsync()
        {
            try
            {
                await _context.Database.MigrateAsync().ConfigureAwait(false);

                foreach (var culture in Settings.Cultures)
                {
                    if (!_translateService.HasTranslationTable(culture.Code))
                    {
                        _translateService.CreateTranslationTable(culture.Code);
                    }
                }

                if (!_context.Cultures.Any())
                {
                    foreach (var culture in Settings.Cultures)
                    {
                        culture.Localizations = Settings.Cultures.Select(s => new Culture_LocaleViewModel
                        {
                            Name        = culture.Name,
                            CultureCode = s.Code
                        }).ToList();

                        Culture entity = new Culture();
                        culture.GenerateKeys();
                        Mapper.Map <CultureViewModel, Culture>(culture, entity);

                        _context.Cultures.Add(entity);
                        culture.AfterCreateEntity(entity);
                    }
                    _context.SaveChanges();
                }

                if (!await _context.Users.AnyAsync())
                {
                    _logger.LogInformation("Generating inbuilt accounts");

                    const string adminRoleName = "administrator";
                    const string userRoleName  = "user";

                    await EnsureRoleAsync(adminRoleName, "Default administrator", Permissions.GetAll());
                    await EnsureRoleAsync(userRoleName, "Default user", new string[] { });

                    await CreateUserAsync("admin", "123qweR!", "Admin", "Admin", "Admin", "*****@*****.**", new string[] { adminRoleName });
                    await CreateUserAsync("user", "123qweR!", "User", "User", "User", "*****@*****.**", new string[] { userRoleName });

                    _logger.LogInformation("Inbuilt account generation completed");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An error occurred while migrating the database.");
            }
        }
Example #2
0
        public override IActionResult Post([FromBody] CultureViewModel model)
        {
            if (ModelState.IsValid)
            {
                Culture entity = Activator.CreateInstance <Culture>();

                if (model.File != null && model.File.IsValid())
                {
                    model.Icon = model.Code + System.IO.Path.GetExtension(model.File.FileName);
                    model.File.Save
                    (
                        model.Icon,
                        Startup.Configuration["Storage:Images"],
                        _appEnvironment
                    );
                }

                model.GenerateKeys();
                entity = Mapper.Map <Culture>(model);

                if (Service.TryCreate(ref entity))
                {
                    model.AfterCreateEntity(entity);
                    if (!TranslateService.HasTranslationTable(model.Code))
                    {
                        TranslateService.CreateTranslationTable(entity.Code);
                    }
                }

                CultureHelper.ReLoad();

                return(Ok(entity));
            }

            return(BadRequest(OperationType.Create));
        }