Exemple #1
0
        public JsonResult AddNewProduct(ProductDto productDto)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { Success = "False", responseText = "Wystąpił błąd. Wprowadź poprawne dane." }));
            }

            var productId = Guid.NewGuid();

            try
            {
                var productGallery =
                    galleryProvider.SaveProductGallery(new Gallery(productId, Request.Files, productDto.MainPhoto))
                    .ToArray();
                var mainPicture = productGallery.Single(x => x.IsMainPhoto);
                var product     = new Product
                {
                    IdProduct       = productId,
                    Name            = productDto.Name,
                    IdCategory      = productDto.IdCategory,
                    Description     = productDto.Description,
                    IdMainPicture   = mainPicture.IdPicture,
                    InsertDate      = DateTime.Now,
                    ProductGallery  = productGallery,
                    UrlFriendlyName = productDto.Name
                };
                validator.Validate(product);

                if (Request.Url == null)
                {
                    throw new ArgumentNullException(nameof(Request.Url));
                }

                var baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
                product.ProductUrl = $"{baseUrl}/Produkty/{productDto.CategoryName}/{product.UrlFriendlyName}";

                bus.ExecuteCommand(new SaveProductCommand(product));
                var newsletterClients = bus.RunQuery(new GetNewletterClientsQuery());

                Task.Run(async() => { await SendEmailsAboutNewProductAsync(product, newsletterClients); })
                .ContinueWith(tsk =>
                {
                    if (tsk.Exception != null)
                    {
                        foreach (var exception in tsk.Exception.InnerExceptions)
                        {
                            logger.Error(exception);
                        }
                    }
                }, TaskContinuationOptions.OnlyOnFaulted);

                return(Json(new { Success = "True", responseText = "Dodano produkt.", Url = product.ProductUrl }));
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return
                    (Json(new { Success = "False", responseText = "Wystąpił błąd. Spróbuj ponownie lub odśwież stronę." }));
            }
        }
Exemple #2
0
        public ActionResult Delete(string clientId)
        {
            var referer = Request.UrlReferrer;

            if (referer == null)
            {
                return(RedirectToAction("Error", "Home"));
            }

            try
            {
                bus.ExecuteCommand(new DeleteClientFromNewsletterCommand {
                    Id = clientId
                });
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"Cannot delete newletter client with id = {clientId}");
            }
            return(Json(HttpStatusCode.Accepted, JsonRequestBehavior.AllowGet));
        }