Esempio n. 1
0
        private void UpdateCamperDietaryRestrictions(string[] selectedOptions, Camper camperToUpdate)
        {
            if (selectedOptions == null)
            {
                camperToUpdate.CamperDiets = new List <CamperDiet>();
                return;
            }

            var selectedDietaryRestrictionsHS = new HashSet <string>(selectedOptions);
            var camperDietaryRestrictionsHS   = new HashSet <int>
                                                    (camperToUpdate.CamperDiets.Select(c => c.DietaryRestrictionID));//IDs of the currently selected dietaryRestrictions

            foreach (var cond in _context.DietaryRestrictions)
            {
                if (selectedDietaryRestrictionsHS.Contains(cond.ID.ToString()))
                {
                    if (!camperDietaryRestrictionsHS.Contains(cond.ID))
                    {
                        camperToUpdate.CamperDiets.Add(new CamperDiet {
                            CamperID = camperToUpdate.ID, DietaryRestrictionID = cond.ID
                        });
                    }
                }
                else
                {
                    if (camperDietaryRestrictionsHS.Contains(cond.ID))
                    {
                        CamperDiet dietaryRestrictionToRemove = camperToUpdate.CamperDiets.SingleOrDefault(c => c.DietaryRestrictionID == cond.ID);
                        _context.Remove(dietaryRestrictionToRemove);
                    }
                }
            }
        }
Esempio n. 2
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new CampOrnoContext(
                       serviceProvider.GetRequiredService <DbContextOptions <CampOrnoContext> >()))
            {
                //Create sample data with some random values
                Random random = new Random();

                string[] firstNames = new string[] { "Fred", "Barney", "Wilma", "Betty" };
                string[] kidNames   = new string[] { "Woodstock", "Sally", "Violet", "Charlie", "Lucy", "Linus", "Franklin", "Marcie", "Schroeder" };
                string[] lastNames  = new string[] { "Stovell", "Jones", "Bloggs", "Flintstone", "Rubble", "Brown", "Smith", "Daniel" };
                string[] nicknames  = new string[] { "Hightower", "Wizard", "Kingfisher", "Prometheus", "Broomspun", "Shooter", "Chuckles" };
                string[] compounds  = new string[] { "Nestlings", "Fledglings", "Sharpies" };
                string[] diets      = new string[] { "Peanuts", "Shellfish", "Gluten", "Lactose", "Halal" };
                string[] genders    = new string[] { "M", "F", "N", "T", "O" };
                string[] activities = new string[] { "Canoeing", "Archery", "Ice Fishing", "Karate", "Coding" };

                //For seeding pictures
                string[] pictures = new string[] { "Barney.png", "blankProfile.png", "Fred.png", "Pebbles.png", "Wilma.png" };

                //For Part 5
                //Activities
                if (!context.Activities.Any())
                {
                    //loop through the array of Compound names
                    foreach (string a in activities)
                    {
                        Activity act = new Activity()
                        {
                            Name = a
                        };
                        context.Activities.Add(act);
                    }
                    context.SaveChanges();
                }

                //Dietary Restrictions
                if (!context.DietaryRestrictions.Any())
                {
                    //loop through the array of Compound names
                    foreach (string d in diets)
                    {
                        DietaryRestriction diet = new DietaryRestriction()
                        {
                            Name = d
                        };
                        context.DietaryRestrictions.Add(diet);
                    }
                    context.SaveChanges();
                }

                //Compounds
                if (!context.Compounds.Any())
                {
                    //loop through the array of Compound names
                    foreach (string c in compounds)
                    {
                        Compound compound = new Compound()
                        {
                            Name = c
                        };
                        context.Compounds.Add(compound);
                    }
                    context.SaveChanges();
                }
                //Counselors
                if (context.Counselors.Count() == 0)
                {
                    List <Counselor> counselors = new List <Counselor>();
                    foreach (string lastName in lastNames)
                    {
                        foreach (string firstname in firstNames)
                        {
                            //Construct some counselor details
                            Counselor newCounselor = new Counselor()
                            {
                                FirstName  = firstname,
                                LastName   = lastName,
                                MiddleName = lastName[1].ToString().ToUpper(),
                                SIN        = random.Next(213214131, 989898989).ToString(),
                            };
                            counselors.Add(newCounselor);
                        }
                    }
                    //Now give a few of them nicknames.
                    //We don't want any duplicates so choose one counselor
                    //for each nickname
                    int numNames      = nicknames.Count();
                    int numCounselors = counselors.Count();
                    for (int i = 0; i < numNames; i++)
                    {
                        counselors[random.Next(0, numNames)].Nickname = nicknames[i];
                    }
                    //Now add your list into the DbSet
                    context.Counselors.AddRange(counselors);
                    context.SaveChanges();
                }
                //Create collections of the primary keys
                int[] counselorIDs     = context.Counselors.Select(a => a.ID).ToArray();
                int   counselorIDCount = counselorIDs.Count();
                int[] compoundIDs      = context.Compounds.Select(a => a.ID).ToArray();
                int   compoundIDCount  = compoundIDs.Count();
                int   genderCount      = genders.Count();
                int   pictureCount     = pictures.Count();

                //Campers
                if (!context.Campers.Any())
                {
                    // Start birthdate for randomly produced campers
                    // We will subtract a random number of days from today
                    DateTime startDOB = DateTime.Today;

                    List <Camper> campers = new List <Camper>();
                    int           toggle  = 1; //Used to alternate assigning counselors
                    foreach (string lastName in lastNames)
                    {
                        foreach (string kidname in kidNames)
                        {
                            //Construct some employee details
                            Camper newCamper = new Camper()
                            {
                                FirstName  = kidname,
                                LastName   = lastName,
                                MiddleName = kidname[1].ToString().ToUpper(),
                                DOB        = startDOB.AddDays(-random.Next(1480, 5800)),
                                Gender     = genders[random.Next(genderCount)],
                                //Uncomment these lines to seed profile pictures
                                //imageContent = imageToBytes("wwwroot/pictures/" + pictures[random.Next(pictureCount)]),
                                //imageMimeType = "image/png",
                                //imageFileName = "Profile.png",
                                eMail      = (kidname.Substring(0, 2) + lastName + random.Next(11, 111).ToString() + "@outlook.com").ToLower(),
                                Phone      = Convert.ToInt64(random.Next(2, 10).ToString() + random.Next(213214131, 989898989).ToString()),
                                CompoundID = compoundIDs[random.Next(compoundIDCount)]
                            };
                            if (toggle % 2 == 0)//Every second camper gets a lead counselor assigned
                            {
                                newCamper.CounselorID = counselorIDs[random.Next(counselorIDCount)];
                            }
                            toggle++;
                            campers.Add(newCamper);
                        }
                    }
                    //Now add your list into the DbSet
                    context.Campers.AddRange(campers);
                    context.SaveChanges();
                }

                //Create collections of the primary keys
                int[] camperIDs                 = context.Campers.Select(a => a.ID).ToArray();
                int   camperIDCount             = camperIDs.Count();
                int[] dietaryRestrictionIDs     = context.DietaryRestrictions.Select(a => a.ID).ToArray();
                int   dietaryRestrictionIDCount = dietaryRestrictionIDs.Count();
                //For Part 5
                int[] activityIDs     = context.Activities.Select(a => a.ID).ToArray();
                int   activityIDCount = activityIDs.Count();

                //For Part 5
                //CamperActivities - the Intersection
                //Add an activity to every second Camper
                if (!context.CamperActivities.Any())
                {
                    int toggle = 1; //Used to alternate
                    for (int i = 0; i < camperIDCount; i++)
                    {
                        if (toggle % 2 == 0)//Every second camper
                        {
                            CamperActivity ds = new CamperActivity()
                            {
                                CamperID   = camperIDs[i],
                                ActivityID = activityIDs[random.Next(activityIDCount)]
                            };
                            context.CamperActivities.Add(ds);
                        }
                        toggle++;
                    }
                    context.SaveChanges();
                }

                //CamperDiets - the Intersection
                //Add a few restrictions to every second Camper
                if (!context.CamperDiets.Any())
                {
                    int toggle = 1; //Used to alternate
                    for (int i = 0; i < camperIDCount; i++)
                    {
                        if (toggle % 2 == 0)//Every second camper
                        {
                            CamperDiet ds = new CamperDiet()
                            {
                                CamperID             = camperIDs[i],
                                DietaryRestrictionID = dietaryRestrictionIDs[random.Next(dietaryRestrictionIDCount)]
                            };
                            context.CamperDiets.Add(ds);
                        }
                        toggle++;
                    }
                    context.SaveChanges();
                }
                //CounselorCompounds - the Intersection
                //Add a compound to each counselor
                if (!context.CounselorCompounds.Any())
                {
                    foreach (int i in counselorIDs)
                    {
                        CounselorCompound cc = new CounselorCompound()
                        {
                            CompoundID  = compoundIDs[random.Next(compoundIDCount)],
                            CounselorID = i
                        };
                        context.CounselorCompounds.Add(cc);
                    }
                }
                context.SaveChanges();
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Create([Bind("ID,FirstName,MiddleName,LastName,DOB,Gender,eMail,Phone,CounselorID,CompoundID")]
                                                 Camper camper, string[] selectedOptions, IFormFile thePicture)
        {
            try
            {
                //Add the selected
                if (selectedOptions != null)
                {
                    camper.CamperDiets = new List <CamperDiet>();
                    foreach (var r in selectedOptions)
                    {
                        var condToAdd = new CamperDiet {
                            CamperID = camper.ID, DietaryRestrictionID = int.Parse(r)
                        };
                        camper.CamperDiets.Add(condToAdd);
                    }
                }
                if (ModelState.IsValid)
                {
                    if (thePicture != null)
                    {
                        string mimeType   = thePicture.ContentType;
                        long   fileLength = thePicture.Length;
                        if (!(mimeType == "" || fileLength == 0))//Looks like we have a file!!!
                        {
                            if (mimeType.Contains("image"))
                            {
                                using (var memoryStream = new MemoryStream())
                                {
                                    await thePicture.CopyToAsync(memoryStream);

                                    camper.imageContent = memoryStream.ToArray();
                                }
                                camper.imageMimeType = mimeType;
                                camper.imageFileName = thePicture.FileName;
                            }
                        }
                    }
                    _context.Add(camper);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index", "CamperActivities", new { CamperID = camper.ID }));
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                ModelState.AddModelError("", "Unable to save changes after multiple attempts. Try again, and if the problem persists, see your system administrator.");
            }
            catch (DbUpdateException dex)
            {
                if (dex.InnerException.Message.Contains("IX_Campers_eMail"))
                {
                    ModelState.AddModelError("eMail", "Unable to save changes. Remember, you cannot have duplicate eMail addresses.");
                }
                else
                {
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
                }
            }
            PopulateAssignedDietaryRestrictionData(camper);
            PopulateDropDownLists(camper);

            if (User.IsInRole("Staff"))//Decide which version of the view to send
            {
                return(View("CreateStaff", camper));
            }
            else
            {
                return(View(camper));
            }
        }