private void Update(GratsDBContext db) { var oldCategoryContacts = (from categoryContact in db.CategoryContacts.Include(cc => cc.Contact) where categoryContact.CategoryID == Category.ID select categoryContact).ToList(); var removed = oldCategoryContacts .Where(oldCC => !Category.CategoryContacts.Any(newCC => newCC.Contact.VKID == oldCC.Contact.VKID)); db.CategoryContacts.RemoveRange(removed); var result = Category; if (IsBirthday && Category is GeneralCategory) { result = new BirthdayCategory(Category); db.GeneralCategories.Remove(Category as GeneralCategory); db.BirthdayCategories.Add(result as BirthdayCategory); } else if (IsGeneral && Category is BirthdayCategory) { result = new GeneralCategory(Category, Date.Value.DateTime); db.BirthdayCategories.Remove(Category as BirthdayCategory); db.GeneralCategories.Add(result as GeneralCategory); } else if (IsGeneral) { (Category as GeneralCategory).Date = Date.Value.DateTime; } db.SaveChanges(); (result as ITaskGenerator).Regenerate(db); }
public void CanCreateGeneralCategoryFromBirthday() { var db = (App.Current as App).dbContext; try { var category = new BirthdayCategory() { Name = "Simple" }; category.CategoryContacts = new List <CategoryContact> { new CategoryContact() { Category = category, Contact = new Contact() { ScreenName = "Foobaar" } } }; db.Categories.Add(category); db.SaveChanges(); category = db.BirthdayCategories .Include(c => c.CategoryContacts) .ThenInclude(cc => cc.Category) .First(); var generalCategory = new GeneralCategory(category, DateTime.Now); db.BirthdayCategories.Remove(category); db.GeneralCategories.Add(generalCategory); db.SaveChanges(); generalCategory = db.GeneralCategories .Include(c => c.CategoryContacts) .ThenInclude(cc => cc.Category) .First(); Assert.NotEmpty(generalCategory.CategoryContacts); Assert.Equal(db.CategoryContacts.Count(), 1); Assert.Equal(db.Contacts.Count(), 1); } finally { db.Database.ExecuteSqlCommand("delete from [categories]"); db.Database.ExecuteSqlCommand("delete from [contacts]"); db.Database.ExecuteSqlCommand("delete from [categorycontacts]"); } }
private void Create(GratsDBContext db) { if (IsBirthday) { var result = new BirthdayCategory(Category); db.BirthdayCategories.Add(result); db.SaveChanges(); result.Generate(db); } else { var result = new GeneralCategory(Category, Date.Value.DateTime); db.GeneralCategories.Add(result); db.SaveChanges(); result.Generate(db); } }
public async void UpdateEvents(List <User> friends) { var db = (App.Current as App).dbContext; var generalCategories = db.GeneralCategories .Include(c => c.CategoryContacts) .ThenInclude(cc => cc.Contact); var birthdayCategories = db.BirthdayCategories .Include(c => c.CategoryContacts) .ThenInclude(cc => cc.Contact); //добавление генеральных событий CalendarEvents.Clear(); foreach (var category in generalCategories) { var color = Extensions.ColorExtensions.FromHex(category.Color); EventCalendarView val; try { val = new EventCalendarView { EventColor = color, EventDate = category.Date, Contacts = category, EventName = category.Name ?? "безымянное событие", ExistingCategory = true }; } catch { val = null; } if (val != null) { CalendarEvents.Add(val); } } //добавление дней рождений, для которых нет поздравлений foreach (var friend in friends) { BirthdayCategory cat = new BirthdayCategory(); cat.Color = "#00FFFFF0"; cat.Name = "День рождения пользователя " + friend.FirstName + " " + friend.LastName + "(поздравление не установлено)"; cat.CategoryContacts = new List <CategoryContact>() { new CategoryContact { Category = cat, Contact = new Model.Contact(friend) } }; EventCalendarView val1 = null, val2 = null; try { var date = DateTime.Parse(friend.BirthDate); var color = Color.FromArgb(255, 255, 255, 240); if (!IsInBithdayCategories(birthdayCategories, friend)) { val1 = new EventCalendarView { EventColor = color, EventDate = new DateTime(DateTime.Now.Year, date.Month, date.Day), Contacts = cat, EventName = cat.Name }; } var nextyearbd = new DateTime(DateTime.Now.Year + 1, date.Month, date.Day); if (nextyearbd <= DateTime.Now.AddYears(1)) { val2 = new EventCalendarView { EventColor = color, EventDate = nextyearbd, Contacts = cat, EventName = cat.Name } } ; } catch { val1 = val2 = null; } if (val1 != null) { CalendarEvents.Add(val1); } if (val2 != null) { CalendarEvents.Add(val2); } } //добавление дней рождений, для которых есть поздравление foreach (var category in birthdayCategories) { var color = Extensions.ColorExtensions.FromHex(category.Color); if (category.CategoryContacts != null) { foreach (var cont in category.CategoryContacts) { EventCalendarView val; try { val = new EventCalendarView { EventColor = color, EventDate = cont.Contact.Birthday.Value, Contacts = category, EventName = "День рождения пользователя " + cont.Contact.ScreenName + " (" + (category.Name ?? "безымянное событие") + ")", ExistingCategory = true }; } catch { val = null; } if (val != null) { CalendarEvents.Add(val); } } } } }