Exemplo n.º 1
0
        //DONE
        public ActionResult PostCategory(CreateCategoryVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var categoryToCreate = _mapper.Map <Category>(model);

                    var result = _categoryServices.AddCategory(categoryToCreate);

                    if (!result)
                    {
                        return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador"));
                    }

                    return(Json("La categoría ha sido agregada."));
                }

                return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState)));
            }
            catch (Exception ex)
            {
                //TODO: Log the exception
                return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador"));
            }
        }
Exemplo n.º 2
0
        public IActionResult Edit(int?id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (id == null)
            {
                return(View("Error"));
            }
            Category category  = _context.Category.Find(id);
            var      viewModel = new CreateCategoryVM
            {
                NameAZ = category.CategoryLangs.FirstOrDefault(x => x.Lang.Code.ToLower() == "az").Name,
                NameEN = category.CategoryLangs.FirstOrDefault(x => x.Lang.Code.ToLower() == "en").Name,
                NameRU = category.CategoryLangs.FirstOrDefault(x => x.Lang.Code.ToLower() == "ru").Name,
            };

            return(View(viewModel));
        }
 public async Task <IActionResult> Create(CreateCategoryVM vm)
 {
     if (vm.Name != "Choose")
     {
         await _userRepo.AddNewsCategoryAsync(
             User.Identity.Name, new NewsCategory { Name = vm.Name });
     }
     return(RedirectToAction("Manage", "Profile"));
 }
Exemplo n.º 4
0
        public async Task <IActionResult> EditPost(int id, CreateCategoryVM category)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Xaiş olunur düzgün doldurun.");
                return(View(category));
            }
            Category newCategory = await _context.Category.FindAsync(id);

            if (newCategory == null)
            {
                return(View("Error"));
            }

            if (category.Photo != null)
            {
                string computerPhoto = Path.Combine(_env.WebRootPath, "images", newCategory.PhotoUrl);

                if (System.IO.File.Exists(computerPhoto))
                {
                    System.IO.File.Delete(computerPhoto);
                }

                string fileName = await category.Photo.SaveAsync(_env.WebRootPath);

                newCategory.PhotoUrl = fileName;
            }
            CategoryLang azBlogLangFromDb = await _context.CategoryLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "az" &&
                                                                                             x.CategoryId == newCategory.Id);

            CategoryLang ruBlogLangFromDb = await _context.CategoryLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "ru" &&
                                                                                             x.CategoryId == newCategory.Id);

            CategoryLang enBlogLangFromDb = await _context.CategoryLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "en" &&
                                                                                             x.CategoryId == newCategory.Id);

            azBlogLangFromDb.Name = category.NameAZ;
            enBlogLangFromDb.Name = category.NameEN;
            ruBlogLangFromDb.Name = category.NameRU;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Create()
        {
            var newsCategories = _configuration.GetValue <string>("NewsCategories");
            var user           = await _userRepo.ReadAsync(User.Identity.Name);

            var userCategoryNames = user.NewsCategories.Select(nc => nc.Name).ToArray();
            var model             = new CreateCategoryVM
            {
                User           = user,
                NewsCategories = newsCategories.Split(',').Except(userCategoryNames).ToArray()
            };

            return(View(model));
        }
        public void InsertCategoryDuplicate()
        {
            var create = new CreateCategoryVM()
            {
                CategoryName = "name",
            };

            var service = new CategoryService(categoryRepoMock.Object, new CategoryValidator());

            categoryRepoMock.Setup(x => x.IsExistAsync(y => y.CategoryName == create.CategoryName)).ReturnsAsync(true);
            var result = service.InsertCategory(create);

            Assert.Equal("Duplicate, category already exist.", result.Exception.InnerException.Message);
        }
Exemplo n.º 7
0
        public async Task <Result <Category> > UpdateCategory(CreateCategoryVM categoryVM, int id)
        {
            var category = await FindAsync(id);

            if (category == null)
            {
                throw new MemeSiteException(HttpStatusCode.NotFound, "Category not found");
            }
            if (await IsExistAsync(m => m.CategoryName == categoryVM.CategoryName))
            {
                throw new MemeSiteException(HttpStatusCode.Conflict, "Duplicate, category already exist.");
            }
            category.CategoryName = categoryVM.CategoryName;
            return(await Update(category));
        }
Exemplo n.º 8
0
        public IActionResult Create(CreateCategoryVM model)
        {
            if (ModelState.IsValid)
            {
                var entity = _mapper.Map <Category>(model);
                entity.Image = new Image();
                _imageService.Upload(entity, model.File, EImageType.Category);
                _categoryRepository.Add(entity);
                _unitOfWork.Commit();

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
        public void InsertCategoryNotDuplicate()
        {
            var create = new CreateCategoryVM()
            {
                CategoryName = "name",
            };

            var service = new CategoryService(categoryRepoMock.Object, new CategoryValidator());

            categoryRepoMock.Setup(x => x.IsExistAsync(y => y.CategoryName == create.CategoryName)).ReturnsAsync(false);
            var result = service.InsertCategory(create);

            Assert.NotNull(result);
            Assert.True(result.Result.Succeeded);
            Assert.Equal("name", result.Result.Value.CategoryName);
        }
Exemplo n.º 10
0
        public async Task <Result <Category> > InsertCategory(CreateCategoryVM create)
        {
            Category entity = new Category()
            {
                CategoryName = create.CategoryName
            };
            var result = Validate(entity);

            if (await IsExistAsync(m => m.CategoryName == create.CategoryName))
            {
                throw new MemeSiteException(HttpStatusCode.Conflict, "Duplicate, category already exist.");
            }
            if (result.Succeeded)
            {
                result.Value = await _repository.InsertAsync(entity);
            }
            return(result);
        }
        public async Task <IActionResult> Create(CreateCategoryVM createCategoryVM)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            Category existCategory = _db.Categories.FirstOrDefault(c => c.Name.ToLower().Trim() == createCategoryVM.Name.ToLower().Trim());

            if (existCategory != null)
            {
                ModelState.AddModelError("Name", "Bu adda kateqoriya movcuddur!");
                return(View());
            }
            Category newCategory = new Category
            {
                Name = createCategoryVM.Name
            };
            await _db.Categories.AddAsync(newCategory);

            await _db.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 12
0
        public IActionResult Create(CreateCategoryVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var existingCategories = categoryService.GetAll();

            foreach (var existingCategory in existingCategories)
            {
                if (string.Equals(existingCategory.Title.ToLower(), model.Title.ToLower()))
                {
                    ModelState.AddModelError("", "You already have this category");
                    return(View(model));
                }
            }

            var category = mapper.Map <Category>(model);

            categoryService.Create(category);

            return(RedirectToAction(nameof(CategoryController.Index)));
        }
Exemplo n.º 13
0
        public IActionResult Create()
        {
            var model = new CreateCategoryVM();

            return(View(model));
        }
Exemplo n.º 14
0
        public async Task <IActionResult> UpdateCategory([FromBody] CreateCategoryVM category, int id)
        {
            Result <Category> result = await _categoryService.UpdateCategory(category, id);

            return(Ok(result.Value));
        }
Exemplo n.º 15
0
        public ActionResult Create()
        {
            CreateCategoryVM model = new CreateCategoryVM();

            return(Json(model, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 16
0
        public async Task <IActionResult> CreateCategory([FromBody] CreateCategoryVM createCategoryVM)
        {
            var result = await _categoryService.InsertCategory(createCategoryVM);

            return(Ok(result));
        }
Exemplo n.º 17
0
        public async Task <IActionResult> Create(CreateCategoryVM category)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (!ModelState.IsValid)
            {
                return(View(category));
            }

            if (category.Photo == null)
            {
                return(View(category));
            }

            string fileName = await category.Photo.SaveAsync(_env.WebRootPath);

            Lang azLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "az");

            Lang ruLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "ru");

            Lang enLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "en");

            Category newCategory = new Category()
            {
                PhotoUrl = fileName
            };

            await _context.Category.AddAsync(newCategory);

            await _context.SaveChangesAsync();

            CategoryLang historyAZ = new CategoryLang
            {
                Name       = category.NameAZ,
                LangId     = azLang.Id,
                CategoryId = newCategory.Id
            };
            CategoryLang historyEN = new CategoryLang
            {
                Name       = category.NameEN,
                LangId     = enLang.Id,
                CategoryId = newCategory.Id
            };
            CategoryLang historyRU = new CategoryLang
            {
                Name       = category.NameRU,
                LangId     = ruLang.Id,
                CategoryId = newCategory.Id
            };

            _context.CategoryLangs.AddRange(historyAZ, historyEN, historyRU);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 18
0
 public ActionResult Create(CreateCategoryVM model)
 {
     return(View());
 }