예제 #1
0
        public override IActionResult Delete(int id)
        {
            var entity = Service.ByID(id);

            if (entity == null)
            {
                return(NotFound());
            }

            entity.Icon = entity.Icon.GetFullPath("Storage:Images");
            if (!string.IsNullOrEmpty(entity.Icon))
            {
                if (System.IO.File.Exists(entity.Icon))
                {
                    System.IO.File.Delete(entity.Icon);
                }
            }

            CultureViewModel model = Activator.CreateInstance <CultureViewModel>();

            model.GetKeys(entity);

            if (Service.TryDelete(entity))
            {
                model.AfterDeleteEntity(entity);
                if (!TranslateService.HasTranslationTable(model.Code))
                {
                    TranslateService.DeleteTranslationTable(model.Code);
                }
            }

            CultureHelper.ReLoad();

            return(Ok(OperationType.Delete));
        }
예제 #2
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.");
            }
        }
예제 #3
0
        public override IActionResult Put([FromBody] CultureViewModel model)
        {
            if (ModelState.IsValid)
            {
                Culture entity = Activator.CreateInstance <Culture>();
                entity = Service.AsObjectQuery()
                         .AsNoTracking()
                         .FirstOrDefault(f => EqualityComparer <int> .Default.Equals(f.Id, model.Id));

                if (model.File != null && model.File.IsValid())
                {
                    if (!string.IsNullOrEmpty(model.Icon))
                    {
                        if (System.IO.File.Exists(model.Icon))
                        {
                            System.IO.File.Delete(model.Icon);
                        }
                    }

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

                model.GetKeys(entity);
                Mapper.Map <CultureViewModel, Culture>(model, entity);

                if (Service.TryUpdate(ref entity))
                {
                    model.AfterUpdateEntity(entity);
                    if (!TranslateService.HasTranslationTable(entity.Code))
                    {
                        TranslateService.UpdateTranslationTable(entity.Code);
                    }
                }

                CultureHelper.ReLoad();

                return(Ok(entity));
            }

            return(BadRequest(OperationType.Update));
        }
예제 #4
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));
        }