Exemplo n.º 1
0
 public IOperationResult <QuizInfo> GetQuizInfo(string quizName)
 {
     return(_dataStorage.GetQuiz(quizName.ToLower())
            .Bind(quiz =>
     {
         return OperationResult.All(new Func <IOperationResult>[]
         {
             () => _imageService.LoadIfNeeded(quiz.LogoImage),
             () => _imageService.LoadIfNeeded(quiz.FinishScreenImage),
             () => _imageService.LoadIfNeeded(quiz.IntroScreenImage)
         })
         .Bind(() => quiz.ToResult());
     }));
 }
Exemplo n.º 2
0
 private IOperationResult <IMongoDatabase> GetDatabase()
 {
     return(OperationResult.All(new Func <IOperationResult <string> >[] {
         () => OperationResult.Try(() => _configurationService.GetString(ConfigurationKey.DbConnectionString)),
         () => OperationResult.Try(() => _configurationService.GetString(ConfigurationKey.DbName))
     })
            .Bind(config => new
     {
         ConnectionString = config[0],
         DbName = config[1]
     }.ToResult())
            .Bind(dbConfig =>
                  OperationResult.Try(() => new MongoClient(dbConfig.ConnectionString).ToResult())
                  .Bind(client => OperationResult.Try(() => client.GetDatabase(dbConfig.DbName).ToResult()))));
 }
Exemplo n.º 3
0
 public IOperationResult <UserResult> GetUserResult(UserAnswer[] userAnswers, string quizName)
 {
     return(OperationResult.Try(() => OperationResult.All(
                                    userAnswers.Select <UserAnswer, Func <IOperationResult <QuestionResult> > >(userAnswer => () =>
     {
         return _dataStorage.GetQuestion(quizName, userAnswer.QuestionId).Bind(questionData =>
                                                                               new QuestionResult
         {
             QuestionText = questionData.Text,
             AnswerResults = Array.ConvertAll(
                 questionData.Answers,
                 y => new AnswerResult
             {
                 AnswerText = y.Text,
                 IsCorrect = y.IsCorrect,
                 IsUserChosen = userAnswer.AnswerIds.Contains(y.AnswerId)
             }),
         }.ToResult());
     })))
            .Bind(questionResults => new UserResult {
         QuestionResults = questionResults
     }.ToResult()));
 }
Exemplo n.º 4
0
        public IOperationResult SendResults(string email, string name, string comment, UserResult result, string quizName)
        {
            return(OperationResult.All(new Func <IOperationResult <string> >[] {
                () => OperationResult.Try(() => _configurationService.GetString(ConfigurationKey.SmtpHost)),
                () => OperationResult.Try(() => _configurationService.GetString(ConfigurationKey.SmtpPort)),
                () => OperationResult.Try(() => _configurationService.GetString(ConfigurationKey.MailingAccountLogin)),
                () => OperationResult.Try(() => _configurationService.GetString(ConfigurationKey.MailingAccountPassword))
            })
                   .Bind(config => OperationResult.Success(new
            {
                Host = config[0],
                Port = config[1],
                Login = config[2],
                Password = config[3]
            }))
                   .Bind(mailSettings => OperationResult.Try(() =>
            {
                return _dataStorage.GetQuiz(quizName)
                .Bind(quiz => OperationResult.Success(quiz.ParticipantMailMessageTemplate))
                .Bind(template =>
                {
                    return OperationResult.All(new Func <IOperationResult <MimeMessage> >[] {
                        // Participant message
                        () =>
                        {
                            var message = new MimeMessage();
                            message.From.Add(new MailboxAddress(template.SenderName, mailSettings.Login));
                            message.To.Add(new MailboxAddress(name, email));
                            message.Subject = template.Subject;
                            message.Body = new TextPart(MimeKit.Text.TextFormat.Html)
                            {
                                Text = template.BodyTemplate.Replace("%%name%%", name).Replace("%%percent-correct%%", result.PercentUserAnswersCorrect.ToString()),
                            };
                            return OperationResult.Success(message);
                        },
                        // Committee message
                        () => GetCommitteeMailText(email, name, comment, result)
                        .Bind(text =>
                        {
                            var message = new MimeMessage();

                            message.From.Add(new MailboxAddress(template.SenderName, mailSettings.Login));
                            message.To.Add(new MailboxAddress(template.SenderName, mailSettings.Login));
                            message.Subject = "Результаты " + name;
                            message.Body = new TextPart(MimeKit.Text.TextFormat.Text)
                            {
                                Text = text
                            };
                            return OperationResult.Success(message);
                        })
                    });
                })
                .Bind(messages =>
                {
                    using (var client = new SmtpClient())
                    {
                        client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                        client.Connect(mailSettings.Host, int.Parse(mailSettings.Port), SecureSocketOptions.SslOnConnect);
                        client.Authenticate(mailSettings.Login, mailSettings.Password);
                        client.Send(messages[0]);
                        client.Send(messages[1]);
                        client.Disconnect(true);
                        return OperationResult.Success();
                    }
                });
            })));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> Create(ProductCreateVM createModel)
        {
            if (createModel?.Product == null)
            {
                ModelState.AddModelError(string.Empty, "Please fill in the required fields");
                createModel = await SetDictionaries(createModel ?? new ProductCreateVM());

                return(View(createModel));
            }

            createModel = await SetDictionaries(createModel);

            if (!ModelState.IsValid)
            {
                if (createModel.Characteristics == null)
                {
                    return(View(createModel));
                }

                ValidateCharacteristics(createModel.Characteristics);

                return(View(createModel));
            }

            OperationResult result = await _productManager.CreateAsync(new ProductCreateDTO
            {
                ProductCharacteristics = createModel.Characteristics,
                Product = new ProductDTO
                {
                    Id          = createModel.Product.Id,
                    CountryId   = createModel.Product.CountryId,
                    CategoryId  = createModel.Product.CategoryId,
                    BrandId     = createModel.Product.BrandId,
                    Description = createModel.Product.Description,
                    Name        = createModel.Product.Name,
                    Price       = createModel.Product.Price,
                    Removed     = createModel.Product.Removed,
                    Weight      = createModel.Product.Weight
                },
                ProductImage = createModel.Image
            });

            // var toastOpt = new NotyOptions
            // {
            //     Timeout = 15000,
            //     ProgressBar = true
            // };
            //
            // if (result.Type == ResultType.Success)
            // {
            //     toastOpt.Timeout = 5000;
            //     _toast.AddSuccessToastMessage("Product successfully created.", toastOpt);
            // }
            // else if (result.Type == ResultType.Warning)
            // {
            //     string warMsg = result.Any(s => !string.IsNullOrWhiteSpace(s))
            //         ? result.BuildMessage()
            //         : "The product has been created with warnings.";
            //     _toast.AddWarningToastMessage(warMsg, toastOpt);
            // }

            if (result.Type == ResultType.Success)
            {
                return(RedirectToAction(nameof(List)));
            }

            if (result.All(string.IsNullOrWhiteSpace))
            {
                result.Append("The product has been created with warnings.");
            }

            AddMessages(result.ToArray());

            return(View(createModel));
        }
Exemplo n.º 6
0
        public async Task <ActionResult> Edit(ProductCreateVM editModel)
        {
            if (editModel?.Product == null)
            {
                ModelState.AddModelError(string.Empty, "Product not found");
                editModel = await SetDictionaries(editModel ?? new ProductCreateVM());

                return(View(editModel));
            }

            editModel = await SetDictionaries(editModel);

            if (!ModelState.IsValid)
            {
                if (editModel.Characteristics == null)
                {
                    return(View(editModel));
                }

                ValidateCharacteristics(editModel.Characteristics);

                return(View(editModel));
            }

            OperationResult result = await _productManager.Edit(new ProductCreateDTO
            {
                ProductCharacteristics = editModel.Characteristics,
                Product = new ProductDTO
                {
                    Id          = editModel.Product.Id,
                    CountryId   = editModel.Product.CountryId,
                    CategoryId  = editModel.Product.CategoryId,
                    BrandId     = editModel.Product.BrandId,
                    Description = editModel.Product.Description,
                    Name        = editModel.Product.Name,
                    Price       = editModel.Product.Price,
                    Removed     = editModel.Product.Removed,
                    Weight      = editModel.Product.Weight
                },
                ProductImage = editModel.Image
            });

            // var toastOpt = new NotyOptions
            // {
            //     Timeout = 15000,
            //     ProgressBar = true
            // };

            // if (result.Type == ResultType.Success)
            // {
            //     _toast.AddSuccessToastMessage("Product successfully edited.", toastOpt);
            // }
            // else if (result.Type == ResultType.Warning)
            // {
            //     string warMsg = result.Any(s => !string.IsNullOrWhiteSpace(s))
            //         ? result.BuildMessage()
            //         : "The product has been edited with warnings.";
            //     _toast.AddWarningToastMessage(warMsg, toastOpt);
            // }
            // else if (result.Type == ResultType.Error)
            // {
            //     string errMsg = result.Any(s => !string.IsNullOrWhiteSpace(s))
            //         ? result.BuildMessage()
            //         : "The product has not been edited due to an unknown error.";
            //     _toast.AddErrorToastMessage(errMsg, toastOpt);
            // }
            if (result.Type == ResultType.Success)
            {
                return(RedirectToAction(nameof(List)));
            }

            if (result.All(string.IsNullOrWhiteSpace))
            {
                string msg = "The product has been edited with warnings.";

                if (result.Type == ResultType.Error)
                {
                    msg = "The product has not been edited due an uknown error.";
                }

                result.Append(msg);
            }

            AddMessages(result.ToArray());

            return(View(editModel));
        }