Esempio n. 1
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. 2
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. 3
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. 4
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. 5
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. 6
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. 7
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. 8
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. 9
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. 10
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");
 }