Exemple #1
0
        public async Task <int> AddProductTypeAsync(AddProductTypeBindingModel model)
        {
            var existingType = this.DbContext
                               .ProductTypes
                               .FirstOrDefault(x => x.TypeName == model.TypeName);

            if (existingType != null)
            {
                return(ErrorId);
            }

            var type = this.Mapper.Map <ProductType>(model);

            await this.DbContext.ProductTypes.AddAsync(type);

            await this.DbContext.SaveChangesAsync();

            return(type.Id);
        }
        public async Task <IActionResult> AddProductType(AddProductTypeBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                SetErrorMessage(CommonConstants.DangerMessage);

                return(this.AddProductType());
            }

            int generatedId = await this.productService.AddProductTypeAsync(model);

            if (generatedId < 1)
            {
                SetErrorMessage(CommonConstants.DuplicateMessage);

                return(this.AddProductType());
            }

            SetSuccessMessage(string.Format(CommonConstants.SuccessMessage, CommonConstants.ProductTypeDisplay));

            return(RedirectToAction(RedirectConstants.ProductTypeDetailsSuffix, RedirectConstants.ProductsSuffix, generatedId));
        }