Пример #1
0
        public ActionResult Create()
        {
            var model = new ProductSaveViewModel {
                Heading = "Add new product"
            };

            return(View("ProductForm", model));
        }
Пример #2
0
        public async Task <ActionResult> Edit(long id)
        {
            var client   = new HttpClient();
            var response = await client.GetAsync(EndPointBase.EndPoint + "/api/products/" + id);

            response.EnsureSuccessStatusCode();
            var responBody = await response.Content.ReadAsStringAsync();

            var product = JsonConvert.DeserializeObject <Product>(responBody);
            var model   = new ProductSaveViewModel {
                Heading = "Update product"
            };

            Mapper.Map(product, model);
            return(View("ProductForm", model));
        }
        public void TestIfProductValidationByCreatingIsWorking()
        {
            var model = new ProductSaveViewModel
            {
                Description = "Some Desc",
                Title       = "Inv",
                File        = null,
                Price       = -100,
                ReleaseDate = DateTime.UtcNow.AddDays(-5)
            };

            var context           = new ValidationContext(model, null, null);
            var results           = new List <ValidationResult>();
            var isModelStateValid = Validator.TryValidateObject(model, context, results, true);

            Assert.IsTrue(results.Count == 5);
        }
Пример #4
0
        public async Task <ActionResult> Update(ProductSaveViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("ProductForm", model));
            }
            using (var client = new HttpClient())
            {
                var myContent   = JsonConvert.SerializeObject(model);
                var buffer      = System.Text.Encoding.UTF8.GetBytes(myContent);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var responseMessage = await client.PutAsync(EndPointBase.EndPoint + "/api/products/" + model.Id, byteContent);

                if (responseMessage.IsSuccessStatusCode)
                {
                    SetAlert("Data successfully saved.", "success");
                    return(RedirectToAction("Index"));
                }
                return(View("ProductForm", model));
            }
        }
        public async Task<ActionResult> Create(ProductSaveViewModel model)
        {
            if (model != null && ModelState.IsValid)
            {
                model.Title = this.sanitizeService.Sanitize(model.Title);
                model.Description = this.sanitizeService.Sanitize(model.Description);

                var newProduct = AutoMapper.Mapper.Map<Product>(model);

                if (model.File != null && model.File.ContentType == "image/jpeg" && model.File.ContentLength < 1048576)
                {
                    string filename = Path.GetFileName(model.File.FileName);
                    string folderPath = Server.MapPath("~/Content/Images/" + this.CurrentUser.Id);
                    string imagePath = folderPath + "/" + filename;
                    string imageUrl = "/Content/Images/" + this.CurrentUser.Id + "/" + filename;

                    if (!Directory.Exists(folderPath))
                    {
                        DirectoryInfo di = Directory.CreateDirectory(folderPath);
                    }

                    model.File.SaveAs(imagePath);
                    newProduct.Picture = imageUrl;
                }
                else
                {
                    return this.View(model);
                }                

                newProduct.UserId = this.CurrentUser.Id;

                var result = await this.productsService.AddProduct(newProduct);

                return this.RedirectToAction("Details", "Product", new { area = "Private", id = result.Id });
            }

            return this.View(model);
        }
Пример #6
0
        public async Task <ActionResult> Create(ProductSaveViewModel model)
        {
            if (model != null && ModelState.IsValid)
            {
                model.Title       = this.sanitizeService.Sanitize(model.Title);
                model.Description = this.sanitizeService.Sanitize(model.Description);

                var newProduct = AutoMapper.Mapper.Map <Product>(model);

                if (model.File != null && model.File.ContentType == "image/jpeg" && model.File.ContentLength < 1048576)
                {
                    string filename   = Path.GetFileName(model.File.FileName);
                    string folderPath = Server.MapPath("~/Content/Images/" + this.CurrentUser.Id);
                    string imagePath  = folderPath + "/" + filename;
                    string imageUrl   = "/Content/Images/" + this.CurrentUser.Id + "/" + filename;

                    if (!Directory.Exists(folderPath))
                    {
                        DirectoryInfo di = Directory.CreateDirectory(folderPath);
                    }

                    model.File.SaveAs(imagePath);
                    newProduct.Picture = imageUrl;
                }
                else
                {
                    return(this.View(model));
                }

                newProduct.UserId = this.CurrentUser.Id;

                var result = await this.productsService.AddProduct(newProduct);

                return(this.RedirectToAction("Details", "Product", new { area = "Private", id = result.Id }));
            }

            return(this.View(model));
        }
        public void TestIfProductValidationByCreatingIsWorking()
        {
            var model = new ProductSaveViewModel
            {
                Description = "Some Desc",
                Title = "Inv",
                File = null,
                Price = -100,
                ReleaseDate = DateTime.UtcNow.AddDays(-5)
            };

            var context = new ValidationContext(model, null, null);
            var results = new List<ValidationResult>();
            var isModelStateValid = Validator.TryValidateObject(model, context, results, true);

            Assert.IsTrue(results.Count == 5);
        }