Exemplo n.º 1
0
        public IActionResult Edit(CategoryEditModel model)
        {
            if (ModelState.IsValid)
            {
                var c = CategoryRepository.Read(model.Id);
                if (null == c)
                {
                    return(NotFound());
                }

                var cat = new Category()
                {
                    Id          = model.Id,
                    Title       = model.Title,
                    RouteName   = model.RouteName,
                    Description = model.Description
                };

                var rows = CategoryRepository.Update(cat);
                if (rows > 0)
                {
                    return(RedirectToAction(nameof(Manage)));
                }
                ModelState.AddModelError(string.Empty, "Create Category Failed on Data Layer.");
            }
            return(View());
        }
        public async Task <IActionResult> FavoriteCatEdit(CategoryEditModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _selectItemsServices.UpdateCategory(model);

                if (result)
                {
                    SuccessNotification("The favorite category data has been updated successfully.");
                    return(RedirectToAction("FavoriteCatEdit", "SelectItems",
                                            new { BOSOID = model.BosoId }));
                }
                else
                {
                    ModelState.AddModelError("", "Something went wrong while saving record.");
                }
            }

            //Redisplay Form
            model = await _selectItemsServices.GetEditCategory(model.BosoId);

            foreach (var item in (await _productManagementService.GetCategories((int)_workContext.CurrentCustomer.ClientId)))
            {
                model.AvailableCategories.Add(new SelectListItem {
                    Text = item.Category1, Value = item.Category1
                });
            }

            return(View(model));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> UpdateCategory(CategoryEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var recCategory = await _context.Categories.FirstOrDefaultAsync(q => q.CategoryId == model.CategoryId);

                    // Get the category entity
                    var record = await _context.Categories.FirstOrDefaultAsync(q => q.CategoryId != model.CategoryId && q.Slug == model.Slug);

                    if (record == null)
                    {
                        // Update unmapped properties
                        recCategory.Name         = model.Name;
                        recCategory.Slug         = model.Slug;
                        recCategory.DateModified = DateTime.UtcNow;

                        // Update
                        _context.Categories.Update(recCategory);
                        await _context.SaveChangesAsync();

                        return(Json(new { success = true }));
                    }
                    ModelState.AddModelError("slug", "Slug must be unique");
                }
                catch
                {
                    ModelState.AddModelError("", "An error occurred while updating the category");
                }
            }

            return(PartialView("_CategoryRecord", model));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(CategoryEditModel model)
        {
            if (ModelState.IsValid)
            {
                var seller = await HttpContext.GetMemberAsync();

                var category = await _categoryService.GetAsync(new CategoryFilter()
                {
                    SellerId = seller.Id, CategoryId = model.Id
                });

                if (category != null)
                {
                    await _appService.PrepareCategoryAsync(category, model);

                    category.SellerId = seller.Id;
                    await _categoryService.UpdateAsync(category);
                    await SaveCategoryImage(category, model);
                    await SaveCategoryTags(category, model);

                    TempData.AddAlert(AlertMode.Notify, AlertType.Success, $"\"{category.Name}\" category was updated.");
                }
            }

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> AddCategory(CategoryEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var category = _mapper.Map <Category>(model);

                    var record = await _context.Categories.FirstOrDefaultAsync(q => q.CategoryId != model.CategoryId && q.Slug == model.Slug);

                    if (record == null)
                    {
                        // Populate unmapped properties
                        category.Slug        = model.Slug;
                        category.DateCreated = DateTime.UtcNow;

                        // Add
                        _context.Categories.Add(category);
                        await _context.SaveChangesAsync();

                        return(Json(new { success = true }));
                    }
                    ModelState.AddModelError("slug", "Slug must be unique");
                }
                catch
                {
                    ModelState.AddModelError("", "An error occurred while adding the category");
                }
            }
            return(PartialView("_CategoryRecord", model));
        }
 public ActionResult Create(CategoryEditModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             db.Category.Add(model.Category);
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         catch (DbUpdateException dbex)
         {
             var ex = (SqlException)dbex.InnerException.InnerException;
             foreach (SqlError error in ex.Errors)
             {
                 if (error.Number == 50000)
                 {
                     ModelState.AddModelError("", error.Message);
                 }
             }
         }
     }
     model.CategorySelectList = GetCategorySelectLists(model.Category.ParentID);
     return(View(model));
 }
Exemplo n.º 7
0
        public async Task <IActionResult> Create([FromBody] CategoryEditModel category)
        {
            if (ModelState.IsValid)
            {
                CategoryResultModel response = await this.categoryService.UpdateCategory(category.Id, category.Name, category.Description);

                if (!response.Success)
                {
                    FailedResponseModel badResponse = new FailedResponseModel()
                    {
                        Errors = response.Errors
                    };

                    return(BadRequest(badResponse));
                }

                CategorySuccessResponseModel successResponse = new CategorySuccessResponseModel()
                {
                    Name = response.Name
                };

                return(Ok(successResponse));
            }

            return(BadRequest(new FailedResponseModel {
                Errors = ModelState.Values.SelectMany(x => x.Errors.Select(y => y.ErrorMessage))
            }));
        }
Exemplo n.º 8
0
        public ActionResult Edit(int id, CategoryEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var category = moneyRepository.GetCategory(model.Id);

            if (category == null)
            {
                return(NotFound());
            }

            if (category.UserId != CurrentUserId)
            {
                return(Unauthorized());
            }

            moneyRepository.UpdateCategory(new Category
            {
                Id    = model.Id,
                Name  = model.Name,
                Color = model.Color
            });

            return(Ok());
        }
Exemplo n.º 9
0
        public ActionResult Edit(CategoryEditModel model)
        {
            if (ModelState.IsValid)
            {
                var category = db.Categories.Where(m => m.Id == model.Id)
                               .FirstOrDefault();

                if (category != null)
                {
                    category.Title = model.Title;
                }
                else
                {
                    category = new Category
                    {
                        Title = model.Title
                    };

                    db.Categories.Add(category);
                }

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Exemplo n.º 10
0
        public async Task PrepareModelAsync(CategoryEditModel model, Category category, User seller)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (category != null)
            {
                model = _mapper.Map(category, model);
                model.TagNames.AddRange(category.Tags.Select(x => x.Name));
            }
            else
            {
                model.Published = true;
            }

            var tagNames = (await _tagService.ListAsync(new TagFilter()
            {
                SellerId = seller.Id
            })).Select(x => x.Name).Distinct();

            model.TagOptions.AddRange(SelectListHelper.GetSelectList(tagNames, x => new SelectListItem <string>(text: x, value: x)));

            var icons = await GenerateFa5IconsAsync();

            model.IconOptions.AddRange(SelectListHelper.GetSelectList(icons, x => new SelectListItem <string>(text: x.Humanize(LetterCasing.Title), value: x, selected: x == model.Icon), defaultText: "No Icon"));
        }
Exemplo n.º 11
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var category = this.Data.Categories.GetById(id);

            if (category == null)
            {
                return(this.HttpNotFound());
            }

            var sections = this.Data.Sections.All().ProjectTo <SectionConciseViewModel>().ToList();
            var model    = new CategoryEditModel
            {
                Id          = category.Id,
                Title       = category.Title,
                Description = category.Description,
                SectionId   = category.SectionId,
                IsDeleted   = category.IsDeleted,
                Sections    = new SelectList(sections, "Id", "Title", category.SectionId)
            };

            return(this.View(model));
        }
Exemplo n.º 12
0
        public ActionResult Add(CategoryEditModel model)
        {
            var newCategory = new Category
            {
                Name            = model.Name,
                FriendlyUrl     = model.FriendlyUrl,
                MetaTitle       = model.MetaTitle,
                MetaDescription = model.MetaDescription,
                MetaKeyWords    = model.MetaKeyWords
            };

            if (this.ShopData.Categories.All().Any(c => c.FriendlyUrl == model.FriendlyUrl || c.Name == model.Name))
            {
                this.ModelState.AddModelError(string.Empty, string.Format("Seo Friendly Url & Name must be unique!"));
            }

            if (this.ModelState.IsValid)
            {
                this.ShopData.Categories.Add(newCategory);
                this.ShopData.SaveChanges();
                this.ClearCache();
                return(this.RedirectToAction("Index"));
            }

            return(this.View(model));
        }
Exemplo n.º 13
0
        public ActionResult Create()
        {
            var viewModel = new CategoryEditModel
            {
                Category = new CategoryDto()
            };

            return(View(viewModel));
        }
Exemplo n.º 14
0
        public ActionResult EditCategory(int id)
        {
            var model = new CategoryEditModel
            {
                Category   = _categoryService.GetCategoryModel(id),
                Categories = _categoryService.GetBaseProductCategories()
            };

            return(View("~/Areas/Admin/Views/Category/AddCategory.cshtml", model));
        }
Exemplo n.º 15
0
        public async Task Update(string id, CategoryEditModel model)
        {
            Category category = this.context.Categories
                                .FirstOrDefault(s => s.Id == id);

            model.To <Category>(category);

            this.context.Categories.Update(category);
            await this.context.SaveChangesAsync();
        }
Exemplo n.º 16
0
        /// <summary>
        /// Maps a CategoryEditModel into a specific Category
        /// </summary>
        /// <param name="item"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        public static Category MapToModel(this CategoryEditModel item, Category category)
        {
            category.Id          = item.Id;
            category.VanityId    = item.VanityId;
            category.Name        = item.Name;
            category.Description = item.Description;
            category.Slug        = item.Slug;

            return(category);
        }
Exemplo n.º 17
0
        public async Task <IActionResult> CreateAsync(CategoryEditModel categoryEditModel)
        {
            if (!ModelState.IsValid)
            {
                SetupMessages("Categories", PageType.Create, panelTitle: "Create a new category");
                return(View("CreateEdit", categoryEditModel));
            }

            return(await SaveAsync(categoryEditModel, nameof(CategoryController.Create)));
        }
Exemplo n.º 18
0
        public async Task <IActionResult> Add()
        {
            var seller = await HttpContext.GetMemberAsync();

            var model = new CategoryEditModel();

            await _appService.PrepareModelAsync(model, null, seller);

            return(View(nameof(Edit), model));
        }
Exemplo n.º 19
0
 /// <summary>
 /// Maps a CategoryEditModel into a Category
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public static Category MapToModel(this CategoryEditModel item)
 {
     return(new Category
     {
         Id = item.Id,
         VanityId = item.VanityId,
         Name = item.Name,
         Description = item.Description,
         Slug = item.Slug,
     });
 }
Exemplo n.º 20
0
        public async Task <IActionResult> Edit(CategoryEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            await _commands.Send(new CreateCategoryCommand { Id = model.Id, Name = model.Name });

            return(RedirectToAction("Index", new { id = model.Id }));
        }
        public async Task <IActionResult> Put(string id, CategoryEditModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest());
            }

            await this.categoryService.Update(id, model);

            return(this.Ok());
        }
Exemplo n.º 22
0
        public async Task<IActionResult> Edit(CategoryEditModel model)
        {
            var request = new UpdateCatRequest
            {
                RouteName = model.RouteName,
                Note = model.Note,
                DisplayName = model.DisplayName
            };

            await _catService.UpdateAsync(model.Id, request);
            return Ok(model);
        }
        public ActionResult Create(CategoryEditModel model)
        {
            this.CheckPrices(model.MinPrice, model.MaxPrice);

            if (!this.ModelState.IsValid)
            {
                return this.View(model);
            }

            this.categoriesService.Create(model.Name, model.MinPrice, model.MaxPrice);
            this.TempData["Success"] = "Category added";
            return this.RedirectToAction("Index");
        }
Exemplo n.º 24
0
        public ActionResult Edit(CategoryEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            model.Category.Updated = DateTime.Now;
            model.Category.ByUser  = User.Identity.Name;
            _service.Update(model.Category);

            return(RedirectToAction("Index"));
        }
Exemplo n.º 25
0
        public async Task Edit_ValidModel()
        {
            var categoryController = CreateCategoryController();
            var model = new CategoryEditModel
            {
                DisplayName = "996",
                RouteName   = "996",
                Note        = "fubao"
            };

            var result = await categoryController.Edit(model);

            Assert.IsInstanceOf <OkObjectResult>(result);
        }
Exemplo n.º 26
0
        public void Category_CategoryEdit_ReturnsCategoryServiceUpdate()
        {
            var viewModel = new CategoryEditModel
            {
                Category = new CategoryDto()
                {
                    Name = "Test"
                }
            };

            var result = _controller.Edit(viewModel) as RedirectToRouteResult;

            _service.Received().Update(viewModel.Category);
        }
Exemplo n.º 27
0
        public void Category_CategoryEdit_ReturnsCategoryIndexView()
        {
            var viewModel = new CategoryEditModel
            {
                Category = new CategoryDto()
                {
                    Name = "Test"
                }
            };

            var result = _controller.Edit(viewModel) as RedirectToRouteResult;

            Assert.That(result.RouteValues["Action"], Is.EqualTo("Index"));
        }
Exemplo n.º 28
0
        public async Task<IActionResult> Edit(Guid id)
        {
            var cat = await _catService.Get(id);
            if (null == cat) return NotFound();

            var model = new CategoryEditModel
            {
                Id = cat.Id,
                DisplayName = cat.DisplayName,
                RouteName = cat.RouteName,
                Note = cat.Note
            };

            return Ok(model);
        }
Exemplo n.º 29
0
        private async Task <IActionResult> SaveAsync(CategoryEditModel categoryEditModel, string sender)
        {
            var returnValue = await _categoryService.Save(categoryEditModel);

            if (!returnValue.IsError)
            {
                TempData[MessageConstants.SuccessMessage] = returnValue.Message;
                return(RedirectToAction(nameof(CategoryController.Index)));
            }
            else
            {
                TempData[MessageConstants.DangerMessage] = returnValue.Message;
                return(RedirectToAction(sender));
            }
        }
Exemplo n.º 30
0
        public async Task Create_BadModelState()
        {
            var categoryController = CreateCategoryController();
            var model = new CategoryEditModel
            {
                DisplayName = "996",
                Note        = string.Empty
            };

            categoryController.ModelState.AddModelError("Note", "Note is required");

            var result = await categoryController.Create(model);

            Assert.IsInstanceOf <BadRequestObjectResult>(result);
        }
Exemplo n.º 31
0
        public Task PrepareCategoryAsync(Category category, CategoryEditModel model)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            category = _mapper.Map(model, category);

            return(Task.CompletedTask);
        }
Exemplo n.º 32
0
        public ActionResult Edit(CategoryEditModel model)
        {
            if (this.ModelState.IsValid)
            {
                var categoryName = this.categories
                    .GetAll()
                    .FirstOrDefault(x => x.Name.ToLower() == model.Name.ToLower());
                if (categoryName == null)
                {
                    var category = this.Mapper.Map<Category>(model);
                    this.categories.Update(model.Id, category);
                    TempData["Success"] = GlobalConstants.CategoryUpdateNotify;
                }
                else
                {
                    TempData["Warning"] = GlobalConstants.CategoryExistsNotify;
                }

                return this.Redirect("/Admin/Categories/Index");
            }

            return this.View(model);
        }
        public ActionResult Edit(CategoryEditModel model)
        {
            this.CheckPrices(model.MinPrice, model.MaxPrice);

            if (!this.ModelState.IsValid)
            {
                this.TempData["Error"] = "Wrong data";
                return this.View(model);
            }

            try
            {
                this.categoriesService.UpdateById(model.Id, model.Name, model.MinPrice, model.MaxPrice);
            }
            catch (Exception ex)
            {
                this.TempData["Error"] = ex.Message;
                return this.View(model);
            }

            this.TempData["Success"] = "Category updated";
            return this.RedirectToAction("Index");
        }