Пример #1
0
        // POST api/Products
        public HttpResponseMessage PostProduct(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, product);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = product.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Пример #2
0
        public virtual ActionResult Create(Product product, string pictureIds, int[] tagIds)
        {
            if (ModelState.IsValid) {
                // Produkt mit den Bildern anreichern.
                var guids = _stringToGuids(pictureIds).ToArray();

                _SetProductPictures(product, guids);
                _SetProductTags(product, tagIds);

                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(product);
        }
Пример #3
0
        // PUT api/Products/5
        public HttpResponseMessage PutProduct(int id, Product product)
        {
            if (ModelState.IsValid && id == product.Id)
            {
                db.Entry(product).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Пример #4
0
        /// <summary>
        /// Bilder zum Produkt hinzufügen.
        /// </summary>
        /// <param name="product"></param>
        /// <param name="pictureIds">Komma-getrennte Guids.</param>
        private void _SetProductPictures(Product product, IEnumerable<Guid> guids)
        {
            var gs = guids.ToArray();

            // Erstmal alle Bilder vom Produkt entfernen, die nicht mehr dazu gehören.
            var toRemove = product.Pictures.Where(p => !guids.Contains(p.Id)).ToArray();
            foreach (var p in toRemove) { product.Pictures.Remove(p); }

            for(var i=0; i<gs.Count(); i+=1) {
                var id = gs[i];
                var pp = product.Pictures.FirstOrDefault(p => p.Id == id);
                if (pp == null) {
                    pp = new Picture() {
                        ProductId = product.Id,
                        //Picture = _GetPictureById(id)
                    };

                    throw new NotImplementedException();

                    product.Pictures.Add(pp);
                }

                // Bilder reihen.
                pp.Pos = i;
            }
        }
Пример #5
0
        /// <summary>
        /// Tags zum Produkt hinzufügen.
        /// </summary>
        /// <param name="product"></param>
        /// <param name="tagIds"></param>
        private void _SetProductTags(Product product, int[] tagIds) 
        {
            var toRemove = product.Tags.Where(p => !tagIds.Contains(p.Id)).ToArray();
            foreach (var t in toRemove) { product.Tags.Remove(t); }

            foreach (var id in (tagIds ?? new int[]{})) {
                if (!product.Tags.Any(t => t.Id == id)) {
                    product.Tags.Add(db.Tags.Find(id));
                }
            }
        }