public void AddContact(string name, string streetAndNumber, short zipCode, string city, Gender gender, DateTime birthDay, string phone, string mobile) { Contact newContact = new Contact() { Name = name, Address = new Address() { StreetNumber = streetAndNumber, AddressId = 1, ZipCode = zipCode }, Birthday = birthDay, Blocked = false, Categories = new List<Category>(), Gender = gender, Mobile = mobile, Phone = phone }; newContact.Address.Contact = newContact; ValidateContact(newContact); contactRepository.CreateContact(newContact); }
public void CreateContact(Contact contactToInsert) { IEnumerable<Contact> temp_contacts = contacts.OrderByDescending(c => c.ContactId); //IEnumerable because more lightweight int newId = temp_contacts.First().ContactId + 1; contactToInsert.ContactId = newId; contacts.Add(contactToInsert); }
private void ValidateContact(Contact contact) { List<ValidationResult> errors = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(contact, new ValidationContext(contact), errors, validateAllProperties: true); /*foreach (ValidationResult result in errors) { throw new ValidationException(result.ErrorMessage); // Leaving this code in, does not return multiple errors, stops after the first one. }*/ if (!valid) { throw new ValidationException(errors.First().ErrorMessage); // So we can get at least a single useful exception } }
public void ChangeContact(Contact contactToChange) { ValidateContact(contactToChange); contactRepository.UpdateContact(contactToChange); }
public void UpdateContact(Contact contactToUpdate) { Contact contact = contacts.Find(c => c.ContactId == contactToUpdate.ContactId); contact = contactToUpdate; // Not sure if this is any good, but it works... }
private void SeedContacts() { Contact c1 = new Contact() { ContactId = 1, Name = "Verstraten Micheline", Address = new Address() { AddressId = 0, StreetNumber = "Antwerpsestraat 10", ZipCode = 2000, City = "Antwerpen" }, Gender = Gender.Female, Birthday = new DateTime(1978, 08, 30), Phone = "03/123.45.67", Mobile = "0495/11.22.33", Blocked = false, Categories = new List<Category>() {categories.Find(c => c.Description == "Family")} //Lambda to check in every object in list his description }; c1.Address.Contact = c1; contacts.Add(c1); Contact c2 = new Contact() { ContactId = 2, Name= "Bogaerts Sven", Address = new Address() { AddressId = 1, StreetNumber = "Brusselstraat 10", ZipCode = 500, City = "Brussel" }, Gender = Gender.Male, Blocked = false, Birthday = new DateTime(1975, 4, 12), Mobile = "0478/12.34.56", Categories = new List<Category>() { categories.Find(c => c.Description == "Family"), categories.Find(c => c.Description == "School"), categories.Find(c => c.Description == "Sports") } //Could've just used the numbers, teacher told me to use this instead. }; c2.Address.Contact = c2; contacts.Add(c2); Contact c3 = new Contact() { ContactId = 3, Name = "Vlaeminckx Dieter", Address = new Address() { AddressId = 2, StreetNumber = "Gentsestraat 95", ZipCode = 9000, City = "Gent" }, Gender = Gender.Male, Blocked = true, //Stoute jongen Birthday = new DateTime(1980, 12, 8), Categories = new List<Category>() { categories.Find(c => c.Description == "Family"), categories.Find(c => c.Description == "Sports") } }; c3.Address.Contact = c3; contacts.Add(c3); }