public ActionResult Index(GetSendDistributiveMessagesPageInformationCommand command)
 {
     return View(new GetHospitalNoticesMessageInformationCommandAnswer
     {
         Token = command.Token.Value,
         Text = command.Text,
         Title = command.Title
     });
 }
        public ActionResult SendMessages(GetSendDistributiveMessagesPageInformationCommand command)
        {
            var answer = _noticesService.GetSendDistributiveMessagesPageInformation(command);

            if (answer.Errors.Any())
            {
                var model = new GetHospitalNoticesMessageInformationCommandAnswer
                {
                    Token = answer.Token,
                    Title = answer.Title,
                    Text = answer.Text,
                    Errors = answer.Errors
                };
                ViewBag.Errors = answer.Errors;
                return View("Index", model);
            }

            return RedirectToAction("Index", "Home", new GetHospitalNoticesMessageInformationCommandAnswer
            {
                Token = command.Token.Value,
                Text = command.Text,
                Title = command.Title
            });
        }
        private List<CommandAnswerError> ValidateGetSendDistributiveMessagesPageInformationCommand(
            GetSendDistributiveMessagesPageInformationCommand command)
        {
            var errors = new List<CommandAnswerError>();

            if (string.IsNullOrWhiteSpace(command.Text))
            {
                errors.Add(new CommandAnswerError
                {
                    FieldName = "Текст",
                    Title = "Текст не может пустым"
                });
            }

            if (string.IsNullOrWhiteSpace(command.Title))
            {
                errors.Add(new CommandAnswerError
                {
                    FieldName = "Заголовок",
                    Title = "Заголовок не может быть пустым"
                });
            }

            return errors;
        }
        public GetSendDistributiveMessagesPageInformationCommandAnswer GetSendDistributiveMessagesPageInformation(
            GetSendDistributiveMessagesPageInformationCommand command)
        {
            var errors = ValidateGetSendDistributiveMessagesPageInformationCommand(command);

            if (errors.Any())
            {
                return new GetSendDistributiveMessagesPageInformationCommandAnswer
                {
                    Token = command.Token.Value,
                    Title = command.Title,
                    Text = command.Text,
                    Errors = errors
                };
            }

            var user = _tokenManager.GetUserByToken(command.Token);

            var messages = ((IDbSet<UserStorageModel>) _userRepository.GetModels())
                .Include(model => model.UserType)
                .Where(model => model.UserType.UserType == UserType.ClinicUser)
                .ToList()
                .Select(model => new MessageStorageModel
                {
                    Date = DateTime.Now,
                    IsRead = false,
                    MessageType = MessageType.UserMessage,
                    ShowStatus = TwoSideShowStatus.Showed,
                    Text = command.Text,
                    Title = command.Title,
                    UserFromId = user.Id,
                    UserToId = model.Id
                });

            foreach (var message in messages)
            {
                _messageRepository.Add(message);
            }
            _messageRepository.SaveChanges();

            return new GetSendDistributiveMessagesPageInformationCommandAnswer
            {
                Token = command.Token.Value,
                Title = command.Title,
                Text = command.Text,
                Errors = errors
            };
        }