Esempio n. 1
0
        public async Task <IActionResult> Add(AddCategoryInputModel input)
        {
            try
            {
                if (!this.ModelState.IsValid)
                {
                    input.WalletName = await this.walletsService.GetWalletNameAsync(input.WalletId);

                    return(this.View(input));
                }

                var user = await this.userManager.GetUserAsync(this.User);

                if (!await this.walletsService.IsUserOwnWalletAsync(user.Id, input.WalletId))
                {
                    return(this.Redirect($"/Home/Error?message={GlobalConstants.NoPermissionForEditWallet}"));
                }

                await this.categoriesService.AddAsync(input.Name, input.WalletId, input.BadgeColor);

                return(this.Redirect($"/Wallets/Categories/{input.WalletId}"));
            }
            catch (Exception ex)
            {
                return(this.Redirect($"/Home/Error?message={ex.Message}"));
            }
        }
Esempio n. 2
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string categoryName = textName.Text;

                if (string.IsNullOrEmpty(categoryName))
                {
                    MessageBox.Show("Please enter category name!");
                    return;
                }

                if (categoryName.Length > 100)
                {
                    MessageBox.Show("Category name should be between 0 and 100 characters");
                    return;
                }

                AddCategoryInputModel inputModel = new AddCategoryInputModel
                {
                    Name = categoryName,
                };

                string inputModelJson = JsonConvert.SerializeObject(inputModel);

                categoryService.AddCategory(inputModelJson);
                MessageBox.Show("Successfully added new category");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Add(int id)
        {
            try
            {
                var user = await this.userManager.GetUserAsync(this.User);

                if (!await this.walletsService.IsUserOwnWalletAsync(user.Id, id))
                {
                    throw new ArgumentException(GlobalConstants.NoPermissionForEditWallet);
                }

                var model = new AddCategoryInputModel();

                var wallets = await this.categoriesService.GetAllWalletsWithNameAndIdAsync(user.Id);

                model.WalletId   = id;
                model.WalletName = await this.walletsService.GetWalletNameAsync(id);

                model.Wallets = wallets.Select(w => new AddCategoryWalletsListViewModel
                {
                    WalletId   = w.WalletId,
                    WalletName = w.WalletName,
                })
                                .ToList();

                return(this.View(model));
            }
            catch (Exception ex)
            {
                return(this.Redirect($"/Home/Error?message={ex.Message}"));
            }
        }
Esempio n. 4
0
        public CategoryViewModel AddCategory(AddCategoryInputModel input)
        {
            var viewModel = _mapper.Map <CategoryViewModel>(input);
            var category  = _categoryRepository.Add(_mapper.Map <Category>(viewModel));

            return(_mapper.Map <CategoryViewModel>(category));
        }
Esempio n. 5
0
        public async Task <IActionResult> Index(AddCategoryInputModel model)
        {
            var currentUser = await this.userManager.GetUserAsync(this.User);

            if (currentUser.IsBlocked)
            {
                this.TempData["Error"] = ErrorMessages.YouAreBlock;
                return(this.RedirectToAction("Index", "Blog"));
            }

            if (this.ModelState.IsValid)
            {
                var tuple = await this.addCategoryService
                            .CreateCategory(model.Name, model.Description = model.SanitizedDescription);

                this.TempData[tuple.Item1] = tuple.Item2;
            }
            else
            {
                this.TempData["Error"] = ErrorMessages.InvalidInputModel;
                return(this.RedirectToAction("Index", "Blog", model));
            }

            return(this.RedirectToAction("Index", "Blog"));
        }
        public async Task AddPostShoudCreateCategoryAndReturnRedirect()
        {
            // Arrange
            this.FillDatabase();

            this.inputModel = new AddCategoryInputModel
            {
                WalletId   = 5,
                Name       = "Test Category",
                BadgeColor = "Danger",
            };

            // Act
            var result = await this.controller.Add(this.inputModel);

            // Assert
            var redirectResult = Assert.IsType <RedirectResult>(result);

            var category = this.db.Categories.FirstOrDefault(c => c.Name.Contains("Test Category"));

            Assert.NotNull(category);
            Assert.Equal("Test Category", category.Name);
            Assert.Equal(5, category.WalletId);
            Assert.Equal(3, this.db.Wallets.FirstOrDefault(w => w.Id == 5).Categories.Count());
        }
Esempio n. 7
0
 public IActionResult UpdateCategory([FromBody] AddCategoryInputModel input, string id)
 {
     if (!input.IsValid())
     {
         return(BadRequest(input.ValidationMessage));
     }
     return(new OkObjectResult(_categoryAppService.Update(input.Name, id)));
 }
Esempio n. 8
0
        public async Task AddCategoryAsync(string inputModel)
        {
            AddCategoryInputModel model = JsonConvert.DeserializeObject <AddCategoryInputModel>(inputModel);

            string insertStatement = $"insert into category(is_del, name)values(0, '{model.Name}')";

            await CommandExecuter.ExecuteNonQuaryAsync(insertStatement);
        }
Esempio n. 9
0
        public IActionResult AddCategory(AddCategoryInputModel input)
        {
            if (!input.IsValid())
            {
                return(BadRequest(input.ValidationMessage));
            }
            var category = _categoryAppService.AddCategory(input);

            return(Created(new Uri($"{Request.Path}/{category.Id}", UriKind.Relative), category));
        }
Esempio n. 10
0
        public async Task <IActionResult> AddCategory(AddCategoryInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.categoryService.CreateAsync(input);

            this.TempData["Message"] = "Category added successfully";

            return(this.Redirect("/Forum/Forum"));
        }
Esempio n. 11
0
        public async Task <IActionResult> Index(AddCategoryInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                var tuple = await this.addCategoryService
                            .CreateCategory(model.Name, model.Description = model.SanitizedDescription);

                this.TempData[tuple.Item1] = tuple.Item2;
            }
            else
            {
                this.TempData["Error"] = ErrorMessages.InvalidInputModel;
            }

            return(this.RedirectToAction("Index", "Blog"));
        }
        public async Task <IActionResult> Create(AddCategoryInputModel inputModel)
        {
            if (this.categoryService.CategoryNameExists(inputModel.Name, true) == true)
            {
                this.ModelState.AddModelError("Name", "This category name has been already taken. Choose a different one");
            }

            if (this.ModelState.IsValid == false)
            {
                TempData[GlobalConstants.ErrorsFromPOSTRequest]     = ModelStateHelper.SerialiseModelState(this.ModelState);
                TempData[GlobalConstants.InputModelFromPOSTRequest] = JsonSerializer.Serialize(inputModel);

                return(this.RedirectToAction(nameof(Create)));
            }

            await this.categoryService.AddAsync(inputModel.Name);

            return(this.RedirectToAction(nameof(All)));
        }
Esempio n. 13
0
        public async Task CreateAsync(AddCategoryInputModel input)
        {
            string imageUrl = this.defaultCategoryPicture;

            if (input.PictureFile != null)
            {
                imageUrl = await this.cloudinaryService.UploudAsync(input.PictureFile);
            }

            var dbCategory = new Category
            {
                Name        = input.Name,
                Description = input.Description,
                ImageURL    = imageUrl,
            };

            await this.db.Categories.AddAsync(dbCategory);

            await this.db.SaveChangesAsync();
        }
        public async Task <IActionResult> Add(AddCategoryInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var isAdded = await this.categoriesService.AddAsync(input.Name, input.Picture, input.Description);

            if (isAdded)
            {
                this.TempData["InfoMessage"] = GlobalConstants.SuccessAddedMessage;
            }
            else
            {
                this.TempData["ErrorMessage"] = GlobalConstants.AlreadyExistingCategory;
            }

            return(this.RedirectToAction(nameof(this.GetAll)));
        }
        public async Task CreateMethodShouldAddCorrectNewCategoryToDb()
        {
            var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new ApplicationDbContext(optionsBuilder.Options);

            var categoryService = new CategoryService(dbContext, null);

            var categoryToAdd = new AddCategoryInputModel
            {
                Name        = "testName",
                Description = "testDescription",
            };

            await categoryService.CreateAsync(categoryToAdd);

            Assert.NotNull(dbContext.Categories.FirstOrDefaultAsync());
            Assert.Equal("testName", dbContext.Categories.FirstAsync().Result.Name);
            Assert.Equal("testDescription", dbContext.Categories.FirstAsync().Result.Description);
        }
        public IActionResult Add()
        {
            var model = new AddCategoryInputModel();

            return(this.View(model));
        }