public async Task <ActionResult> PostVendor([FromBody] VendorInModel vendorModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Contact contact = await _context.Contacts.SingleOrDefaultAsync(m => m.Email == vendorModel.ContactEmail); Vendor vendor = new Vendor { Contact = contact, Name = vendorModel.Name }; _context.Vendors.Add(vendor); await _context.SaveChangesAsync(); return(CreatedAtAction("GetVendor", new { id = vendor.Name }, vendor)); }
public async Task <ActionResult> PutVendor([FromRoute] string id, [FromBody] VendorInModel vendorInModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != vendorInModel.Name) { return(BadRequest()); } Contact contact = await _context.Contacts.FirstOrDefaultAsync(m => m.Email == vendorInModel.ContactEmail); Vendor vendor = new Vendor { Contact = contact, Name = vendorInModel.Name }; _context.Entry(vendor).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!VendorExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }