public ActionResult Create(ContactsViewModel newContact, HttpPostedFileBase image) { Contact contact = new Contact(); contact.FirstName = newContact.FirstName; contact.LastName = newContact.LastName; contact.City = newContact.City; contact.Description = newContact.Description; contact.HomePhone = newContact.HomePhone; contact.MobilePhone = newContact.MobilePhone; contact.WorkPhone = newContact.WorkPhone; if (image != null) { contact.Image = new byte[image.ContentLength]; image.InputStream.Read(contact.Image, 0, image.ContentLength); } if (ModelState.IsValid) { _db.Contacts.Add(contact); _db.SaveChanges(); } return(RedirectToAction("Index")); }
public async Task TestContacts() { using (ContactsDB db = new ContactsDB()) { // Ensure Test Record does not exist Assert.IsTrue(PurgeTestRecord(db), "PurgeTestRecord unsuccessful"); // Query current database List <Contact> contactsList = await db.Contacts.ToListAsync(); int initialCount = contactsList.Count; // CREATE: Create new contact Assert.IsTrue(CreateTestContact(db), "CreateTestContact unsuccessful"); // READ: verify contact created contactsList = await db.Contacts.ToListAsync(); int currentCount = contactsList.Count; //Assert.Equals(currentCount, (initialCount + 1)); // FAILS Assert.IsTrue(currentCount == (initialCount + 1)); var resultSet = db.Contacts.Where(c => c.Name == TEST_NAME); currentCount = resultSet.Count(); //Assert.Equals(currentCount, 1); // FAILS Assert.IsTrue(currentCount == 1); Contact contact = resultSet.First(); Assert.AreEqual(contact.EMail, TEST_NAME + "@xyz.com"); Assert.AreEqual(contact.Phone1, "999-888-7777"); // UPDATE: verify record modification contact.Phone1 = "111-222-3333"; db.SaveChanges(); contactsList = await db.Contacts.ToListAsync(); resultSet = db.Contacts .Where(c => c.Name == TEST_NAME); contact = resultSet.First(); currentCount = resultSet.Count(); Assert.IsTrue(currentCount == 1); Assert.AreEqual(contact.Phone1, "111-222-3333"); // DELETE: verify record delection db.Contacts.Remove(contact); db.SaveChanges(); contactsList = await db.Contacts.ToListAsync(); currentCount = contactsList.Count; Assert.IsTrue(currentCount == initialCount); } }
public async Task <IActionResult> Put(int id, [FromBody] Contact contact) { using (var db = new ContactsDB()) { var c = db.Contacts.Find(contact.ContactID); if (c == null) { return(NotFound()); } db.Entry(c).CurrentValues.SetValues(contact); db.SaveChanges(); return(Ok()); //new BodyModelBinder().BindModelAsync(); if (await base.TryUpdateModelAsync(c)) { } else { return(BadRequest(ModelState)); } } }
public ActionResult EditNote([Bind(Include = "NoteId,Name,Text,Date")] Note note) { if (ModelState.IsValid) { //// This crashes with:[SqlException (0x80131904): //// The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Notes_dbo.Contacts_ContactId" //db.Entry(note).State = EntityState.Modified; //await db.SaveChangesAsync(); //return RedirectToAction("Index"); Note note2 = db.Notes.FirstOrDefault(n => n.NoteId == note.NoteId); note2.Text = note.Text; note2.Date = note.Date; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(note)); }
public async Task <IActionResult> Post([FromBody] Contact contact) { using (var db = new ContactsDB()) { if (contact == null || TryValidateModel(contact)) { return(BadRequest(ModelState)); } db.Contacts.Add(contact); db.SaveChanges(); return(Created("", contact.ContactID)); } }
private bool CreateTestContact(ContactsDB db) { bool result = false; try { Contact contact = new Contact(); contact.Name = TEST_NAME; contact.EMail = TEST_NAME + "@xyz.com"; contact.Phone1 = "999-888-7777"; db.Contacts.Add(contact); db.SaveChanges(); result = true; } catch (Exception ex) { Console.WriteLine(ex.Message); } return(result); }
//[TestInitialize] //public void SetupTest() //{ // Database.SetInitializer(new ContactsApp.Models.ContactsDBInitializer()); //} private bool PurgeTestRecord(ContactsDB db) { bool result = false; try { var resultSet = db.Contacts .Where(c => c.Name == TEST_NAME); int i = resultSet.Count(); if (resultSet.Count() > 0) { db.Contacts.RemoveRange(resultSet); db.SaveChanges(); } result = true; } catch (Exception ex) { Console.WriteLine(ex.Message); } return(result); }