public void AddChallengesToUser(int userid, IList <int> challengeids) { User usr = _users.FirstOrDefault(u => u.UserId == userid); //Feth the challenges that might be added List <Challenge> challenges = _challenges.Where(c => challengeids.Contains(c.ChallengeId)).ToList(); //Fetch the existing challenges for the user List <int> existing = _dbContext.ChallengeUser.Where(cu => cu.UserId == userid).Select(cu => cu.ChallengeId).ToList(); //Add the not already existing challenges List <ChallengeUser> challengesToAdd = challenges.Where(c => !existing.Contains(c.ChallengeId)).Select(c => new ChallengeUser() { ChallengeId = c.ChallengeId, Challenge = c, User = usr, UserId = usr.UserId } ).ToList(); usr.AddChallenges(challengesToAdd); _dbContext.ChallengeUser.AddRange(challengesToAdd); _dbContext.SaveChanges(); }
public void SaveChanges() { _dbContext.SaveChanges(); }
public async Task InitializeData() { _dbContext.Database.EnsureDeleted(); if (_dbContext.Database.EnsureCreated()) { await CreateRoles(); //Multimeduser await CreateUser("SofieV", "*****@*****.**", "P@ssword123", UserRole.MULTIMED); //categories Category c = new Category() { Name = "Ondergewicht" }; _dbContext.Add(c); //challenges Challenge ch = new Challenge() { ChallengeImage = "", Description = "Loop 2 km", Title = "Lopen", Category = c, Level = 1 }; //TherapistType TherapistType thType = new TherapistType() { Type = "Diƫtist", Categories = new List <Category> { c } }; _dbContext.Add(thType); //OpeningTimes List <OpeningTimes> otList = new List <OpeningTimes>() { new OpeningTimes() { Interval = "9-17" }, new OpeningTimes() { Interval = "9-17" }, new OpeningTimes() { Interval = "9-17" }, new OpeningTimes() { Interval = "9-17" }, new OpeningTimes() { Interval = "9-17" }, new OpeningTimes() { Interval = "9-17" }, new OpeningTimes() { Interval = "9-17" }, }; //Therapist Therapist th = new Therapist() { FirstName = "Therapist", LastName = "De Peape", HouseNumber = 1, PhoneNumber = "0474139526", PostalCode = 9000, Street = "test", Website = "www.google.com", City = "Gent", Email = "*****@*****.**", TherapistType = thType, OpeningTimes = otList }; await CreateUser("TestTh", th.Email, "P@ssword123", UserRole.THERAPIST); _dbContext.Add(th); //company Company cmp = new Company() { Name = "Multimed", Street = "Multimedstraat", City = "Gent", Contract = DateTime.Now.AddYears(30), Country = "Belgie", Mail = "*****@*****.**", Phone = "04785889764", PostalCode = 9000, HouseNumber = 1, Site = "multimed.be" }; _dbContext.Add(cmp); //user User usr = new User() { FirstName = "Boefer", FamilyName = "rob", Phone = "0478995888", ExperiencePoints = 16, Company = cmp, Email = "*****@*****.**", Contract = cmp.Contract }; usr.AddTherapist(th); //many to many from user List <CategoryUser> cusr = new List <CategoryUser> { new CategoryUser() { Category = c, User = usr, UserId = usr.UserId, CategoryId = c.CategoryId } }; ChallengeUser chUsr = new ChallengeUser() { ChallengeUserId = usr.UserId, User = usr, ChallengeId = ch.ChallengeId, Challenge = ch }; usr.AddChallenges(new List <ChallengeUser> { chUsr }); usr.AddCategories(cusr); //account user await CreateUser("Boeferrob", usr.Email, "P@ssword123", UserRole.USER); _dbContext.Add(usr); _dbContext.ChallengeUser.Add(chUsr); th.AddClient(usr); #region Save changes _dbContext.SaveChanges(); #endregion } }