Beispiel #1
0
        public Task Handle(CreateConfigCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.CompletedTask);
            }

            var Config = new Config(Guid.NewGuid(), message.SystemEnable, message.Currency, message.ReferalBonus);

            if (_ConfigRepository.GetExist())
            {
                Bus.RaiseEvent(new DomainNotification(message.MessageType, "The Config has already been taken."));
                return(Task.CompletedTask);
            }

            _ConfigRepository.Add(Config);

            if (Commit())
            {
                Bus.RaiseEvent(new ConfigCreatedEvent(Config.Id, Config.SystemEnable, Config.Currency, Config.ReferalBonus));
            }

            return(Task.CompletedTask);
        }
Beispiel #2
0
        public async Task <bool> ChangeBackground(IFormFile file)
        {
            if (file == null)
            {
                return(false);
            }

            var    extension   = Path.GetExtension(file.FileName).ToLower();
            var    uploadImage = $"main-bg{extension}";
            string pathImages  = @"images";
            string folder      = Path.Combine(_webHostEnvironment.WebRootPath, pathImages);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            string filePath = Path.Combine(folder, uploadImage);

            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }

            var item = await _configRepository.FindAll().FirstOrDefaultAsync();

            if (item != null)
            {
                item.Background = Path.Combine(pathImages, uploadImage);
            }
            else
            {
                var model = new Config();
                model.Background = Path.Combine(pathImages, uploadImage);
                _configRepository.Add(model);
            }

            try
            {
                using (FileStream fs = System.IO.File.Create(filePath))
                {
                    await file.CopyToAsync(fs);

                    await fs.FlushAsync();
                }
                await _configRepository.Save();

                return(true);
            }
            catch (System.Exception)
            {
                return(false);
            }
        }