Пример #1
0
        public async Task <IActionResult> Create(CreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (_tagRepository.NameExists(model.Tag.Name))
                {
                    var message = $"Ya existe una etiqueta con el nombre {model.Tag.Name}";
                    ModelState.AddModelError("Tag.Name", message);
                    return(View(model));
                }

                var result = await _uploadImageService.UploadAndResizeAsync(
                    model.Image, _uploadTo, _maxWidth, _maxHeight
                    );

                if (result.Success)
                {
                    var filename = result.ReturnMessages["filename"];
                    model.Tag.Image = Path.Combine(_uploadTo, filename);
                    await _tagRepository.CreateAsync(model.Tag);

                    this.AddMessage("success", "Etiqueta creada con éxito");
                    return(RedirectToAction(nameof(List)));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error);
                }
            }

            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> Create(
            CreateEditViewModel model,
            [FromServices] ISubscribeArticleRepository subscribeArticleRepository,
            [FromServices] IEmailService emailService)
        {
            await _initializeCreateEditViewModel(model);

            if (ModelState.IsValid)
            {
                if (_articleRepository.TitleExists(model.Title))
                {
                    var message = $"El titulo '{model.Title}' ya existe";
                    ModelState.AddModelError(nameof(model.Title), message);
                    return(View(model));
                }

                var article = new Article
                {
                    Title           = model.Title,
                    Slug            = model.Slug,
                    Body            = model.Body,
                    Views           = model.Views,
                    Active          = model.Active,
                    Likes           = model.Likes,
                    CreateAt        = model.CreateAt,
                    UpdateAt        = model.UpdateAt,
                    OwnerId         = model.OwnerId,
                    DefaultTagId    = model.DefaultTagId,
                    TableOfContents = model.TableOfContents
                };

                if (model.ImageHeader != null)
                {
                    var result = await _uploadImageService.UploadAndResizeAsync(
                        model.ImageHeader,
                        _uploadTo,
                        _maxWidth,
                        _maxHeight);

                    if (result.Success)
                    {
                        var filename = result.ReturnMessages["filename"];
                        article.ImageHeader = Path.Combine(_uploadTo, filename);
                    }
                }

                await _articleRepository.CreateAsync(_tagRepository, article, model.Tags);

                // Notificar a los subscritos del nuevo articulo.
                // Solo si el artículo esta activo.
                if (model.Active)
                {
                    var subscribeList = subscribeArticleRepository
                                        .GetAll()
                                        .Select(s => new
                    {
                        EmailAddress = new MailAddress(s.Email),
                        s.Token
                    });

                    var siteName   = _configuration["Site:SiteName"];
                    var modelEmail = new ArticleSubscribeNotifyViewModel
                    {
                        SiteName = siteName,
                        Title    = article.Title,
                        CallBack = Url.Action(
                            "Details",
                            "Article",
                            new { slug = article.Slug },
                            HttpContext.Request.Scheme)
                    };

                    foreach (var subscribe in subscribeList.ToList())
                    {
                        var unsubscribe = Url.Action(
                            "UnSubscribe",
                            "ArticleSubscribe",
                            new { token = subscribe.Token },
                            HttpContext.Request.Scheme
                            );
                        modelEmail.UnsubscribeLink = unsubscribe;
                        emailService.Subject       = $"Nuevo articulo en {siteName}";
                        emailService.To            = new List <MailAddress> {
                            subscribe.EmailAddress
                        };
                        await emailService.SendEmailAsync("ArticleSubscribersNotify", modelEmail);
                    }

                    // Ping a google
                    var sitemapUrl = Url.Action("Sitemap", "Page", null, HttpContext.Request.Scheme);
                    var repGoogle  = WebRequest.Create(sitemapUrl);
                    repGoogle.GetResponse();
                    this.AddMessage("success", "Articulo creado con éxito");
                }
                else
                {
                    this.AddMessage("warning", "El artículo ha sido creado, pero no esta activo");
                }

                return(RedirectToAction(nameof(List)));
            }

            return(View(model));
        }
Пример #3
0
        public async Task <IActionResult> UploadImage([FromServices] IUploadImageService uploadImageService, IFormFile file)
        {
            var imageResult = await uploadImageService.UploadAndResizeAsync(file, "blog/articles/posts", 960, 960);

            return(new ObjectResult(imageResult));
        }