예제 #1
0
        public static StoreMessageModel ConvertModifyMessageModelToStoreMessage(ModifyMessageModel model)
        {
            //NOTE: model.sendDateTime will be in local time. We need it in the timezone the user has selected
            var correctDateTime = model.SendDateTime;

            if (correctDateTime.HasValue)
            {
                var timezone = System.TimeZoneInfo.FindSystemTimeZoneById(model.TimeZone);
                var newDate  = System.TimeZoneInfo.ConvertTimeToUtc(model.SendDateTime.Value, timezone);


                correctDateTime = newDate;
            }

            return(new StoreMessageModel
            {
                UrlOnly = model.UrlOnly,
                Text = model.Text,
                Url = model.Url,
                MessageType = model.MessageType,
                Title = model.Title,
                Recipients = "",
                SendDate = correctDateTime,
                TimeZone = model.TimeZone
            });
        }
예제 #2
0
        public ActionResult CreateText()
        {
            ModifyMessageModel model = new ModifyMessageModel()
            {
                UrlOnly = false
            };

            return(View("Create", model));
        }
예제 #3
0
        public ActionResult Edit(ModifyMessageModel model)
        {
            var updateRecipients = false;

            ModelState.Remove(model.UrlOnly ? "Text" : "Url");
            ProcessRecipientsCsvUpload csvData = new ProcessRecipientsCsvUpload
            {
                PostedFile = model.UploadedFile,
            };

            if (!csvData.CheckCsvOk()) // check csv upload before modelstate so can process irrespectively
            {
                ModelState.AddModelError("UploadedFile", csvData.Error);
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View(model));
            }
            if (csvData.Empty && !model.DeleteRecipients && !ModelState.IsValid)
            {
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View(model));
            }
            var msg = Db.CmsMessages.FirstOrDefault(m => m.Id == model.Id);

            if (msg == null)
            {
                return(new HttpNotFoundResult("Message item was not found"));
            }
            var store = MessageLayer.ConvertModifyMessageModelToStoreMessage(model);

            if (!csvData.Empty)
            {
                updateRecipients = true;
                store.Recipients = csvData.Recipients;
            }
            else if (model.DeleteRecipients)
            {
                updateRecipients = true;
                store.Recipients = "";
            }
            else
            {
                store.Recipients = model.RecipientsField;
            }
            try
            {
                MessageLayer.UpdateMessage(Db, store, msg);
            }
            catch (Exception)
            {
                //FIXME: add logging here
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View(model));
            }
            return(updateRecipients ? RedirectToAction("Edit", new { id = model.Id }) : RedirectToAction("Index"));
        }
        public override async Task <int> UpdateAsync(ModifyMessageModel message, CancellationToken cancellationToken = default)
        {
            var messageEntity = await GetAsync(message.Id, cancellationToken);

            if (messageEntity == null)
            {
                return(0);
            }

            if (messageEntity.Key != message.Key)
            {
                var exist = await ExistAsync(message.Key, cancellationToken);

                if (exist)
                {
                    throw new Exception($"The configuration key for the {_source.Environment} environment and {_source.NameSpace} NameSpace already exists");
                }
            }

            var dateTime = DateTime.Now;

            object[] sqlParams =
            {
                new SqlParameter("@Id",          message.Id),
                new SqlParameter("@Key",         message.Key),
                new SqlParameter("@Value",       message.Value),
                new SqlParameter("@Description", message.Description),
                new SqlParameter("@UpdateTime",  dateTime)
            };
            var sql = $@"UPDATE {GetTableName()} 
SET [Key]=@Key,[Value]=@Value,[Description]=@Description,[UpdateTime]=@UpdateTime
WHERE [Id]=@Id";

            using var connection = new SqlConnection(_source.DbConnection);
            var executeRows = connection.ExecuteNonQuery(sql, sqlParams);

            if (executeRows <= 0)
            {
                return(executeRows);
            }
            var events = new ConcurrentQueue <Event>();

            events.Enqueue(new Event(EventType.Deleted, messageEntity.Key, messageEntity.Value));
            events.Enqueue(new Event(EventType.Add, message.Key, message.Value));
            InvokeEvent(events);
            return(await Task.FromResult(executeRows));
        }
예제 #5
0
        public async Task <int> UpdateAsync(ModifyMessageModel message, CancellationToken cancellationToken = default)
        {
            object[] sqlParams =
            {
                new NpgsqlParameter("@Id",         message.Id),
                new NpgsqlParameter("@Roles",      message.Roles ?? new List <string>()),
                new NpgsqlParameter("@UpdateTime", DateTime.Now)
            };
            var sql = $@"UPDATE {GetTableName()} 
SET Roles=@Roles,UpdateTime=@UpdateTime
WHERE Id=@Id";

            await using var connection = new NpgsqlConnection(_options.Value.DbConnection);
            var executeRows = connection.ExecuteNonQuery(sql, sqlParams);

            return(await Task.FromResult(executeRows));
        }
예제 #6
0
        public async Task UpdateAsync(ModifyMessageModel dto, HttpContext context, CancellationToken cancellationToken)
        {
            try
            {
                dto ??= new ModifyMessageModel();
                await _configurationStorage.UpdateAsync(dto, cancellationToken);

                var apiResult = new ApiResult();
                await context.Response.WriteAsync(apiResult.ToJson(), Encoding.UTF8, cancellationToken);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                var apiResult = new ApiResult(500, e.Message);
                await context.Response.WriteAsync(apiResult.ToJson(), Encoding.UTF8, cancellationToken);
            }
        }
예제 #7
0
        private ModifyMessageModel MessageToModifyMessageModel(CmsMessage msg)
        {
            var result = new ModifyMessageModel
            {
                UrlOnly         = msg.UrlOnly,
                Text            = msg.Text,
                Url             = msg.Url,
                Id              = msg.Id,
                MessageType     = msg.MessageType,
                Title           = msg.Title,
                RecipientsField = msg.Recipients,
                SendDateTime    = msg.SendDate,
                TimeZone        = msg.TimeZone,
                RecipientList   = MessageLayer.FieldToRecipientList(msg.Recipients)
            };

            return(result);
        }
예제 #8
0
        private ActionResult DoCreate(ModifyMessageModel model)
        {
            ModelState.Remove(model.UrlOnly ? "Text" : "Url");
            ProcessRecipientsCsvUpload csvData = new ProcessRecipientsCsvUpload
            {
                PostedFile = model.UploadedFile,
            };

            if (!csvData.CheckCsvOk()) // check csv upload before modelstate so can process irrespectively
            {
                ModelState.AddModelError("UploadedFile", csvData.Error);
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View("Create", model));
            }
            if (csvData.Empty && !ModelState.IsValid)
            {
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View("Create", model));
            }
            var store = MessageLayer.ConvertModifyMessageModelToStoreMessage(model);

            store.UserId = User.Identity.GetUserId();
            if (!csvData.Empty)
            {
                store.Recipients = csvData.Recipients;
            }
            //model.Recipients;
            if (!MessageLayer.AddMessage(Db, store))
            {
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View("Create", model));
            }
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Edit", new { id = store.NewId }));
            }
            return(RedirectToAction("Index"));
        }
예제 #9
0
 public ActionResult CreateText(ModifyMessageModel model)
 {
     return(DoCreate(model));
 }