public IActionResult CreateFromJson([FromBody] Customer customer)
        {
            if (ModelState.IsValid)
            {
                Customer customerToSave = null;
                bool     isNewCustomer  = true;
                if (customer.CustomerId == Guid.Empty)
                {
                    customer.CustomerId = Guid.NewGuid();
                    _context.Customer.Add(customer);
                    customerToSave      = new Customer();
                    customerToSave.Name = customer.Name;
                }
                else
                {
                    isNewCustomer  = false;
                    customerToSave = _context.Customer.Where(c => c.CustomerId == customer.CustomerId).Include(c => c.Contacts).Single();
                    //customerToSave.Name = customer.Name;
                    _context.Entry(customerToSave).CurrentValues.SetValues(customer);
                }

                foreach (var contact in customer.Contacts)
                {
                    if (contact.ContactId == Guid.Empty)
                    {
                        contact.ContactId = Guid.NewGuid();
                        customerToSave.Contacts.Add(contact);
                        _context.Contact.Add(contact);
                    }
                    else
                    {
                        var existingContact = _context.Contact.Where(c => c.ContactId == contact.ContactId).Single();
                        //existingContact.Name = contact.Name;
                        _context.Entry(existingContact).CurrentValues.SetValues(contact);
                    }
                }
                if (!isNewCustomer)
                {
                    var contactIds       = customer.Contacts.Select(c => c.ContactId);
                    var contactsToRemove = customerToSave.Contacts.Where(c => !contactIds.Contains(c.ContactId));
                    foreach (var contactToRemove in contactsToRemove)
                    {
                        _context.Contact.Remove(contactToRemove);
                    }
                }

                _context.SaveChanges();
            }
            return(Ok(_context.Customer.ToList()));
        }
        public IHttpActionResult PutCustomer(int id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.Id)
            {
                return(BadRequest());
            }

            db.Entry(customer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemple #3
0
        public async Task <IActionResult> PutCustomer([FromRoute] long id, [FromBody] Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.CustomerId)
            {
                return(BadRequest());
            }

            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #4
0
 public ActionResult Edit([Bind(Include = "CustomerID,name,surname,email,password,dob,phone_number")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
Exemple #5
0
 public ActionResult Edit([Bind(Include = "ID,LastName,FirstName,Address,Age")] Customers customers)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customers).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customers));
 }
Exemple #6
0
 public ActionResult Edit([Bind(Include = "ID,Name,Email,Phone,BirthDate")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
 public ActionResult Edit([Bind(Include = "CategoryId,CategoryName,CategoryDescription")] Category category)
 {
     if (ModelState.IsValid)
     {
         db.Entry(category).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(category));
 }
Exemple #8
0
 public ActionResult Edit(CustomerModel customermodel)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customermodel).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customermodel));
 }
Exemple #9
0
 public ActionResult Edit([Bind(Include = "CustomerID,Name,phonenumber,Email,DeliveryAddress")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
Exemple #10
0
 public ActionResult Edit([Bind(Include = "CustomerID,FirstName,LastName,CustomerAge,Phone,DrinkID,DrinkType")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
Exemple #11
0
 public ActionResult Edit([Bind(Include = "Guid,FullName,DateCreated,Amount,Ref")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
Exemple #12
0
 public ActionResult Edit([Bind(Include = "Id,ProductName,Price,Description,CategoryId")] Product product)
 {
     if (ModelState.IsValid)
     {
         db.Entry(product).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryName", product.CategoryId);
     return(View(product));
 }
        public async Task <IActionResult> PutCustomer(long id, Customer customer)
        {
            if (id != customer.Id)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                _context.Entry(customer).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }

            return(NoContent());
        }
Exemple #14
0
        public JsonResult UpdateProduct(Product product)
        {
            var result = false;

            try
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                result = true;
            }
            catch (Exception)
            {
                throw;
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
 public async Task <bool> Update(int id, Customer customer)
 {
     _context.Entry(customer).State = EntityState.Modified;
     try
     {
         await _context.SaveChangesAsync();
     }
     catch (DbUpdateConcurrencyException)
     {
         if (!CustomerExists(id))
         {
             return(false);
         }
         else
         {
             throw;
         }
     }
     return(true);
 }
Exemple #16
0
        public bool Update(Customer customer)
        {
            int isExecuted = 0;

            //Method 1
            //Student aStudent = db.Students.FirstOrDefault(c => c.ID == student.ID);
            //if (aStudent != null)
            //{
            //    aStudent.Name = student.Name;
            //    isExecuted = db.SaveChanges();
            //}


            //Method 2
            db.Entry(customer).State = EntityState.Modified;
            isExecuted = db.SaveChanges();
            if (isExecuted > 0)
            {
                return(true);
            }

            return(false);
        }
Exemple #17
0
 public ActionResult Edit(Customer customer)
 {
     db.Entry(customer).State = EntityState.Modified;
     db.SaveChanges();
     return(RedirectToAction("index"));
 }
 /// <summary>
 /// //In every Test Case the DBContext is a new Instance,
 /// due to this in order to get a successfull delete we detach the Entity to Update
 /// </summary>
 private void DetachEntity(int keyId) => _inMemoryDB.Entry(_inMemoryDB.Customers.Find(keyId)).State = EntityState.Detached;