public static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); //logger.Info(e.Message); static void Main(string[] args) { PersonSqlDbAccess.Add( new Models.Person.PersonModel { Firstname = "Mike", Lastname = "Corso", Age = 32, Gender = "Male", Address = new Models.Person.AddressModel { AddrLine1 = "32 West 32nd St", AddrLine2 = "Room 218", City = "New York", State = "New York", FK_Country = 1, Zipcode = "10003" }, ContactInfo = new Models.Person.ContactInfoModel { Number = "1234567890", Ext = "", Email = "*****@*****.**" } } ); Console.ReadLine(); }
public IHttpActionResult Post(PersonModel p) { if (p != null) { try { // Convert ISO2 to CountryModel objects p.Address.FK_Country = Guid.Empty; if (p.Address.CountryISO2 != null) { p.Address.FK_Country = CountrySqlDbAccess.GetByISO2(p.Address.CountryISO2).Id; } p.ContactInfo.FK_Country = Guid.Empty; if (p.Address.CountryISO2 != null) { p.ContactInfo.FK_Country = CountrySqlDbAccess.GetByISO2(p.ContactInfo.CountryISO2).Id; } // Create new Guid for Person, Address, ContactInfo p.Id = p.Address.Id = p.ContactInfo.Id = Guid.NewGuid(); PersonSqlDbAccess.Add(p); logger.Info($"PersonController.Post successfully added {p.Print()}"); return(Ok()); } catch (Exception ex) { logger.Info($"PersonController.Post threw error {ex.Message} trying to add {p.Print()}"); return(BadRequest()); } } else { logger.Info($"PersonController.Post failed to add {p.Print()}"); return(BadRequest()); } }
public void Test_PersonSqlDbCRUD() { Guid[] guids = new Guid[] { new Guid("F7AB131E-354C-4698-A673-BB27D0D0D64F"), new Guid("92115905-9A48-4A17-9521-E35560F74674") }; /* Create */ // DB before changes List <PersonModel> before = PersonSqlDbAccess.GetAll(); // Changes PersonSqlDbAccess.Add( new Models.Person.PersonModel { Id = guids[0], Firstname = "Mike", Lastname = "Corso", Age = 32, Gender = "Male", Address = new Models.Person.AddressModel { Id = Guid.NewGuid(), AddrLine1 = "32 West 32nd St", AddrLine2 = "Room 218", City = "New York", State = "New York", FK_Country = new Guid("DC9E0EF0-EC74-4E4D-A7DA-00754C3D2616"), Zipcode = "10003" }, ContactInfo = new Models.Person.ContactInfoModel { Id = Guid.NewGuid(), FK_Country = new Guid("DC9E0EF0-EC74-4E4D-A7DA-00754C3D2616"), Number = "1234567890", Ext = "", Email = "*****@*****.**" } } ); PersonSqlDbAccess.Add( new Models.Person.PersonModel { Id = guids[1], Firstname = "Samantha", Lastname = "Corso", Age = 25, Gender = "Female", Address = new Models.Person.AddressModel { Id = Guid.NewGuid(), AddrLine1 = "31 West 32nd St", AddrLine2 = "Room 217", City = "New York", State = "New York", FK_Country = new Guid("DC9E0EF0-EC74-4E4D-A7DA-00754C3D2616"), Zipcode = "10004" }, ContactInfo = new Models.Person.ContactInfoModel { Id = Guid.NewGuid(), FK_Country = new Guid("DC9E0EF0-EC74-4E4D-A7DA-00754C3D2616"), Number = "349667890", Ext = "", Email = "*****@*****.**" } } ); // DB after changes List <PersonModel> after = PersonSqlDbAccess.GetAll(); // Assert Assert.True(after.Count - before.Count == 2); /* Update */ // DB before changes before = PersonSqlDbAccess.Search("John"); // Changes PersonSqlDbAccess.Update( new Models.Person.PersonModel { Id = guids[0], Firstname = "John", Lastname = "Corso", Age = 32, Gender = "Male", Address = new Models.Person.AddressModel { Id = Guid.NewGuid(), AddrLine1 = "32 West 32nd St", AddrLine2 = "Room 218", City = "New York", State = "New York", FK_Country = new Guid("DC9E0EF0-EC74-4E4D-A7DA-00754C3D2616"), Zipcode = "10003" }, ContactInfo = new Models.Person.ContactInfoModel { Id = Guid.NewGuid(), FK_Country = new Guid("DC9E0EF0-EC74-4E4D-A7DA-00754C3D2616"), Number = "1234567890", Ext = "", Email = "*****@*****.**" } } ); // After changes after = PersonSqlDbAccess.Search("John"); // Assert Assert.True(after.Count - before.Count == 1); /* Delete */ // Changes PersonSqlDbAccess.Delete(guids[0]); PersonSqlDbAccess.Delete(guids[1]); // Assert Assert.True(PersonSqlDbAccess.GetPersonById(guids[0]) == null); Assert.True(PersonSqlDbAccess.GetPersonById(guids[1]) == null); }