Example #1
0
 public ActionResult Edit(NewsTimeViewModel model)
 {
     try
     {
         var news = db.News.Find(model.News.ID);
         if (news == null)
             return RedirectToAction("Index", "Home");
         if (!news.Author.Equals(Membership.GetUser().UserName) && !Roles.IsUserInRole(Membership.GetUser().UserName, "admin"))
             return RedirectToAction("Details", new { ID = news.ID });
         int hour, minutes;
         Int32.TryParse(model.Hour, out hour);
         Int32.TryParse(model.Minutes, out minutes);
         TimeSpan time = new TimeSpan(hour, minutes, 0);
         news.Date = model.News.Date + time;
         news.Text = model.News.Text;
         news.Title = model.News.Title;
         try
         {
             var cats = db.NewsCategories.Where(r => model.CategoryIDs.Contains(r.ID));
             news.NewsCategories.Clear();
             foreach (var cat in cats)
             {
                 news.NewsCategories.Add(cat);
             }
         }
         catch (NotSupportedException) { /* 0 categories */ }
         UpdateModel(news);
         db.SaveChanges();
         return RedirectToAction("Details", new { id = model.News.ID });
     }
     catch (Exception)
     {
         ModelState.AddModelError("", "Błąd edycji!");
     }
     ViewBag.NewsCategories = db.NewsCategories;
     ViewBag.CurrentCategories = model.News.NewsCategories;
     return View(model);
 }
Example #2
0
        public ActionResult Create(NewsTimeViewModel newNewsTime)
        {
            if (ModelState.IsValid)
            {
                News newNews = newNewsTime.News;
                int hour, minutes;
                Int32.TryParse(newNewsTime.Hour,out hour);
                Int32.TryParse(newNewsTime.Minutes,out minutes);
                TimeSpan time = new TimeSpan(hour, minutes, 0);
                newNews.Date = newNews.Date + time;
                try
                {
                    var cats = db.NewsCategories.Where(r => newNewsTime.CategoryIDs.Contains(r.ID)).ToList();
                    foreach (var cat in cats)
                    {
                        newNews.NewsCategories.Add(cat);
                    }
                }
                catch (NotSupportedException) { /* 0 categories */ }
                db.News.Add(newNews);
                db.SaveChanges();

                //notify users
                List<Profile> profiles = new List<Profile>();
                foreach(var category in newNews.NewsCategories)
                {
                    var prof = db.Profiles.ToList();
                    foreach(var one in prof)
                    {
                        if(one.Subscriptions.Contains(category))
                            profiles.Add(one);
                    }
                }
                profiles = profiles.Distinct().ToList();
                try
                {
                    MailMessage msg = new MailMessage();
                    msg.From = new MailAddress("*****@*****.**", "Grupa .NET EKA");
                    foreach (var profile in profiles)
                    {
                        msg.To.Add(Membership.GetUser(profile.UserName).Email);
                    }
                    msg.Subject = "Nowa wiadomość na stronie Grupa .NET EKA";
                    msg.Body = "Na stronie pojawiała się nowa wiadomość. Aby ją zobaczyć kliknij link poniżej:<br />" +
                        "http://" + Request.Url.Host + "/News/Details/" + newNews.ID;
                    msg.IsBodyHtml = true;

                    SmtpClient smtp = new SmtpClient("smtp.live.com", 25);
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "master");
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Send(msg);

                    msg.Dispose();
                }
                catch (Exception)
                {
                    ViewBag.NewsCreateError = "Błąd! Nie wysłano powiadomień!";
                }
                //

                return RedirectToAction("Index","Home");
            }
            else
            {
                ViewBag.Now = DateTime.Now;
                ViewBag.NewsCategories = db.NewsCategories;
                return View(newNewsTime);
            }
        }
Example #3
0
 public ActionResult Edit(int id)
 {
     ViewBag.NewsCategories = db.NewsCategories;
     News news = db.News.Find(id);
     if (news == null)
         return RedirectToAction("Index", "Home");
     if (!news.Author.Equals(Membership.GetUser().UserName) && !Roles.IsUserInRole(Membership.GetUser().UserName, "admin"))
         return RedirectToAction("Details", new { ID = news.ID });
     NewsTimeViewModel editNews = new NewsTimeViewModel();
     editNews.News = news;
     editNews.Hour = news.Date.Hour.ToString();
     editNews.Minutes = news.Date.Minute.ToString();
     ViewBag.CurrentCategories = editNews.News.NewsCategories;
     return View(editNews);
 }
Example #4
0
 public ActionResult Create()
 {
     ViewBag.Now = DateTime.Now;
     ViewBag.NewsCategories = db.NewsCategories;
     NewsTimeViewModel newNewsTime = new NewsTimeViewModel();
     newNewsTime.News = new News();
     newNewsTime.News.Date = DateTime.Now;
     return View(newNewsTime);
 }