Ejemplo n.º 1
0
        // POST api/phones
        public void Post(NewPhone newPhone)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("Adding {0}", newPhone));

            Manufacturer manufacturer = _db.Manufacturers.GetById(newPhone.ManufacturerId);
            if (manufacturer == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);

            Phone phone = new Phone {
                Manufacturer = manufacturer,
                Model = newPhone.Model,
                Description = newPhone.Description,
                Price = newPhone.Price,
                ImageFile = newPhone.ImageFile
            };

            var plans = _db.Plans.GetAll();
            foreach (Plan plan in plans) {
                if (newPhone.PlanIds.Contains(plan.PlanId)) {
                    phone.Plans.Add(plan);
                }
            }

            var apps = _db.Apps.GetAll();
            foreach (App app in apps) {
                if (newPhone.AppIds.Contains(app.AppId)) {
                    phone.Apps.Add(app);
                }
            }

            _db.Phones.Add(phone);
            _db.Commit();

            System.Diagnostics.Trace.WriteLine(string.Format("Added {0}", phone));
        }
Ejemplo n.º 2
0
        public void Put(int id, NewPhone phoneData)
        {
            var phone = _db.Phones.GetById(id);
            if (phone == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);
            var manufacturer = _db.Manufacturers.GetById(phoneData.ManufacturerId);
            if (manufacturer == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);

            phone.Manufacturer = manufacturer;
            phone.Model = phoneData.Model;
            phone.Description = phoneData.Description;
            phone.Price = phoneData.Price;

            phone.Plans.Clear();
            var plans = _db.Plans.GetAll();
            foreach (Plan plan in plans) {
                if (phoneData.PlanIds.Contains(plan.PlanId)) {
                    phone.Plans.Add(plan);
                }
            }

            phone.Apps.Clear();
            var apps = _db.Apps.GetAll();
            foreach (App app in apps) {
                if (phoneData.AppIds.Contains(app.AppId)) {
                    phone.Apps.Add(app);
                }
            }

            phone.Accessories.Clear();
            var accessories = _db.Accessories.GetAll();
            foreach (Accessory accessory in accessories) {
                if (phoneData.AccessoryIds.Contains(accessory.AccessoryId)) {
                    phone.Accessories.Add(accessory);
                }
            }
            _db.Commit();
        }