Esempio n. 1
0
        public HttpResponseMessage Put(int id, [FromBody] CampsViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }



            var camp      = Mapper.Map <CampsViewModel, Camp>(model);
            var existCamp = _campService.Find(id);

            existCamp.Phones.Clear();

            foreach (var item in camp.Phones)
            {
                // var find = _campService.Find(c => c.Phones.Any(x => x.PhoneNumber == item.PhoneNumber));
                var item1  = item;
                var result = _phoneService.Find(x => x.PhoneNumber == item1.PhoneNumber);
                _phoneService.Delete(result);
            }
            if (existCamp.AddressId != null)
            {
                var address = _addressService.Find(existCamp.AddressId.Value);
                existCamp.AddressId = null;
                existCamp.Address   = null;
                _addressService.Delete(address);
            }

            try
            {
                _unitOfWork.SaveChanges();

                camp.Address.CityId = camp.Address.City.Id;
                camp.Id             = id;
                camp.CampFacilities = null;
                camp.Galleries      = null;
                camp.Suites         = null;
                camp.Address.City   = null;

                //var existItem = _campService.Find(id);
                //if (existItem == null)
                //{
                //    return Request.CreateResponse(HttpStatusCode.BadRequest);
                //}

                //existItem.Address = camp.Address;


                _campService.Update(camp);


                // db.Entry(camp).State = EntityState.Modified;


                _unitOfWork.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CampExists(id))
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                else
                {
                    throw;
                }
            }

            return(Request.CreateResponse(HttpStatusCode.NoContent));
        }