Beispiel #1
0
 /// <summary>
 /// It's possible to validate input values using DataAnnotation &amp; ModelState mechanism
 /// however I choose simpler way here
 /// </summary>
 /// <param name="pe"></param>
 /// <returns></returns>
 private void InputValidation(CreateUpdateProductViewModel pe)
 {
     if (!(pe.Term == "annually" || pe.Term == "monthly"))
     {
         ModelState.AddModelError(string.Empty, "Term allowed values annually or monthly");
     }
     if (string.IsNullOrEmpty(pe.Name))
     {
         ModelState.AddModelError(string.Empty, "Name should not be empty");
     }
 }
Beispiel #2
0
        public IActionResult Create(CreateUpdateProductViewModel createUpdateProductViewModel)
        {
            if (User.Identity.Name != "*****@*****.**")
            {
                return(RedirectToAction("Index", "Home"));
            }

            var categories       = _categoryRepository.AllCategories.ToList();
            var selectedCategory = categories.FirstOrDefault(c => c.CategoryName == createUpdateProductViewModel.CategoryName);

            createUpdateProductViewModel.Product.CategoryId = selectedCategory.CategoryId;

            createUpdateProductViewModel.Product.ImageThumbnailUrl = createUpdateProductViewModel.Product.ImageUrl;

            _productRepository.CreateProduct(createUpdateProductViewModel.Product);

            return(RedirectToAction("Index", "ProductsTable"));
        }
Beispiel #3
0
        public async Task <ActionResult <ProductViewModel> > PostAsync(CreateUpdateProductViewModel pe)
        {
            _logger.LogDebug("Post {0} {1} {2} {3}", pe.Name, pe.IsActive, pe.Term, pe.BrandId);

            InputValidation(pe);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await _repository.CreateProductAsync(pe.Name, pe.IsActive, pe.Term, pe.BrandId);

            if (!result)
            {
                return(BadRequest(new ProblemDetails()
                {
                    Detail = result.Message
                }));
            }

            return(CreatedAtRoute("Product" + nameof(GetAsync), new { id = result.Data.ProductId }, _mapper.Map <ProductViewModel>(result.Data)));
        }
Beispiel #4
0
        public async Task <ActionResult <ProductViewModel> > PutAsync(int id, CreateUpdateProductViewModel pe)
        {
            _logger.LogDebug("Put {0} {1} {2} {3} {4}", id, pe.Name, pe.IsActive, pe.Term, pe.BrandId);

            InputValidation(pe);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await _repository.UpdateProductAsync(id, pe.Name, pe.IsActive, pe.Term, pe.BrandId);

            if (!result)
            {
                return(BadRequest(new ProblemDetails()
                {
                    Detail = result.Message
                }));
            }

            return(Ok(_mapper.Map <ProductViewModel>(result.Data)));
        }