// POST api/values public object Post(CustomerViewModel value) { //Validate something about your input if (value == null) throw new ArgumentException(); using (var crmContext = new CrmContext()) { //Find the record in the DB Customer customer = crmContext.Customers.Find(value.CustomerId); if (customer != null) { //Update the record value.ApplyToCustomer(customer); //Save the crmContext back crmContext.SaveChanges(); return new { Success = true, ViewModel = new CustomerViewModel(customer) }; } } return new { Success = false }; }
// GET api/values public IEnumerable<CustomerViewModel> Get() { Database.SetInitializer(new InitializerWithTestData()); using (var context = new CrmContext()) { return context.Customers.ToList().Select(customer => new CustomerViewModel(customer)).ToList(); } }
// POST new notes public object Post(NoteToCustomerModel value) { //Validate something about your input if (value == null) throw new ArgumentException(); if (value.Content == "") throw new ArgumentException(); var note = value.Content.Trim(' '); using (var crmContext = new CrmContext()) { //Find the record in the DB var customer = crmContext.Customers.Find(value.CustomerId); if (customer != null) { var newNote = new Note(note); customer.Notes.Add(newNote); crmContext.SaveChanges(); return new { Success = true, ViewModel = new CustomerViewModel(customer) }; } return new { Success = false, ViewModel = "" }; } }