public async Task <IActionResult> Email(ContactViewModel model)
        {
            ViewData["Title"]    = _localizer["PageTitle"];
            ViewData["PageDesc"] = _localizer["PageDesc"];

            // create email for admin
            var msg = $"<p>User: {model?.User.UserName} <br />Email: {model.User.Email} <br />Last Name: {model.User.LastName} <br />First Name: {model.User.FirstName}</p> " +
                      $"<p>Subject: {model.Subject}</p><p>Message: {model.Message.Replace(Environment.NewLine, "<br />", StringComparison.CurrentCultureIgnoreCase)}</p>";

            // send email
            await _emailAgent.SendEmailAsync(_appSettings.SMTP.FromEmail, "Contact Page of WMS", _appSettings.SMTP.AdminEmail, model.Subject, msg, true, null).ConfigureAwait(false);

            if (!ModelState.IsValid)
            {
                Warning(_localizer["ContactGeneralError"], true);
                return(View("Index", model));
            }

            Success(_localizer["ContactSuccess"], true);
            model.Subject = string.Empty;
            model.Message = string.Empty;
            return(View("Index", model));
        }
Beispiel #2
0
        public async Task <IActionResult> Add(AddRecipeViewModel model)
        {
            ViewData["Title"]    = _localizer["PageTitle"];
            ViewData["PageDesc"] = _localizer["PageDesc"];

            var getCategoriesQuery = _queryFactory.CreateCategoriesQuery();
            var cList = await getCategoriesQuery.ExecuteAsync().ConfigureAwait(false);

            var getVarietiesQuery = _queryFactory.CreateVarietiesQuery();
            var vList             = await getVarietiesQuery.ExecuteAsync().ConfigureAwait(false);

            var getYeastQuery = _yeastQueryFactory.CreateYeastsQuery();
            var yList         = await getYeastQuery.ExecuteAsync().ConfigureAwait(false);

            var batchTempQuery = _journalQueryFactory.CreateBatchTempUOMQuery();
            var uomTempList    = await batchTempQuery.ExecuteAsync().ConfigureAwait(false);

            var batchSugarQuery = _journalQueryFactory.CreateBatchSugarUOMQuery();
            var uomSugarList    = await batchSugarQuery.ExecuteAsync().ConfigureAwait(false);

            // must be logged in to continue
            var submittedBy = await UserManagerAgent.GetUserAsync(User).ConfigureAwait(false);

            if (submittedBy == null)
            {
                var addRecipeModel = _modelFactory.CreateAddRecipeModel(cList, vList, yList, uomSugarList, uomTempList, model);
                Warning(_localizer["NoLogIn"], false);
                return(View(addRecipeModel));
            }

            // using model validation attributes, if model state says errors do nothing
            if (!ModelState.IsValid)
            {
                var addRecipeModel = _modelFactory.CreateAddRecipeModel(cList, vList, yList, uomSugarList, uomTempList, model);
                Warning(_localizer["AddGeneralError"], true);
                return(View(addRecipeModel));
            }

            ICode variety = null;

            if (int.TryParse(model?.VarietyId, out int varietyId))
            {
                variety = vList.FirstOrDefault(c => c.Id == varietyId);
            }

            ICode category = null;

            if (variety != null && variety.ParentId.HasValue)
            {
                category = cList.FirstOrDefault(c => c.Id == variety.ParentId.Value);
            }

            YeastDto yeast = null;

            if (int.TryParse(model?.YeastId, out int yeastId))
            {
                yeast = yList.FirstOrDefault(y => y.Id == yeastId);
            }

            Business.Journal.Dto.TargetDto target = null;

            if (model.Target.HasTargetData())
            {
                target = new Business.Journal.Dto.TargetDto
                {
                    EndSugar   = model.Target.EndingSugar,
                    pH         = model.Target.pH,
                    StartSugar = model.Target.StartingSugar,
                    TA         = model.Target.TA,
                    Temp       = model.Target.FermentationTemp,
                };

                if (model.Target.StartSugarUOM.HasValue)
                {
                    target.StartSugarUom = uomSugarList.FirstOrDefault(u => u.Id == model.Target.StartSugarUOM.Value);
                }
                if (model.Target.EndSugarUOM.HasValue)
                {
                    target.EndSugarUom = uomSugarList.FirstOrDefault(u => u.Id == model.Target.EndSugarUOM.Value);
                }
                if (model.Target.TempUOM.HasValue)
                {
                    target.TempUom = uomTempList.FirstOrDefault(u => u.Id == model.Target.TempUOM.Value);
                }
            }
            // convert add model to recipe dto
            var recipeDto = new Business.Recipe.Dto.RecipeDto
            {
                Description   = model.Description,
                Enabled       = false,
                Hits          = 0,
                Ingredients   = model.Ingredients,
                Instructions  = model.Instructions,
                NeedsApproved = true,
                Rating        = null,
                SubmittedBy   = submittedBy.Id,
                Target        = target,
                Title         = model.Title,
                Yeast         = yeast,
                Variety       = variety
            };

            //recipeDto.Id = 1;  // for testing only
            var updateRecipesCommand = _commandsFactory.CreateRecipesCommand();

            recipeDto = await updateRecipesCommand.AddAsync(recipeDto).ConfigureAwait(false);


            // process uploaded files
            if (model.Images != null)
            {
                var           updateImageCommand = _commandsFactory.CreateImageCommand();
                long          maxFileSizeBytes   = 512000;
                List <string> allowedExtensions  = new List <string> {
                    ".jpg", ".jpeg", ".bmp", ".png", ".gif"
                };
                int maxUploads  = 4;
                int uploadCount = 1;

                foreach (FormFile file in model.Images)
                {
                    // Max File Size per Image: 500 KB
                    if (file.Length > maxFileSizeBytes)
                    {
                        continue;
                    }
                    // Allowed Image Extensions: .jpg | .gif | .bmp | .jpeg | .png ONLY
                    var ext = Path.GetExtension(file.FileName);
                    if (!allowedExtensions.Any(e => e.Equals(ext, StringComparison.OrdinalIgnoreCase)))
                    {
                        continue;
                    }
                    // Pictures Max 4
                    if (uploadCount > maxUploads)
                    {
                        break;
                    }

                    using MemoryStream ms = new MemoryStream();
                    file.OpenReadStream().CopyTo(ms);
                    var imageData = await ResizeImage(ms.ToArray(), 360, 480).ConfigureAwait(false);

                    var thumbData = await ResizeImage(ms.ToArray(), 100, 150).ConfigureAwait(false);

                    var imageDto = _dtoFactory.CreateNewImageFile(recipeDto.Id, file.FileName, file.Name, imageData, thumbData, file.Length, file.ContentType);
                    await updateImageCommand.AddAsync(imageDto).ConfigureAwait(false);

                    uploadCount++;
                }
            }

            var subjectLine = "There is a new recipe is in the approval queue.";
            var bodyContent = "A new recipe has been submitted and needs approved.";

            // notify admin that new recipe is in the approval queue
            await _emailAgent.SendEmailAsync(_appSettings.SMTP.FromEmail, _appSettings.SMTP.FromEmail, _appSettings.SMTP.AdminEmail,
                                             subjectLine, bodyContent, false, null).ConfigureAwait(false);

            // tell user good job and clear or go to thank you page
            ModelState.Clear();
            var addNewRecipeModel = _modelFactory.CreateAddRecipeModel(cList, vList, yList, uomSugarList, uomTempList);

            addNewRecipeModel.User = submittedBy;

            Success(_localizer["AddSuccess"], true);

            return(View(addNewRecipeModel));
        }