コード例 #1
0
        public async Task <IHttpActionResult> Put(int key, Customer customer, ODataQueryOptions <Customer> options)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!(key == customer.Id))
            {
                return(BadRequest("The customer Id must match the key in the URI"));
            }
            if (options.IfMatch != null)
            {
                if (!_context.Customers.Where(c => c.Id == key).Any())
                {
                    // The entity doesn't exist on the database and as the request contains an If-Match header we don't
                    // insert the entity instead (No UPSERT behavior if the If-Match header is present).
                    return(NotFound());
                }
                else if (!((IQueryable <Customer>)options.IfMatch.ApplyTo(_context.Customers.Where(c => c.Id == key))).Any())
                {
                    // The ETag of the entity doesn't match the value sent on the If-Match header, so the entity has
                    // been modified by a third party between the entity retrieval and update..
                    return(StatusCode(HttpStatusCode.PreconditionFailed));
                }
                else
                {
                    // The entity exists in the database and the ETag of the entity matches the value on the If-Match
                    // header, so we update the entity.
                    _context.Entry(customer).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    return(Ok(customer));
                }
            }
            else
            {
                if (!_context.Customers.Where(c => c.Id == key).Any())
                {
                    // The request didn't contain any If-Match header and the entity doesn't exist on the database, so
                    // we create a new one. For more details see the section 11.4.4 of the OData v4.0 specification.
                    _context.Customers.Add(customer);
                    await _context.SaveChangesAsync();

                    return(base.Created(customer));
                }
                else
                {
                    // the request didn't contain any If-Match header and the entity exists on the database, so we
                    // update it's value.
                    _context.Entry(customer).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    return(Ok(customer));
                }
            }
        }
コード例 #2
0
        public async Task <IActionResult> PutCustomer([FromRoute] int id, [FromBody] Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != customer.CustID)
            {
                return(BadRequest());
            }

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


            try
            {
                _repo.Update(customer);
                var save = await _repo.SaveAsync(customer);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> PutCustomer(int id, Customer customer)
        {
            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());
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        public ActionResult Edit([Bind(Include = "DepartamentId,Name,Address,ManagerName")] Departament departament)
        {
            if (ModelState.IsValid)
            {
                departament.Manager = (from users in db.LoginUser
                                       where users.Name == departament.ManagerName
                                       select users).FirstOrDefault();
                try
                {
                    //I admit that it's not legal
                    var sqlCommand = String.Format("update dbo.Departaments set Manager_LoginUserId = {0} where DepartamentId = {1}", departament.Manager.LoginUserId, departament.DepartamentId);
                    db.Database.ExecuteSqlCommand(sqlCommand);
                    db.Entry(departament).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch
                {
                    ModelState.AddModelError("FullName", "Query error");
                    return(View(departament));
                }
            }

            return(View(departament));
        }
コード例 #6
0
        public async Task <IActionResult> PutCustomers([FromRoute] Guid id, [FromBody] Customers customers)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            return(NoContent());
        }
コード例 #7
0
ファイル: BaseRepository.cs プロジェクト: bradlak/Samples
        protected TEntity Update(TEntity entity)
        {
            var toUpdate = GetById(entity.Id);

            dbContext.Entry <TEntity>(toUpdate).CurrentValues.SetValues(entity);
            dbContext.SaveChanges();

            return(toUpdate);
        }
コード例 #8
0
 public ActionResult Edit([Bind(Include = "CustomerId,Fisrtname,LastName,Telephone,DOB")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
コード例 #9
0
 public ActionResult Edit([Bind(Include = "ContactDetailId,Name,Role,Phone,Mail")] ContactsDetail contactsDetail)
 {
     if (ModelState.IsValid)
     {
         db.Entry(contactsDetail).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(contactsDetail));
 }
コード例 #10
0
        public ActionResult Edit([Bind(Include = "CustomerId,Name,Address,Email,Phone,Password,Comments,IsMunicipalityCustomer,NumberOfSchools,OldName")] CustomerInformation customerInformation)
        {
            if (ModelState.IsValid)
            {
                UserActionsHelper.ChangeUserData(customerInformation.OldName, customerInformation.Name, customerInformation.Email, customerInformation.Password);
                customerInformation.OldName = customerInformation.Name;

                db.Entry(customerInformation).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(customerInformation));
        }
コード例 #11
0
 public bool Update(Customers customer)
 {
     try
     {
         //var entry = _ctx.Entry(product);
         //customer.CustomerID = CustomerID;
         _ctx.Set <Customers>().Attach(customer);
         _ctx.Entry(customer).State = EntityState.Modified;
         return(true);
     }
     catch
     {
         return(false);
     }
 }
コード例 #12
0
ファイル: CustomerService.cs プロジェクト: huangal/Banking
        public async Task <CustomerModel> UpdateCustomerAsync(CustomerModel customerModel)
        {
            var customer = await _context.Customers.FirstOrDefaultAsync(c => c.Id == customerModel.Id);

            if (customer == null)
            {
                return(null);
            }

            _context.Entry(customer).State = EntityState.Detached;
            customer = _mapper.Map <Customer>(customerModel);

            _context.Customers.Update(customer);
            await _context.SaveChangesAsync();

            return(_mapper.Map <CustomerModel>(customer));
        }
コード例 #13
0
        public ActionResult Edit([Bind(Include = "LoginUserId,Name,Mobile,Mail,UserName,Password,OldName,Departament,DepartamentName")] LoginUser loginUser)
        {
            var username = User.Identity.GetUserName();

            if (ModelState.IsValid)
            {
                UserActionsHelper.ChangeUserData(loginUser.OldName, loginUser.UserName, loginUser.Mail, loginUser.Password);
                loginUser.OldName = loginUser.UserName;

                loginUser.Departament = (from departament in db.Departament
                                         where departament.Name == loginUser.DepartamentName
                                         select departament).FirstOrDefault();

                try
                {
                    //I admit that it's not legal
                    var sqlCommand = String.Format("update dbo.LoginUsers set Departament_DepartamentId = {0} where LoginUserId = {1}", loginUser.Departament.DepartamentId, loginUser.LoginUserId);
                    db.Database.ExecuteSqlCommand(sqlCommand);
                    db.Entry(loginUser).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch
                {
                    ModelState.AddModelError("FullName", "Query error");
                    return(View(loginUser));
                }
            }
            var listOfDepartamets = (from departament in db.Departament
                                     where departament.Customer.Name == username
                                     select departament).ToList().Select(u => new SelectListItem
            {
                Text  = u.Name,
                Value = u.DepartamentId.ToString()
            });

            ViewBag.ListOfDepartaments = listOfDepartamets;
            return(View(loginUser));
        }
コード例 #14
0
        public async Task <IHttpActionResult> Put([FromODataUri] int key, [FromBody] Customer entity)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else if (key != entity.Id)
            {
                return(BadRequest("The key from the url must match the key of the entity in the body"));
            }
            var originalCustomer = await context.Customers.FindAsync(key);

            if (originalCustomer == null)
            {
                return(NotFound());
            }
            else
            {
                context.Entry(originalCustomer).CurrentValues.SetValues(entity);
                await context.SaveChangesAsync();
            }
            return(Updated(entity));
        }
コード例 #15
0
 /// <summary>
 /// Sets the original version.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="version">The version.</param>
 protected override void SetOriginalVersion(Customer model, byte[] version)
 {
     _context.Entry(model).OriginalValues["Version"] = version;
 }