Esempio n. 1
0
 // GET: /Kit/Create
 public ActionResult Create()
 {
     var db = new BP_ModelContainer();
     var sampleKits = db.SampleKits.ToList();
     ViewBag.SampleKits = sampleKits;
     return View();
 }
Esempio n. 2
0
 public static Prepper GetCurrentPrepper(IPrincipal user)
 {
     var db = new BP_ModelContainer();
     var prepperName = user.Identity.Name;
     var prepper = (from p in db.Preppers where p.Name == prepperName select p).FirstOrDefault();
     return prepper ?? null;
 }
Esempio n. 3
0
        private void ResendConfirmationEmail(int prepperId)
        {
            var db = new BP_ModelContainer();
            var prepper = db.Preppers.Find(prepperId);
            if (prepper == null)
                throw new Exception(string.Format("Cannot find Prepper with ID [{0}]", prepperId));

            SendRegistrationConfirnationEmail(prepper);
        }
Esempio n. 4
0
        internal static string GetParameterValue(ParameterName parameterName)
        {
            var db = new BP_ModelContainer();
            var name = parameterName.ToString();
            var parameter = (from p in db.Parameters where p.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) select p).FirstOrDefault();
            if (parameter == null)
                throw new Exception(string.Format("Cannot find parameter [{0}]", name));

            return parameter.Value;
        }
Esempio n. 5
0
        public ActionResult Create([Bind(Include = "Id,Name,Location,PrepperId")] Kit kit)
        {
            var db = new BP_ModelContainer();
            if (ModelState.IsValid)
            {
                var prepper = BpFunctions.GetCurrentPrepper(User);
                kit.PrepperId = prepper.Id;

                db.Kits.Add(kit);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.PrepperId = new SelectList(db.Preppers, "Id", "Name", kit.PrepperId);
            return View(kit);
        }
Esempio n. 6
0
        public void AddKit(string name, string location, int sampleKitId)
        {
            try
            {
                var db = new BP_ModelContainer();
                var prepper = (from p in db.Preppers where p.Name == User.Identity.Name select p).FirstOrDefault();
                if (prepper == null) throw new Exception(string.Format("Cannot find Prepper [{0}]", User.Identity.Name));

                var kit = new Kit
                {
                    Name = name,
                    Location = location,
                    PrepperId = prepper.Id
                };

                if (sampleKitId > 0)
                {
                    var status = (from s in db.ItemStatus where s.Name.Equals("planned", StringComparison.InvariantCultureIgnoreCase) select s).FirstOrDefault();
                    if (status == null) throw new Exception("Cannot find Status");

                    var sampleItems = (from si in db.SampleItems where si.SampleKitId == sampleKitId select si).ToList();
                    foreach (var sampleItem in sampleItems)
                    {
                        kit.Items.Add(new Item
                        {
                            ItemStatusId = status.Id,
                            Name = sampleItem.Name,
                            DaysWarningOfReplacement = sampleItem.DaysWarningOfReplacement,
                            Quantity = sampleItem.Quantity,
                            SampleItemId = sampleItem.Id
                        });
                    }
                }

                db.Kits.Add(kit);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Esempio n. 7
0
        public string GetAds()
        {
            var db = new BP_ModelContainer();

            var availableAdds = (from si in db.SampleItems where si.AmazonWidget != null select si.AmazonWidget).ToList();

            const int maxAds = 2;
            var selectedAds = new List<string>();
            while (selectedAds.Count < maxAds)
            {
                var random = new Random();
                var i = random.Next(availableAdds.Count);
                selectedAds.Add(availableAdds[i]);
                availableAdds.RemoveAt(i);
                if (availableAdds.Count == 0) break;
            }

            var adsHtml = "";
            foreach (var ad in selectedAds)
                adsHtml += string.Format("<div>{0}</div>", ad);

            return adsHtml;
        }
Esempio n. 8
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindAsync(model.UserName, model.Password);
                if (user != null)
                {
                    //Make sure user has been validated
                    var db = new BP_ModelContainer();
                    var prepper = (from p in db.Preppers where p.Name.Equals(model.UserName, StringComparison.InvariantCultureIgnoreCase) select p).FirstOrDefault();
                    //todo: error function that emails me as well as reports to UI
                    if (prepper == null)
                        ModelState.AddModelError("", "System cannot locate prepper");
                    else
                    {
                        if (prepper.ConfirmationGuid != null)
                        {
                            ResendConfirmationEmail(prepper.Id);
                            ModelState.AddModelError("", "This user has not been confirmed yet.  Please click the link in the confirmation email that has been resent to you.");
                        }
                        else
                        {
                            await SignInAsync(user, model.RememberMe);
                            return RedirectToLocal(returnUrl);
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Invalid username or password.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Esempio n. 9
0
        public void SendWeeklyEmails()
        {
            var adminEmailAddress = BpFunctions.GetParameterValue(BpFunctions.ParameterName.AdminEmailAddress);
            if (adminEmailAddress == null) throw new Exception(string.Format("Cannot find value for parameter [{0}]", BpFunctions.ParameterName.AdminEmailAddress));

            //Is it time to send the emails?
            var lastWeeklyEmailsSentValue = BpFunctions.GetParameterValue(BpFunctions.ParameterName.LastWeeklyEmailsSent);
            DateTime emailsSent;
            if (!DateTime.TryParse(lastWeeklyEmailsSentValue, out emailsSent))
                emailsSent = new DateTime(1, 1, 1);
            var timeSpan = DateTime.Today - emailsSent;
            if (timeSpan.TotalDays < 7) return;

            var db = new BP_ModelContainer();

            var weeklyEmailSummary = new StringBuilder();
            weeklyEmailSummary.AppendLine(string.Format("bPrep.net Weekly Summary {0}", DateTime.Now.ToString("dd/MMM/yyyy")));

            foreach (var prepper in db.Preppers)
            {
                var prepperWeeklyReport = new StringBuilder();
                foreach (var kit in prepper.Kits)
                {
                    foreach (var item in kit.Items)
                    {
                        //Planned?
                        if (item.ItemStatus.Name.Equals(BpFunctions.ItemStatuses.Planned.ToString()))
                            prepperWeeklyReport.AppendLine(string.Format("Yet to be ordered: Kit [{0}], Item [{1}]", kit.Name, item.Name));

                        //Ordered?
                        if (item.ItemStatus.Name.Equals(BpFunctions.ItemStatuses.Ordered.ToString()))
                            prepperWeeklyReport.AppendLine(string.Format("Waiting for delivery: Kit [{0}], Item [{1}]", kit.Name, item.Name));

                        //Received?
                        if (item.ItemStatus.Name.Equals(BpFunctions.ItemStatuses.Received.ToString()))
                            prepperWeeklyReport.AppendLine(string.Format("Put in place!: Kit [{0}], Item [{1}]", kit.Name, item.Name));

                    }
                }

                //Unsubscribe line
                if (Request.Url != null)
                {
                    var host = Request.Url.Authority.ToLower();
                    var fullLink = string.Format("http://{0}/Account/Unsubscribe?guid={1}", host, prepper.UnsubscribeGuid);
                    prepperWeeklyReport.AppendLine(" ");
                    prepperWeeklyReport.AppendLine(" ");
                    prepperWeeklyReport.AppendLine("To Unsubscribe from bPrep.net click this link; " + fullLink);
                }

                if (prepperWeeklyReport.Length > 0)
                {
                    BpFunctions.SendEmail(prepper.Email, "bPrep.net Weekly Summary Report", prepperWeeklyReport.ToString());

                    weeklyEmailSummary.AppendLine(" ");
                    weeklyEmailSummary.AppendLine(string.Format("Prepper [{0}]", prepper.Name));
                    weeklyEmailSummary.AppendLine(prepperWeeklyReport.ToString());
                }
            }

            //Summary report to me
            BpFunctions.SendEmail(adminEmailAddress, "bPrep.Net Admin Weekly Email Report", weeklyEmailSummary.ToString());

            //Set last sent time to today
            var paramName = BpFunctions.ParameterName.LastWeeklyEmailsSent.ToString();
            var parameter = (from p in db.Parameters where p.Name == paramName select p).FirstOrDefault();
            if (parameter == null) throw new Exception(string.Format("Cannot find parameter [{0}]", BpFunctions.ParameterName.LastWeeklyEmailsSent));
            parameter.Value = DateTime.Today.ToString("dd/MMM/yyyy");
            db.SaveChanges();
        }
Esempio n. 10
0
        public ActionResult Unsubscribe(string guid)
        {
            var db = new BP_ModelContainer();
            var prepper = (from p in db.Preppers where p.UnsubscribeGuid == guid select p).FirstOrDefault();
            if (prepper == null) throw new Exception("Cannot find Prepper to Unsubscribe");

            //Get ids
            var kitIds = new List<int>();
            var itemIds = new List<int>();
            foreach (var kit in prepper.Kits)
            {
                kitIds.Add(kit.Id);
                itemIds.AddRange(kit.Items.Select(item => item.Id));
            }

            //Remove records from bPrep
            foreach (var itemId in itemIds)
                db.Items.Remove(db.Items.Find(itemId));
            foreach (var kitId in kitIds)
                db.Kits.Remove(db.Kits.Find(kitId));
            db.Preppers.Remove(prepper);

            //Remove record from bPrep-Memebership
            var sql = string.Format("delete from [dbo].[AspNetUsers] where UserName = '******'", prepper.Name);
            var connMembership = new SqlConnection(Properties.Settings.Default.MembershipCS);
            var commMembership = new SqlCommand(sql, connMembership);
            connMembership.Open();
            var executeNonQuery = commMembership.ExecuteNonQuery();
            connMembership.Close();
            connMembership.Dispose();

            if (executeNonQuery > 0)
                db.SaveChanges();

            return View("Unsubscribed");
        }
Esempio n. 11
0
 public ActionResult DeleteConfirmed(int id)
 {
     var db = new BP_ModelContainer();
     Kit kit = db.Kits.Find(id);
     db.Kits.Remove(kit);
     db.SaveChanges();
     return RedirectToAction("Index");
 }
Esempio n. 12
0
        //[ValidateAntiForgeryToken]
        public string SendRegistration(string userName, string emailAddress, string password)
        {
            var db = new BP_ModelContainer();

            userName = userName.Trim();
            emailAddress = emailAddress.Trim();
            password = password.Trim();

            //Check for matching username
            var prepper = (from p in db.Preppers where p.Name.Equals(userName, StringComparison.InvariantCultureIgnoreCase) select p).FirstOrDefault();
            if (prepper != null) return "Username already exists";

            //Check for matching email
            prepper = (from p in db.Preppers where p.Email.Equals(emailAddress, StringComparison.InvariantCultureIgnoreCase) select p).FirstOrDefault();
            if (prepper != null) return "Email already registered";

            var user = new ApplicationUser { UserName = userName };
            var result = UserManager.Create(user, password);
            if (!result.Succeeded) return result.Errors.FirstOrDefault();

            //Save info as Prepper
            prepper = new Prepper
            {
                Name = user.UserName,
                Email = emailAddress,
                ConfirmationRequestSent = DateTime.Now,
                ConfirmationGuid = Guid.NewGuid().ToString(),
                ShowTipKit = true,
                ShowTipKitItems = true,
                UnsubscribeGuid = Guid.NewGuid().ToString()
            };
            db.Preppers.Add(prepper);
            db.SaveChanges();

            SendRegistrationConfirnationEmail(prepper);

            return "";
        }
Esempio n. 13
0
        public ActionResult ConfirmRegistration(string guid)
        {
            var db = new BP_ModelContainer();
            var prepper = (from p in db.Preppers where p.ConfirmationGuid == guid select p).FirstOrDefault();
            if (prepper == null)
                return RedirectToAction("Login", "Account");

            prepper.ConfirmationGuid = null;
            db.SaveChanges();

            return View();
        }
Esempio n. 14
0
 // GET: /Kit/Edit/5
 public ActionResult Edit(int? id)
 {
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     var db = new BP_ModelContainer();
     Kit kit = db.Kits.Find(id);
     if (kit == null)
     {
         return HttpNotFound();
     }
     ViewBag.PrepperId = new SelectList(db.Preppers, "Id", "Name", kit.PrepperId);
     return View(kit);
 }
Esempio n. 15
0
        public void SendDailyEmailsIfNecessary()
        {
            var adminEmailAddress = BpFunctions.GetParameterValue(BpFunctions.ParameterName.AdminEmailAddress);
            if (adminEmailAddress == null) throw new Exception(string.Format("Cannot find value for parameter [{0}]", BpFunctions.ParameterName.AdminEmailAddress));

            //Is it time to send the emails?
            var lastDailyEmailsSent = BpFunctions.GetParameterValue(BpFunctions.ParameterName.LastDailyEmailsSent);
            DateTime emailsSent;
            if (!DateTime.TryParse(lastDailyEmailsSent, out emailsSent))
                emailsSent = new DateTime(1, 1, 1);
            var timeSpan = DateTime.Today - emailsSent;
            if (timeSpan.TotalDays < 1) return;

            var db = new BP_ModelContainer();

            var dailyEmailSummary = new StringBuilder();
            dailyEmailSummary.AppendLine(string.Format("bPrep.net Daily Summary {0}", DateTime.Now.ToString("yy-mm-dd/MMM/yyyy")));

            foreach (var prepper in db.Preppers)
            {
                //Get items that need replacing/checking
                var prepperDailyReport = new StringBuilder();
                foreach (var kit in prepper.Kits)
                {
                    foreach (var item in kit.Items)
                    {
                        //Is Positioned?
                        if (!item.ItemStatus.Name.Equals(BpFunctions.ItemStatuses.Positioned.ToString())) continue;
                        if (item.StartWarningForReplacement == null) continue;

                        //Is due for replacement / check
                        var startWarningForReplacement = (DateTime)item.StartWarningForReplacement;
                        if (startWarningForReplacement <= DateTime.Today)
                            prepperDailyReport.AppendLine(string.Format("Kit [{0}], Item [{1}] replace/Check by [{2}]",
                                kit.Name, item.Name, startWarningForReplacement.ToString("dd/MMM/yyyy")));
                    }
                }
                if (prepperDailyReport.Length > 0)
                {
                    BpFunctions.SendEmail(prepper.Email, "bPrep.net  Item Expiery Report", prepperDailyReport.ToString());

                    dailyEmailSummary.AppendLine(" ");
                    dailyEmailSummary.AppendLine(string.Format("Prepper [{0}]", prepper.Name));
                    dailyEmailSummary.AppendLine(prepperDailyReport.ToString());
                }
            }

            //Summary report to me
            BpFunctions.SendEmail(adminEmailAddress, "bPrep.Net Daily Email Report (Admin)", dailyEmailSummary.ToString());

            //Set last sent time to today
            var paramName = BpFunctions.ParameterName.LastDailyEmailsSent.ToString();
            var parameter = (from p in db.Parameters where p.Name == paramName select p).FirstOrDefault();
            if (parameter == null) throw new Exception(string.Format("Cannot find parameter [{0}]", BpFunctions.ParameterName.LastDailyEmailsSent));
            parameter.Value = DateTime.Today.ToString("dd/MMM/yyyy");
            db.SaveChanges();
        }
Esempio n. 16
0
 public ActionResult Edit([Bind(Include = "Id,Name,Location,PrepperId")] Kit kit)
 {
     var db = new BP_ModelContainer();
     if (ModelState.IsValid)
     {
         db.Entry(kit).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.PrepperId = new SelectList(db.Preppers, "Id", "Name", kit.PrepperId);
     return View(kit);
 }
Esempio n. 17
0
        // GET: /Kit/
        public ActionResult Index()
        {
            var db = new BP_ModelContainer();
            ViewBag.SampleKits = db.SampleKits.ToList();

            var currentPrepper = BpFunctions.GetCurrentPrepper(User);
            return View(currentPrepper);
        }
Esempio n. 18
0
        public string GetKits()
        {
            var db = new BP_ModelContainer();
            var prepperName = User.Identity.Name;
            var prepper = (from p in db.Preppers where p.Name == prepperName select p).FirstOrDefault();
            if (prepper == null) throw new Exception("Cannot find Prepper");

            var kits = (from k in db.Kits where k.PrepperId==prepper.Id select k).ToList();

            var builder = new StringBuilder();
            foreach (var kit in kits)
            {
                builder.Append("<tr>");
                builder.Append("<td>");
                builder.Append(kit.Name);
                builder.Append("</td>");
                builder.Append("<td>");
                builder.Append(kit.Location);
                builder.Append("</td>");
                builder.Append(string.Format("<td><a href='/Item/Index/{0}'>edit</a></td>", kit.Id));
                builder.Append("</tr>");
            }
            var html = builder.ToString();
            return html;
        }
Esempio n. 19
0
        public void DontShowKitItemTipAgain()
        {
            var currentPrepper = BpFunctions.GetCurrentPrepper(User);
            if (currentPrepper == null) throw new Exception("Cannot find Prepper");

            var db = new BP_ModelContainer();
            var prepper = db.Preppers.Find(currentPrepper.Id);
            if (prepper == null) throw new Exception("Cannot find Prepper");

            prepper.ShowTipKitItems = false;
            db.SaveChanges();
        }
Esempio n. 20
0
 // GET: /Kit/Delete/5
 public ActionResult Delete(int? id)
 {
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     var db = new BP_ModelContainer();
     Kit kit = db.Kits.Find(id);
     if (kit == null)
     {
         return HttpNotFound();
     }
     return View(kit);
 }