Beispiel #1
0
        // GET: Admin/ArticleCategories/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            var vm = new ArticleCategoryIM
            {
                Active     = true,
                Importance = 0
            };

            if (id == null)
            {
                return(View(vm));
            }

            var category = await _context.ArticleCategories.FindAsync(id);

            if (category == null)
            {
                return(NotFound());
            }
            var model = _mapper.Map <ArticleCategoryIM>(category);

            var pm = await _context.PageMetas.FirstOrDefaultAsync(d => d.ModuleType == (short)ModuleType.ARTICLECATEGORY && d.ObjectId == category.Alias);

            if (pm != null)
            {
                model.SEOTitle       = pm.Title;
                model.SEOKeywords    = pm.Keywords;
                model.SEODescription = pm.Description;
            }

            return(View(model));
        }
Beispiel #2
0
        public async Task <ActionResult> Edit(int?id)
        {
            if (id > 0)
            {
                var vCase = await _db.ArticleCategories.FindAsync(id);

                if (vCase == null)
                {
                    AR.Setfailure(Messages.HttpNotFound);
                    return(Json(AR, JsonRequestBehavior.AllowGet));
                }

                var editCase = _mapper.Map <ArticleCategory, ArticleCategoryIM>(vCase);

                var pageMeta = await _db.PageMetas.FirstOrDefaultAsync(d => d.ModelType == (short)ModelType.ARTICLECATEGORY && d.ObjectId == editCase.Id.ToString());

                if (pageMeta != null)
                {
                    editCase.SEOTitle       = pageMeta.Title;
                    editCase.Keywords       = pageMeta.Keyword;
                    editCase.SEODescription = pageMeta.Description;
                }

                return(View(editCase));
            }
            else
            {
                var vm = new ArticleCategoryIM
                {
                    Active     = true,
                    Importance = 0
                };
                return(View(vm));
            }
        }
Beispiel #3
0
        public async Task <JsonResult> Edit(ArticleCategoryIM vm)
        {
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return(Json(AR, JsonRequestBehavior.DenyGet));
            }

            try
            {
                if (vm.Id > 0)
                {
                    var editCase = await _db.ArticleCategories.FindAsync(vm.Id);

                    editCase             = _mapper.Map(vm, editCase);
                    editCase.UpdatedDate = DateTime.Now;
                    editCase.UpdatedBy   = Site.CurrentUserName;

                    _db.Entry(editCase).State = EntityState.Modified;
                    await _db.SaveChangesAsync();


                    await SetPageMetaAsync(_db, (short)ModelType.ARTICLECATEGORY, editCase.Id.ToString(), editCase.Title, vm.SEOTitle, vm.Keywords, vm.SEODescription);



                    AR.SetSuccess(String.Format(Messages.AlertUpdateSuccess, EntityNames.Category));
                    return(Json(AR, JsonRequestBehavior.DenyGet));
                }
                else
                {
                    var newCase = _mapper.Map <ArticleCategoryIM, ArticleCategory>(vm);
                    newCase.CreatedDate = DateTime.Now;
                    newCase.CreatedBy   = Site.CurrentUserName;

                    newCase = _db.ArticleCategories.Add(newCase);
                    var result = await _db.SaveChangesAsync();

                    if (result > 0)
                    {
                        await SetPageMetaAsync(_db, (short)ModelType.ARTICLECATEGORY, newCase.Id.ToString(), newCase.Title, vm.SEOTitle, vm.Keywords, vm.SEODescription);
                    }

                    AR.SetSuccess(String.Format(Messages.AlertCreateSuccess, EntityNames.Category));
                    return(Json(AR, JsonRequestBehavior.DenyGet));
                }
            }
            catch (Exception er)
            {
                AR.Setfailure(er.Message);
                return(Json(AR, JsonRequestBehavior.DenyGet));
            }
        }
Beispiel #4
0
        public async Task <IActionResult> PostArticleCategory([FromBody] ArticleCategoryIM articleCategory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var model = _mapper.Map <ArticleCategory>(articleCategory);

            _context.ArticleCategories.Add(model);
            await _context.SaveChangesAsync();

            return(Ok());  // CreatedAtAction("GetArticleCategory", new { id = articleCategory.Id }, articleCategory);
        }
Beispiel #5
0
        public async Task <IActionResult> PutArticleCategory([FromRoute] int id, [FromBody] ArticleCategoryIM articleCategory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != articleCategory.Id)
            {
                return(BadRequest());
            }

            var model = await _context.ArticleCategories.FirstOrDefaultAsync(d => d.Id == id);

            model = _mapper.Map(articleCategory, model);

            _context.Entry(model).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ArticleCategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
        }
Beispiel #6
0
        public async Task <IActionResult> Edit([Bind("Id,Title,ImageUrl,Alias,Description,Importance,Active,SEOTitle,SEOKeywords,SEODescription")] ArticleCategoryIM im, int id = 0)
        {
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return(Json(AR));
            }

            if (id == 0)
            {
                var model = _mapper.Map <ArticleCategory>(im);
                model.CreatedBy   = User.Identity.Name;
                model.CreatedDate = DateTime.Now;
                _context.Add(model);

                await _context.SaveChangesAsync();

                // return RedirectToAction(nameof(Index));

                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.ArticleCategory));
                return(Json(AR));
            }

            if (id != im.Id)
            {
                AR.Setfailure("未发现此分类");
                return(Json(AR));
            }


            try
            {
                var model = await _context.ArticleCategories.FindAsync(id);

                model = _mapper.Map(im, model);

                model.UpdatedBy   = User.Identity.Name;
                model.UpdatedDate = DateTime.Now;
                _context.Update(model);
                await _context.SaveChangesAsync();

                var pm = new PageMeta
                {
                    Title       = im.SEOTitle,
                    Description = im.SEODescription,
                    Keywords    = im.SEOKeywords,
                    ModuleType  = (short)ModuleType.ARTICLECATEGORY,
                    ObjectId    = im.Alias
                };

                await CreatedUpdatedPageMetaAsync(_context, pm);

                AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.ArticleCategory));
                return(Json(AR));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ArticleCategoryExists(im.Id))
                {
                    AR.Setfailure("未发现此分类");
                    return(Json(AR));
                }
                else
                {
                    AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.ArticleCategory));
                    return(Json(AR));
                }
            }
        }