コード例 #1
0
        public IHttpActionResult PostAccomodation(AccomodationViewModel accomodationViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Client c = db.Clients.SingleOrDefault(x => x.FirstName + " " + x.LastName == accomodationViewModel.ClientName);

            if (c == null)
            {
                string[] parts     = accomodationViewModel.ClientName.Split(' ');
                Client   newClient = new Client()
                {
                    FirstName = parts.FirstOrDefault(), LastName = parts.LastOrDefault()
                };
                c = db.Clients.Add(newClient);
            }

            Accomodation accomodation = ViewModelMapper.ToModelAccomodations(accomodationViewModel);

            accomodation.Client = c;
            db.Accomodations.Add(accomodation);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = accomodation.AccomodationId }, ViewModelMapper.ToViewModelAccomodations(accomodation)));
        }
コード例 #2
0
        public IHttpActionResult PutAccomodation(int id, AccomodationViewModel accomodationViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != accomodationViewModel.AccomodationId)
            {
                return(BadRequest());
            }

            Accomodation accomodation = ViewModelMapper.ToModelAccomodations(accomodationViewModel);

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }