Exemplo n.º 1
0
        public ActionResult Save(NewsCategory newsCategory)
        {
            if (!ModelState.IsValid)
            {
                var newsCategoryForm = new NewsCategoryForm
                {
                    Name = newsCategory.Name
                };

                return(View("Edit", newsCategoryForm));
            }

            var categoryInDb = _repository.FindById(newsCategory.Id);

            if (categoryInDb == null)
            {
                // New Category
                CreateNewCategoryContext(newsCategory);
                // await _repository.Add(newsCategory);
            }
            else
            {
                // Update Category
                categoryInDb.Name = newsCategory.Name;
                _repository.SaveChanges();
            }

            return(RedirectToAction("Index", "Categories"));
        }
Exemplo n.º 2
0
        public ActionResult New()
        {
            var category = new NewsCategoryForm
            {
                ViewMode = Enums.ViewMode.Add
            };

            return(View("Edit", category));
        }
Exemplo n.º 3
0
        public IActionResult Get(long id)
        {
            var category = _categoryRepository.Query().FirstOrDefault(x => x.Id == id);
            var model    = new NewsCategoryForm
            {
                Id          = category.Id,
                Name        = category.Name,
                IsPublished = category.IsPublished
            };

            return(Json(model));
        }
Exemplo n.º 4
0
        public IActionResult Put(long id, [FromBody] NewsCategoryForm model)
        {
            if (ModelState.IsValid)
            {
                var category = _categoryRepository.Query().FirstOrDefault(x => x.Id == id);
                category.Name        = model.Name;
                category.SeoTitle    = model.Slug;
                category.IsPublished = model.IsPublished;

                _categoryService.Update(category);
                return(Ok());
            }

            return(new BadRequestObjectResult(ModelState));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Put(long id, [FromBody] NewsCategoryForm model)
        {
            if (ModelState.IsValid)
            {
                var category = _categoryRepository.Query().FirstOrDefault(x => x.Id == id);
                category.Name        = model.Name;
                category.SeoTitle    = model.Slug;
                category.IsPublished = model.IsPublished;

                await _categoryService.Update(category);

                return(Accepted());
            }

            return(BadRequest(ModelState));
        }
Exemplo n.º 6
0
        public IActionResult Post([FromBody] NewsCategoryForm model)
        {
            if (ModelState.IsValid)
            {
                var category = new NewsCategory
                {
                    Name        = model.Name,
                    SeoTitle    = model.Slug,
                    IsPublished = model.IsPublished
                };

                _categoryService.Create(category);

                return(Ok());
            }
            return(new BadRequestObjectResult(ModelState));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Post([FromBody] NewsCategoryForm model)
        {
            if (ModelState.IsValid)
            {
                var category = new NewsCategory
                {
                    Name        = model.Name,
                    SeoTitle    = model.Slug,
                    IsPublished = model.IsPublished
                };

                await _categoryService.Create(category);

                return(CreatedAtAction(nameof(Get), new { id = category.Id }, null));
            }

            return(BadRequest(ModelState));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Get(long id)
        {
            var category = await _categoryRepository.Query().FirstOrDefaultAsync(x => x.Id == id);

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

            var model = new NewsCategoryForm
            {
                Id          = category.Id,
                Name        = category.Name,
                Slug        = category.SeoTitle,
                IsPublished = category.IsPublished
            };

            return(Json(model));
        }
Exemplo n.º 9
0
        public ActionResult Edit(int id)
        {
            var result = _repository.FindById(id);

            if (result == null)
            {
                return(HttpNotFound());
            }

            var category = Mapper.Map <NewsCategory, NewsCategoryDtoForUpdate>(result);

            var newsCategoryForm = new NewsCategoryForm
            {
                Name     = category.Name,
                ViewMode = Enums.ViewMode.Edit
            };

            ViewBag.CategoryId = result.Id;

            return(View(newsCategoryForm));
        }