Ejemplo n.º 1
0
        // POST api/Prices
        public HttpResponseMessage PostPrice(PriceDto price)
        {
            if (ModelState.IsValid)
            {
                Price priceEntity = price.ToEntity();
                db.Prices.Add(priceEntity);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, price);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = price.ID }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
Ejemplo n.º 2
0
        // PUT api/Prices/5
        public HttpResponseMessage PutPrice(int id, PriceDto price)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != price.ID)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            Price priceEntity = price.ToEntity();
            Location fromLocationEntity = db.Locations.Find(price.LocationFromId);
            Location toLocationEntity = db.Locations.Find(price.LocationToId);

            db.Entry(fromLocationEntity).State = EntityState.Detached;
            db.Entry(toLocationEntity).State = EntityState.Detached;
            db.Entry(priceEntity).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }