// PUT api/<controller>/5
        public IHttpActionResult Put(HttpRequestMessage request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var      jsonString = request.Content.ReadAsStringAsync().Result;
            var      jsonObj    = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonString);
            Medicine medicine   = new Medicine
            {
                Id        = int.Parse(jsonObj["id"]),
                Full_Name = jsonObj["pFullName"],
                Address   = jsonObj["pAddress"],
                Disease   = jsonObj["pDisease"],
                Form      = jsonObj["pForm"],
                Treatment = jsonObj["pTreatment"],
                Title     = jsonObj["pTitle"],
                Dosage    = jsonObj["pDosage"],
                NumPack   = jsonObj["pNumPack"],
                PricePack = jsonObj["pPricePack"]
            };

            db.Entry(medicine).State = EntityState.Modified;
            db.SaveChanges();
            return(Ok(medicine));
        }
コード例 #2
0
        public bool UpdatePerson([FromBody] Person Person)
        {
            //_context.Entry(Person).State = EntityState.Modified;
            //_context.SaveChanges();

            using (var trans = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.Entry(Person).State = EntityState.Modified;

                    try
                    {
                        _context.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        return(false);
                    }

                    trans.Commit();
                }
                catch (Exception err)
                {
                    trans.Rollback();
                    return(false);
                }
            }

            return(true);
        }
コード例 #3
0
        public async Task <IActionResult> PutPerson(int id, Person person)
        {
            if (id != person.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
コード例 #4
0
        public IHttpActionResult PutPerson(int id, Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #5
0
 public ActionResult Edit(Person person)
 {
     if (ModelState.IsValid)
     {
         db.Entry(person).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(person));
 }
コード例 #6
0
        public async Task <IActionResult> Delete(int id)
        {
            var person = new Person {
                Id = id
            };

            _context.Attach(person);
            _context.Entry(person).State = EntityState.Deleted;

            await _context.SaveChangesAsync();

            return(NoContent());
        }
コード例 #7
0
        public async Task <ActionResult <Person> > Update(Person updatedPerson)
        {
            //Поиск существующего пользователя.
            Person person = await db.Persons.FindAsync(updatedPerson.Id);

            //Обновление всех данных кроме навыков.
            db.Entry(person).CurrentValues.SetValues(updatedPerson);

            //Навыки работника.
            var personSkills = person.Skills.ToList();

            foreach (var personSkill in personSkills)
            {
                //Ищем навыки которые были до изменений и остались после изменений.
                var skill = updatedPerson.Skills.SingleOrDefault(s => s.Name == personSkill.Name);
                if (skill != null)
                {
                    //Обновляем поле у навыка сотрудника.
                    db.Entry(personSkill).CurrentValues.SetValues(skill);
                }
                else
                {
                    //Удаляем, если навыка нет.
                    db.Remove(personSkill);
                }
            }

            //Добавляем новые навыки.
            foreach (var skill in updatedPerson.Skills)
            {
                if (personSkills.All(s => s.Name != skill.Name))
                {
                    person.Skills.Add(skill);
                }
            }
            await db.SaveChangesAsync();

            return(person);
        }
コード例 #8
0
        // PUT api/<controller>/5
        public IHttpActionResult Put(HttpRequestMessage request)
        {
            var  jsonString = request.Content.ReadAsStringAsync().Result;
            var  jsonObj    = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonString);
            Sick sick       = new Sick
            {
                Id        = int.Parse(jsonObj["id"]),
                Full_Name = jsonObj["pFullName"],
                Address   = jsonObj["pAddress"],
                Disease   = jsonObj["pDisease"],
                Form      = jsonObj["pForm"],
                Treatment = jsonObj["pTreatment"]
            };

            db.Entry(sick).State = EntityState.Modified;
            db.SaveChanges();
            return(Ok(sick));
        }