示例#1
0
        public ActionResult Create(string entityName, FormCollection collection)
        {
            var entity = _admin.GetEntity(entityName);

            if (entity == null)
            {
                return(RedirectToAction("NotFound", new { entityName }));
            }

            try
            {
                var savedId = _entityService.Create(entity, collection, Request.Files);
                if (savedId != null)
                {
                    _notificator.Success(IlaroAdminResources.AddSuccess, entity.Verbose.Singular);

                    return(SaveOrUpdateSucceed(entityName, savedId));
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                _notificator.Error(ex.Message);
            }

            var model = new EntityCreateModel
            {
                Entity           = entity,
                PropertiesGroups = _entityService.PrepareGroups(entity.CreateRecord(collection, Request.Files))
            };

            return(View(model));
        }
示例#2
0
        public string Create(Entity entity)
        {
            var existingItem = _source.GetRecord(entity, entity.Key.Value.AsObject);

            if (existingItem != null)
            {
                _notificator.Error(IlaroAdminResources.EntityAlreadyExist);
                return(null);
            }

            var id = _creator.Create(entity);

            return(id);
        }
示例#3
0
        public object ExecuteWithChanges(DbCommand cmd, ChangeInfo changeInfo)
        {
            object result;

            using (var conn = DB.OpenConnection())
                using (var tx = conn.BeginTransaction())
                {
                    try
                    {
                        cmd.Connection  = conn;
                        cmd.Transaction = tx;
                        result          = cmd.ExecuteScalar();

                        if (AdminInitialise.IsChangesEnabled)
                        {
                            var changeCmd = CreateChangeCommand(changeInfo, result.ToString());
                            changeCmd.Connection  = conn;
                            changeCmd.Transaction = tx;
                            changeCmd.ExecuteNonQuery();
                        }

                        tx.Commit();
                    }
                    catch (Exception ex)
                    {
                        result = null;
                        _notificator.Error(ex.Message);
                        tx.Rollback();
                    }
                }

            return(result);
        }
示例#4
0
        public Entity GetEntityWithData(string entityName, string key)
        {
            var entity = AdminInitialise.EntitiesTypes
                         .FirstOrDefault(x => x.Name == entityName);

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

            var item = GetRecord(entity, entity.Key.Value.ToObject(key));

            if (item == null)
            {
                _notificator.Error(IlaroAdminResources.EntityNotExist);
                return(null);
            }

            var propertiesDict = item as IDictionary <string, object>;

            foreach (var property in entity.CreateProperties(false))
            {
                property.Value.Raw =
                    propertiesDict.ContainsKey(property.ColumnName) ?
                    propertiesDict[property.ColumnName] :
                    null;
            }

            return(entity);
        }
示例#5
0
        public Entity GetEntityWithData(Entity entity, params string[] key)
        {
            var keys = new object[key.Length];

            for (int i = 0; i < key.Length; i++)
            {
                keys[i] = entity.Key[i].Value.ToObject(key[i]);
            }
            var item = GetRecord(entity, keys);

            if (item == null)
            {
                _notificator.Error(IlaroAdminResources.EntityNotExist);
                return(null);
            }

            foreach (var property in entity.CreateProperties(false))
            {
                property.Value.Raw =
                    item.ContainsKey(property.ColumnName.Undecorate()) ?
                    item[property.ColumnName.Undecorate()] :
                    null;
            }

            return(entity);
        }
示例#6
0
        public string Create(
            Entity entity,
            FormCollection collection,
            HttpFileCollectionBase files)
        {
            entity.Fill(collection, files);
            if (_validator.Validate(entity) == false)
            {
                _notificator.Error("Not valid");
                return(null);
            }
            var existingRecord = _source.GetRecord(entity, entity.Key.Select(x => x.Value.AsObject));

            if (existingRecord != null)
            {
                _notificator.Error(IlaroAdminResources.EntityAlreadyExist);
                return(null);
            }

            var propertiesWithUploadedFiles = _filesHandler.Upload(entity);

            var id = _creator.Create(entity, () => _changeDescriber.CreateChanges(entity));

            if (id.IsNullOrWhiteSpace() == false)
            {
                _filesHandler.ProcessUploaded(propertiesWithUploadedFiles);
            }
            else
            {
                _filesHandler.DeleteUploaded(propertiesWithUploadedFiles);
            }

            return(id);
        }
示例#7
0
        public ActionResult Create(string entityName, FormCollection collection)
        {
            ViewBag.IsAjaxRequest = HttpContext.Request.IsAjaxRequest();

            var entity = AdminInitialise.EntitiesTypes
                         .FirstOrDefault(x => x.Name == entityName);

            if (entity == null)
            {
                throw new NoNullAllowedException("entity is null");
            }

            entity.Fill(collection, Request.Files);
            if (_validator.Validate(entity))
            {
                try
                {
                    var savedId = _entityService.Create(entity);
                    if (savedId != null)
                    {
                        _notificator.Success(IlaroAdminResources.AddSuccess, entity.Verbose.Singular);

                        if (Request["ContinueEdit"] != null)
                        {
                            return(RedirectToAction(
                                       "Edit",
                                       new
                            {
                                entityName,
                                key = entity.Key.Value.ToObject(savedId)
                            }));
                        }
                        if (Request["AddNext"] != null)
                        {
                            return(RedirectToAction("Create", new { entityName }));
                        }

                        return(RedirectToAction("Index", "Entities", new { entityName }));
                    }
                    _notificator.Error(IlaroAdminResources.UncaughtError);
                }
                catch (Exception ex)
                {
                    var message = ex.Message;
                    if (ex.InnerException != null)
                    {
                        message += "<br />" + ex.InnerException.Message;
                    }
                    _notificator.Error(message);
                }
            }

            var model = new EntityCreateModel
            {
                Entity           = entity,
                PropertiesGroups = _entityService.PrepareGroups(entity)
            };

            return(View(model));
        }
示例#8
0
        private async Task ProcessConvertAsync(Message message)
        {
            try
            {
                var tempFile = Path.GetTempFileName();
                var fileName = Path.ChangeExtension(tempFile, ".mp4");
                using var file = File.Create(fileName);

                try
                {
                    Notificator.VideoHandled(message);

                    var video = await _client.GetFileAsync(message.Video.FileId);

                    await _client.DownloadFileAsync(video.FilePath, file);

                    file.Flush();
                    file.Close();

                    using var videoStream = await _videoСonverter.ConvertAsync(fileName);

                    Notificator.VideoConverted(message, videoStream.Length);

                    var caption = message.GetFromFirstName() + _settings.Messages.CaptionPhrases.GetRandom();

                    var inputFile = new InputOnlineFile(videoStream, _settings.ResultFileName);
                    await _client.SendVideoAsync(message.Chat.Id, inputFile,
                                                 caption : caption,
                                                 replyToMessageId : message.MessageId);

                    Notificator.VideoSendSuccess(message, caption);
                }
                catch (Exception exc)
                {
                    Notificator.Error("Exception: " + exc.Message);
                    await _client.SendTextMessageAsync(message.Chat.Id,
                                                       $"{message.GetFromFirstName()},\n" +
                                                       $"{exc.Message}\n" +
                                                       $"{exc.StackTrace}",
                                                       replyToMessageId : message.MessageId);
                }
                finally
                {
                    File.Delete(file.Name);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#9
0
        public EntityRecord GetEntityRecord(Entity entity, params string[] key)
        {
            var keys = new object[key.Length];

            for (int i = 0; i < key.Length; i++)
            {
                keys[i] = new PropertyValue(entity.Keys[i]).ToObject(key[i]);
            }
            var item = GetRecord(entity, keys);

            if (item == null)
            {
                _notificator.Error(IlaroAdminResources.EntityNotExist);
                return(null);
            }

            return(entity.CreateRecord(item));
        }
示例#10
0
        private void ProcessVoice(Message message)
        {
            Task.Run(async() =>
            {
                var fileName = Path.ChangeExtension(Path.GetTempFileName(), ".mp4");
                var fs       = File.Create(fileName);

                try
                {
                    Notificator.VoiceHandled(message);

                    var file = await _client.GetFileAsync(message.Voice.FileId);
                    await _client.DownloadFileAsync(file.FilePath, fs);
                    fs.Close();

                    var mediaInfo = FFmpeg.GetMediaInfo(fileName).Result;

                    IStream audioStream = mediaInfo.AudioStreams.FirstOrDefault()
                                          ?.SetCodec(AudioCodec.pcm_s16le)
                                          ?.SetChannels(1)
                                          ?.SetSampleRate(16000);

                    var outputPath = @"C:\test\.test.wav"; //Path.ChangeExtension(Path.GetTempFileName(), ".wav");

                    await FFmpeg.Conversions.New().AddStream(audioStream).SetOutput(outputPath).Start();

                    //var text = await _voiceСonverter.ConvertAsync(fs.Name);
                    //Notificator.VoiceConverted(message, text.Length);

                    //await _client.SendTextMessageAsync(message.Chat.Id, PhrasesHelper.RandomVoice + $"\n{text}.", replyToMessageId: message.MessageId);
                }
                catch (Exception exc)
                {
                    Notificator.Error(exc.Message);
                    await _client.SendTextMessageAsync(message.Chat.Id,
                                                       message.GetFromFirstName() + _settings.Messages.ErrorUploadMessage,
                                                       replyToMessageId: message.MessageId);
                }
                finally
                {
                    File.Delete(fs.Name);
                }
            });
        }
示例#11
0
        public string Create(Entity entity)
        {
            try
            {
                var cmd = CreateCommand(entity);

                var result = _executor
                             .ExecuteWithChanges(cmd, new ChangeInfo(entity.Name, EntityChangeType.Insert));

                return(result.ToStringSafe());
            }
            catch (Exception ex)
            {
                _notificator.Error("");
                return(null);
            }
            finally
            {
                entity.ClearPropertiesValues();
            }
        }
示例#12
0
        public bool Delete(Entity entity, IDictionary <string, DeleteOption> options)
        {
            try
            {
                var cmd = CreateCommand(entity, options);

                var result = (int)_executor
                             .ExecuteWithChanges(cmd, new ChangeInfo(entity.Name, EntityChangeType.Delete));

                return(result > 0);
            }
            catch (Exception ex)
            {
                _notificator.Error("");
                return(false);
            }
            finally
            {
                entity.ClearPropertiesValues();
            }
        }
示例#13
0
        public bool Update(Entity entity)
        {
            try
            {
                var cmd = CreateCommand(entity);

                // TODO: get info about changed properties
                var result = _executor
                             .ExecuteWithChanges(cmd, new ChangeInfo(entity.Name, EntityChangeType.Update));

                return(result != null);
            }
            catch (Exception ex)
            {
                _notificator.Error("");
                return(true);
            }
            finally
            {
                entity.ClearPropertiesValues();
            }
        }
示例#14
0
        public string Create(
            Entity entity,
            FormCollection collection,
            IFormFileCollection files)
        {
            var entityRecord = entity.CreateRecord(collection, files, x => x.OnCreateDefaultValue);

            if (_validator.Validate(entityRecord) == false)
            {
                _notificator.Error(IlaroAdminResources.RecordNotValid);
                return(null);
            }
            var existingRecord = _source.GetRecord(
                entity,
                entityRecord.Keys.Select(value => value.AsObject).ToArray());

            if (existingRecord != null)
            {
                _notificator.Error(IlaroAdminResources.EntityAlreadyExist);
                return(null);
            }

            var propertiesWithUploadedFiles = _filesHandler.Upload(
                entityRecord,
                x => x.OnCreateDefaultValue);

            var id = _creator.Create(
                entityRecord,
                () => _changeDescriber.CreateChanges(entityRecord));

            if (id.IsNullOrWhiteSpace() == false)
            {
                _filesHandler.ProcessUploaded(propertiesWithUploadedFiles);
            }
            else
            {
                _filesHandler.DeleteUploaded(propertiesWithUploadedFiles);
            }

            return(id);
        }