public ActionResult CreateProduct(NewProductModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("New", model));
            }

            var products = ProductService.GetProducts();

            if (products.Any(product => product.Name == model.Name))
            {
                ModelState.AddModelError("Name", "A product with this name was already found.");
                return(View("New", model));
            }

            try
            {
                var product = new Product
                {
                    Name        = model.Name,
                    Description = model.Description,
                    Url         = model.Url,
                    CreatedBy   = GetCurrentUser().Username
                };

                ProductService.CreateProduct(product);
            }
            catch (Exception ex)
            {
                QuietLog.LogHandledException(ex);
            }

            return(SafeRedirect(Url.AdminProducts()));
        }
Exemple #2
0
 private void ValidateProduct(NewProductModel newProduct)
 {
     if (string.IsNullOrEmpty(newProduct.Name))
     {
         ModelState.AddModelError("Name", "Please enter name");
     }
 }
Exemple #3
0
        public ActionResult AddNewProduct(int receiptId, NewProductModel newProductModel)
        {
            if (ModelState.IsValid)
            {
                var client  = new RestClient(WebConfigurationManager.AppSettings["webApiUrl"]);
                var request = authorizationService.GenerateAuthorizedRequest
                                  ("/receipts/" + receiptId + "/products/", Method.POST, HttpContext);

                request.RequestFormat = DataFormat.Json;
                request.AddJsonBody(newProductModel);

                var response = client.Execute(request);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(RedirectToAction("GetUserReceiptProducts"));
                }
                else
                {
                    ViewBag.WrongMessage = response.ErrorMessage;
                    return(View());
                }
            }

            return(View());
        }
Exemple #4
0
        public async Task <BaseModel> UpdateAsync(NewProductModel productModel, long id)
        {
            var resultModel = new BaseModel();

            if (productModel == null)
            {
                resultModel.Errors.Add(ModelIsNotValid);
            }

            var printingEdition = _mapper.NewProductModelToEntity(productModel);

            if (printingEdition == null)
            {
                resultModel.Errors.Add(PrintingEditionIsNotFound);
                return(resultModel);
            }
            printingEdition.Id = id;
            _authorInPrintingEditionRepository.RemoveByPrintingEdition(id);
            var resultAddRange = await _authorInPrintingEditionRepository.AddRange(productModel.AuthorsId, id);

            if (resultAddRange)
            {
                resultModel.Errors.Add(FailedCreatingСonnection);
            }
            var result = await _printingEditionRepository.Update(printingEdition);

            if (!result)
            {
                resultModel.Errors.Add(FailedToUpdatePrintingEdition);
            }
            return(resultModel);
        }
Exemple #5
0
        public async Task <BaseModel> CreateAsync(NewProductModel newProductModel)
        {
            var resultModel     = new BaseModel();
            var printingEdition = _mapper.NewProductModelToEntity(newProductModel);

            if (printingEdition == null)
            {
                resultModel.Errors.Add(ModelIsNotValid);
                return(resultModel);
            }

            var resultAdd = await _printingEditionRepository.Add(printingEdition);

            if (resultAdd == 0)
            {
                resultModel.Errors.Add(FailedToCreatePrintingEdition);
            }

            var resultAddRange = await _authorInPrintingEditionRepository.AddRange(newProductModel.AuthorsId, resultAdd);

            if (resultAddRange)
            {
                resultModel.Errors.Add(FailedCreatingСonnection);
            }
            return(resultModel);
        }
 public IActionResult Add(NewProductModel newProduct)
 {
     if (newProduct != null && newProduct.Name == null)
     {
         _productService.Add(newProduct);
     }
     return(RedirectToAction("Index"));
 }
Exemple #7
0
        public void Update(int id, NewProductModel productToUpdate)
        {
            var apiClient  = new RestClient("http://localhost:2534/api");
            var apiRequest = new RestRequest($"products/{id}", Method.PUT, DataFormat.Json);

            apiRequest.AddJsonBody(productToUpdate);
            var apiResponse = apiClient.Execute(apiRequest);
        }
        public IActionResult Index()
        {
            List <Product>  dbProducts = dbContext.products.ToList();
            NewProductModel viewModel  = new NewProductModel();

            viewModel.allProducts = dbProducts;
            return(View("Index", viewModel));
        }
Exemple #9
0
 public IActionResult Update(string id, NewProductModel product)
 {
     if (ModelState.IsValid)
     {
         _productService.Update(id, product);
     }
     return(RedirectToAction("Index"));
 }
Exemple #10
0
        public IActionResult Add(NewProductModel newProduct)
        {
            if (ModelState.IsValid)
            {
                _productService.Add(newProduct);
            }

            return(RedirectToAction("Index"));
        }
Exemple #11
0
        public IActionResult Add(NewProductModel newProduct)
        {
            if (!ModelState.IsValid)
            {
                return(View("Add", newProduct));
            }

            _productService.Add(newProduct);
            return(RedirectToAction("Index"));
        }
        public ProductModel Add(NewProductModel newProduct)
        {
            var apiClient  = new RestClient("http://localhost:5000/api");
            var apiRequest = new RestRequest("products", Method.POST, DataFormat.Json);

            apiRequest.AddJsonBody(newProduct);
            var apiResponse = apiClient.Execute <ProductModel>(apiRequest);

            return(apiResponse.Data);
        }
        public async Task <ActionResult> Create(NewProductModel model)
        {
            await _productService.Create(
                CallingContext.UserId,
                model.Name,
                model.SiteUrl,
                model.NotificationEmail,
                model.ActivationUrl);

            return(RedirectToAction("Index"));
        }
Exemple #14
0
        public IActionResult Add(NewProductModel newProduct)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            _productService.Add(newProduct);

            return(RedirectToAction("Index"));
        }
Exemple #15
0
 public IEnumerable <string> AddOrUpdateErrors(NewProductModel productModel)
 {
     if (productModel.Name == null || productModel.Name == string.Empty)
     {
         yield return("Name cannot be empty");
     }
     if (productModel.Category == null || productModel.Category == string.Empty)
     {
         yield return("Category cannot be empty");
     }
 }
        public PrintingEdition NewProductModelToEntity(NewProductModel modelItem)
        {
            var printingEdition = new PrintingEdition();

            printingEdition.Title       = modelItem.Title;
            printingEdition.Description = modelItem.Description;
            printingEdition.Price       = modelItem.Price;
            printingEdition.Currency    = (Currency)modelItem.Currency;
            printingEdition.Type        = (PrintingEditionType)modelItem.Type;
            return(printingEdition);
        }
        public async Task <IActionResult> Add(NewProductModel newProduct)
        {
            if (!ModelState.IsValid)
            {
                return(View(newProduct));
            }

            await _productService.Add(newProduct);

            return(RedirectToAction("Index"));
        }
        public IActionResult Add(NewProductModel newProduct)
        {
            if (ModelState.IsValid)
            {
                _productService.Add(newProduct);
            }
            else
            {
                return(View(viewName: nameof(Add)));
            }

            return(RedirectToAction("Index"));
        }
        public IActionResult Add(NewProductModel newProduct)
        {
            var errors = _validationService.AddOrUpdateErrors(newProduct);

            if (errors.Any())
            {
                newProduct.Errors = errors;
                return(View(newProduct));
            }

            _productService.Add(newProduct);
            return(RedirectToAction("Index"));
        }
Exemple #20
0
 public IActionResult Add(NewProductModel newProduct)
 {
     if (ModelState.IsValid)
     {
         _productService.Add(newProduct);
     }
     else
     {
         ModelState.AddModelError("", "Name is required!");
         return(View(newProduct));
     }
     return(RedirectToAction("Index"));
 }
        public void Edit(ProductModel productToEdit)
        {
            var             apiClient     = new RestClient("http://localhost:2534/api");
            var             apiRequest    = new RestRequest($"products/{productToEdit.Id}", Method.PUT, DataFormat.Json);
            NewProductModel productToSend = new NewProductModel
            {
                Name     = productToEdit.Name,
                Category = productToEdit.Category
            };

            apiRequest.AddJsonBody(productToSend);
            var apiResponse = apiClient.Execute <ProductModel>(apiRequest);
        }
Exemple #22
0
        public void Update(string id, NewProductModel newProduct)
        {
            var apiClient  = new RestClient("http://localhost:5000/api");
            var resource   = $"products/{id}";
            var apiRequest = new RestRequest(resource, Method.PUT, DataFormat.Json);

            apiRequest.AddJsonBody(newProduct);
            var apiResponse = apiClient.Execute <ProductModel>(apiRequest);

            if (!apiResponse.IsSuccessful)
            {
                throw new Exception(apiResponse.ErrorMessage);
            }
        }
        public Task <ProductModel> Add(NewProductModel newProduct)
        {
            var tcs = new TaskCompletionSource <ProductModel>();

            Task.Run(() =>
            {
                var apiClient  = new RestClient("http://localhost:2534/api");
                var apiRequest = new RestRequest("products", Method.POST, DataFormat.Json);
                apiRequest.AddJsonBody(newProduct);
                var apiResponse = apiClient.Execute <ProductModel>(apiRequest);
                tcs.SetResult(apiResponse.Data);
            });

            return(tcs.Task);
        }
        public Task <bool> Update(string id, NewProductModel updatedProduct)
        {
            var tcs = new TaskCompletionSource <bool>();

            Task.Run(() =>
            {
                var apiClient  = new RestClient("http://localhost:2534/api");
                var apiRequest = new RestRequest("products/" + id, Method.PUT, DataFormat.Json);
                apiRequest.AddJsonBody(updatedProduct);
                var apiResponse = apiClient.Execute(apiRequest);
                tcs.SetResult(apiResponse.StatusCode == System.Net.HttpStatusCode.OK);
            });

            return(tcs.Task);
        }
Exemple #25
0
 public ActionResult <Product> Create(NewProductModel product)
 {
     try
     {
         return(Ok(_productRepository
                   .Create(_mapper.Map <NewProductModel, Product>(product))));
     }
     catch (ArgumentNullException)
     {
         return(BadRequest());
     }
     catch (Exception ex)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
     }
 }
Exemple #26
0
        public async Task <IActionResult> New([FromForm] NewProductModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var product = new Product()
            {
                Name = model.Name, Price = model.Price
            };

            await _productRepository.Create(product);

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Update(ProductModel updatedProduct)
        {
            if (!ModelState.IsValid)
            {
                return(View(updatedProduct));
            }

            var id             = updatedProduct.Id;
            var productDetails = new NewProductModel()
            {
                Category = updatedProduct.Category,
                Name     = updatedProduct.Name
            };
            await _productService.Update(id, productDetails);

            return(RedirectToAction("Index"));
        }
 public IActionResult Add(NewProductModel newProduct)
 {
     _productService.Add(newProduct);
     return(RedirectToAction("Index"));
 }
        public async Task <IActionResult> Put([FromBody] NewProductModel model)
        {
            await _productService.AddNewProductAsync(_mapper.Map <NewProductDescriptor>(model));

            return(Ok());
        }
        public async Task AddOrUpdateProduct(NewProductModel newProductModel)
        {
            Product product = await FindOrCreateProduct(newProductModel.Name, newProductModel.Description, newProductModel.Image);

            await SaveNewPrice(product.Id, newProductModel.Price);
        }