// GET: odata/Products(5
        //[EnableQuery(PageSize = 10, AllowedQueryOptions = AllowedQueryOptions.All)]
        //public SingleResult<Product> GetProducts([FromODataUri] int key)
        //{
        //    //return SingleResult.Create(db.Products.Where(products => products.Id == key));
        //}

        // PUT: api/Products
        public async Task<IHttpActionResult> PutProducts(Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                ProductViewModel prodVm = new ProductViewModel();
                prodVm.RelatedProducts = product.RelatedProducts;

                product.RelatedProducts = null;
                await productRepository.EditAsync(product);

                RelatedProductRepository relprod = new RelatedProductRepository(new MyRoomDbContext());

                relprod.DeleteProductRealted(product.Id);
                if (prodVm.RelatedProducts.Count() > 0)
                    relprod.InsertRelatedProducts(prodVm.RelatedProducts.ToList());
            }
            catch (Exception ex)
            {
                if (!ProductExists(product.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw ex;
                }
            }

            return Ok("Product Updated");
        }
        // POST: api/Products
        public IHttpActionResult PostProducts(ProductViewModel productViewModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            try
            {
                Product product = ProductMapper.CreateModel(productViewModel);
                productRepository.Insert(product);

                product.RelatedProducts = new List<RelatedProduct>();
                RelatedProductRepository relProdRepo = new RelatedProductRepository(new MyRoomDbContext());
                foreach (RelatedProduct rp in productViewModel.RelatedProducts)
                {
                    rp.IdProduct = product.Id;
                }
                relProdRepo.InsertRelatedProducts(productViewModel.RelatedProducts.ToList());

                //Inserta productos a ActiveHotelProduct
                ActiveHotelCatalogRepository hotelActiveRepo = new ActiveHotelCatalogRepository(relProdRepo.Context);
                //int hotelId = hotelActiveRepo.GetByCatalogId(productViewModel.CatalogId);
                
//                if (hotelId > 0)
  //              {
                    ActiveHotelProductRepository productHotelActiveRepo = new ActiveHotelProductRepository(relProdRepo.Context);
                    productHotelActiveRepo.Insert(new ActiveHotelProduct() { IdHotel = productViewModel.HotelId, IdProduct = product.Id, Active = true });
    //            }
                return Ok("Product Inserted");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 3
0
        public static Product CreateModel(ProductViewModel productViewModel)
        {
            Product product = new Product()
            {
                Name            = productViewModel.Name,
                Description     = productViewModel.Description,
                Price           = productViewModel.Price,
                Image           = productViewModel.Image,
                Type            = productViewModel.Type,
                Active          = productViewModel.Active,
                Prefix          = productViewModel.Prefix,
                UrlScanDocument = productViewModel.UrlScanDocument,
                Pending         = productViewModel.Pending,
                EmailMoreInfo   = productViewModel.EmailMoreInfo,
                Standard        = productViewModel.Standard,
                Premium         = productViewModel.Premium,
                Order           = productViewModel.Order,
                IdDepartment    = productViewModel.IdDepartment 
            };

            product.Translation = new Translation()
            {
                Spanish = productViewModel.Spanish,
                English = productViewModel.English,
                French = productViewModel.French,
                German = productViewModel.German,
                Language5 = productViewModel.Language5,
                Language6 = productViewModel.Language6,
                Language7 = productViewModel.Language7,
                Language8 = productViewModel.Language8,
                Active = productViewModel.TranslationActive,
            };

            product.TranslationDescription = new Translation()
            {
                Spanish = productViewModel.SpanishDesc,
                English = productViewModel.EnglishDesc,
                French = productViewModel.FrenchDesc,
                German = productViewModel.GermanDesc,
                Language5 = productViewModel.LanguageDesc5,
                Language6 = productViewModel.LanguageDesc6,
                Language7 = productViewModel.LanguageDesc7,
                Language8 = productViewModel.LanguageDesc8,
                Active = productViewModel.TranslationActiveDesc,
            };

            return product;
        }