/// <summary> /// TO DO !!!! /// </summary> /// <param name="address"></param> /// <returns></returns> public async Task <SimsAddress> Update(SimsAddress address) { var dbItem = await ctx.Addresses.Include(i => i.Contacts).SingleAsync(s => s.Id == address.Id); // Updates the address details this.mapper.Map(address, dbItem); //address.ToDb(dbItem); ctx.Addresses.Update(dbItem); // Main Contact. We don't have the key for contacins. // Currently (25/08/2020) you may only add one contact. // so hear we either update an existing contact (is always main), or add a new one. if (address.Contacts.Count() > 0) { var firstContact = address.Contacts.First(); if (dbItem.Contacts.Count() > 0) { var dbContact = dbItem.Contacts.First(); this.mapper.Map(firstContact, dbContact); //firstContact.ToDb(dbContact); } else { var newDbContact = mapper.Map <SimsAddressContact, AddressContactDb>(firstContact); dbItem.Contacts.Add(newDbContact); } await ctx.SaveChangesAsync(); } return(mapper.Map <AddressDb, SimsAddress>(dbItem)); }
public async Task AddAddress() { var testTitle = "Test - New orgnaisations"; var org = new SimsAddress { Title = testTitle, //MainContact = "New-Org Smity", AddressLine1 = "Address line 1", AddressLine2 = "Address line 2", PostCode = "Test post", TownCity = "Test Town City", CountryId = 201, County = "Country", TelephoneNumber = "01234567890", Contacts = new List <SimsAddressContact> { new SimsAddressContact { EmailAddress = "email@address", TelephoneNumber = "01234567890", Name = "New-Org Smity", IsMain = true } } }; using (var ctx = SeedingConfigData.GetDbContext(this.conn)) { var simsHost = SimsDbHost.CreateHost(ctx, this.mapper, this.userId); var savedAddress = await simsHost.Addresses.Add(org); Assert.True(savedAddress.Title == testTitle); } }
public Task <SimsAddress> Update(SimsAddress address) { if (address.Id == 0) { throw new SimsItemMissing("Address Id missing"); } return(dbHost.Addresses.Update(address)); }
public Task <SimsAddress> Add(SimsAddress address) { if (address.Id != 0) { throw new SimsItemExists("SimsAddress already exists"); } return(dbHost.Addresses.Add(address)); }
public async Task <SimsAddress> Add(SimsAddress address) { var newDbAddress = mapper.Map <SimsAddress, AddressDb>(address); //newDbItem.Contacts = address.Contacts.ToDb().ToList(); this.ctx.Addresses.Add(newDbAddress); await this.ctx.SaveChangesAsync(); return(mapper.Map <AddressDb, SimsAddress>(newDbAddress)); }