public async Task <bool> AddAddress(AddAddressToContactDto dto)
        {
            using (var dbTran = _db.Database.BeginTransaction())
            {
                try
                {
                    var contact = _db.Contact.First(x => x.Id == dto.ContactId);

                    var address = new Address()
                    {
                        City     = dto.City,
                        PostCode = dto.PostCode,
                        Country  = dto.Country,
                        Line1    = dto.Line1,
                        Line2    = dto.Line2,
                        Line3    = dto.Line3,
                    };
                    _db.Address.Add(address);

                    var rel = new ContactAddress()
                    {
                        Address     = address,
                        Contact     = contact,
                        AddressType = dto.AddressType
                    };
                    _db.Set <ContactAddress>().Add(rel);

                    await _db.SaveChangesAsync();

                    dbTran.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    dbTran.Rollback();
                    //log exception
                    return(false);
                }
            }
        }
Example #2
0
        public async Task <IHttpActionResult> AddAddress([FromBody] AddAddressToContactDto dto)
        {
            var result = await _contactRepo.AddAddress(dto);

            return(Ok(result));
        }