public EntityModel.Contact Add(EntityModel.Contact contact) { contact.CreatedDate = DateTime.Now; contact.UpdatedDate = DateTime.Now; _context.Add(contact); return(_context.SaveChanges() == 1 ? contact : null); }
public IEnumerable <EntityModel.Contact> GetAll(string zipcode) { var result = (from cont in _context.Contacts join addr in _context.Addresses on cont.ContactId equals addr.ContactId where addr.ZipCode == zipcode select new { Id = cont.ContactId, FN = cont.FirstName, LN = cont.LastName, BN = cont.BusinessName, Typ = cont.Type, Addrs = cont.Addresses.Where(z => z.ZipCode.Equals(zipcode)).ToList(), CD = cont.CreatedDate, UD = cont.UpdatedDate } ).Distinct().ToList(); var contacts = new List <EntityModel.Contact>(); EntityModel.Contact contact; foreach (var item in result) { contact = new EntityModel.Contact(); contact.ContactId = item.Id; contact.FirstName = item.FN; contact.LastName = item.LN; contact.BusinessName = item.BN; contact.Type = item.Typ; contact.Addresses = item.Addrs; contact.CreatedDate = item.CD; contact.UpdatedDate = item.UD; contacts.Add(contact); } return(contacts); }
public ContactsShould() { //Assign _loggerMocked = new Mock <ILogger <Appl.Contacts> >(); _contactRepoMocked = new Mock <IContactRepo>(); _mapperMocked = new Mock <IMapper>(); _addressAppModel1 = new ApplModels.Address { ContactId = contactId11, Street = "124", City = "Team city", State = "Hello State", ZipCode = zipcode1 }; _appContact1 = new ApplModels.Contact { FirstName = "John", LastName = "Doe", BusinessName = "NA" }; _contact1 = new DomainModels.Contact { ContactId = 1, FirstName = "John", LastName = "Doe", BusinessName = "NA", Addresses = new List <DomainModels.Address> { new DomainModels.Address { Street = "street", City = "city", State = "state", ContactId = 1, ZipCode = "123456" } }, CreatedDate = DateTime.Now.AddDays(-5), UpdatedDate = DateTime.Now.AddDays(-4) }; _contact2 = new DomainModels.Contact { ContactId = 2, FirstName = "BoA", LastName = "BoA", BusinessName = "Bank Of America", CreatedDate = DateTime.Now.AddMonths(-5), UpdatedDate = DateTime.Now }; _mapperMocked.Setup(x => x.Map <ApplModels.Address>(It.IsAny <DomainModels.Address>())) .Returns((DomainModels.Address source) => new ApplModels.Address() { AddressId = 123, ContactId = 11, Street = "2727", City = "Houston", State = "Texas", ZipCode = "123456" }); _mapperMocked.Setup(x => x.Map <ApplModels.Contact>(It.IsAny <DomainModels.Contact>())) .Returns((DomainModels.Contact source) => new ApplModels.Contact() { ContactId = 1, FirstName = FirstName, LastName = LastName, BusinessName = BusinessName }); _mapperMocked.Setup(x => x.Map <IEnumerable <ApplModels.Address> >(It.IsAny <IEnumerable <DomainModels.Address> >())) .Returns((List <DomainModels.Address> source) => new List <ApplModels.Address>() { new ApplModels.Address { AddressId = addressId1, ContactId = 11, Street = "2727", City = "Houston", State = "Texas", ZipCode = zipcode1 }, new ApplModels.Address { AddressId = addressId2, ContactId = contactId11, Street = "2727", City = "Austin", State = "Texas", ZipCode = zipcode2 }, new ApplModels.Address { AddressId = addressId3, ContactId = 11, Street = "1111", City = "Dallas", State = "Texas", ZipCode = zipcode1 } }); _mapperMocked.Setup(x => x.Map <IEnumerable <ApplModels.Contact> >(It.IsAny <IEnumerable <DomainModels.Contact> >())) .Returns((List <DomainModels.Contact> source) => new List <ApplModels.Contact>() { new ApplModels.Contact { ContactId = 1, FirstName = "John", LastName = "Doe", BusinessName = "NA", Addresses = new List <ApplModels.Address> { new ApplModels.Address { ContactId = 1, Street = "Street", City = "City" } } }, new ApplModels.Contact { ContactId = 2, FirstName = "BoA", LastName = "BoA", BusinessName = "Bank Of America" } }); _contacts = new List <DomainModels.Contact> { _contact1, _contact2 }; }