Beispiel #1
0
        public async Task <ActionResult> Index(EmailNotifyViewModel model)
        {
            try
            {
                // Verification
                if (ModelState.IsValid)
                {
                    // Initialization
                    string emailMsg     = "Dear " + model.ToEmail + ", <br /><br /> A post has been made in one of the categories you subscribe to!<b style='color: blue'> ORU Blog Notification </b> <br /><br /> Thanks & Regards, <br />ORU Bloggsters";
                    string emailSubject = EmailInfo.EMAIL_SUBJECT_DEFAULT + " Blog Notification";

                    // Sends Email
                    await this.SendEmailAsync(model.ToEmail, emailMsg, emailSubject);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            return(View());
        }
Beispiel #2
0
        public async Task <ActionResult> CreatePost(Post model, HttpPostedFileBase FilePath, string dropdownMenu)
        {
            if (!ModelState.IsValid)
            {
                var db     = new BlogDbContext();
                var myList = db.Categories.ToList();
                ViewBag.categories = myList;
                return(View());
            }

            var ctx    = new BlogDbContext();
            var user   = User.Identity.GetUserId();
            var catId  = ctx.Categories.FirstOrDefault(p => p.Name == dropdownMenu);
            var author = ctx.Profiles.FirstOrDefault(p => p.ProfileID == user);

            var post = new Post
            {
                Title         = model.Title,
                Content       = model.Content,
                PublishedWall = model.PublishedWall,
                PostDateTime  = DateTime.Now,
                Category      = catId,
                AuthorOfPosts = author
            };

            if (FilePath != null)
            {
                try
                {
                    var checkextension = Path.GetExtension(FilePath.FileName).ToLower();
                    if (checkextension.ToLower().Contains(".jpg") || checkextension.ToLower().Contains(".jpeg") || checkextension.Contains(".png") || checkextension.Contains(".pdf") || checkextension.Contains(".xlsx") || checkextension.Contains(".docx"))
                    {
                        // path skapar sökväg för att lägga in bilden i projektmappen Images
                        string path = System.IO.Path.Combine(Server.MapPath("~/Files"), System.IO.Path.GetFileName(FilePath.FileName));
                        // relativePath skapar den relativa sökvägen som läggs in i databasen
                        string relativePath = System.IO.Path.Combine("~/Files/" + FilePath.FileName);

                        post.FilePath = relativePath;
                        ctx.Posts.Add(post);
                        FilePath.SaveAs(path);
                        ctx.SaveChanges();
                        ViewBag.FileStatus = "Photo uploaded successfully.";

                        var postCat   = post.Category.Name;
                        var viewModel = new EmailNotifyViewModel();
                        var profiles  = ctx.SelectedCategories.Where(x => x.Name == postCat).Select(x => x.ProfileID).ToList();
                        var blogDb    = new BlogDbContext();
                        foreach (var p in profiles)
                        {
                            var destinationEmail = blogDb.Users.FirstOrDefault(x => x.UserID == p);
                            var mail             = destinationEmail.Email;
                            viewModel.ToEmail = mail;
                            var emailSent = await Index(viewModel);
                        }
                    }


                    else
                    {
                        ViewBag.FileStatus = "Only .JPEG, .PFD, .DOCX, .XLSX and .PNG allowed";
                    }
                }
                catch (Exception)
                {
                    ViewBag.FileStatus = "Error while photo uploading.";
                }
            }
            else
            {
                ctx.Posts.Add(post);
                ctx.SaveChanges();

                var postCat   = post.Category.Name;
                var viewModel = new EmailNotifyViewModel();
                var profiles  = ctx.SelectedCategories.Where(x => x.Name == postCat).Select(x => x.ProfileID).ToList();
                var blogDb    = new BlogDbContext();
                foreach (var p in profiles)
                {
                    var destinationEmail = blogDb.Users.FirstOrDefault(x => x.UserID == p);
                    var mail             = destinationEmail.Email;
                    viewModel.ToEmail = mail;
                    var emailSent = await Index(viewModel);
                }
            }

            ViewBag.EditStatus = "Successful registration";

            if (post.PublishedWall.Equals("Formell"))
            {
                return(RedirectToAction("FormalWall", "Wall"));
            }
            else
            {
                return(RedirectToAction("InformalWall", "Wall"));
            }
        }