public async Task <IActionResult> Edit([Bind("Id,Title,Description,WebLink,ImageUrl,ImageUrlMobile,Importance,Active,SpaceId")] AdvertisementIM advertisement) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } try { if (advertisement.Id > 0) { var model = await _context.Advertisements.FirstOrDefaultAsync(d => d.Id == advertisement.Id); if (model == null) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } model = _mapper.Map(advertisement, model); model.UpdatedBy = User.Identity.Name; model.UpdatedDate = DateTime.Now; _context.Update(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Carousel)); } else { var model = _mapper.Map <Advertisement>(advertisement); model.CreatedBy = User.Identity.Name; model.CreatedDate = DateTime.Now; _context.Add(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Carousel)); } _cacheService.Invalidate("ADVERTISEMENT"); return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!AdvertisementExists(advertisement.Id)) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } else { throw; } } }
public async Task <ActionResult> EditRole(RoleIM role) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } if (role.Id > 0) { Role vRole = await _context.Roles.FindAsync(role.Id); if (!vRole.IsSys) { vRole.RoleName = role.RoleName; vRole.Description = role.Description; _context.Entry(vRole).State = EntityState.Modified; } else { AR.SetWarning("系统角色不可编辑"); return(Json(AR)); } } else { var vm = _mapper.Map <Role>(role); _context.Add(vm); } await _context.SaveChangesAsync(); AR.SetSuccess(String.Format(Messages.AlertCreateSuccess, EntityNames.Role)); return(Json(AR)); }
protected async Task CreatedUpdatedPageMetaAsync(QNZContext db, PageMeta pm) { var origin = await db.PageMetas.FirstOrDefaultAsync(d => d.ModuleType == pm.ModuleType && d.ObjectId == pm.ObjectId); if (origin != null) { if (string.IsNullOrEmpty(pm.Title) && string.IsNullOrEmpty(pm.Keywords) && string.IsNullOrEmpty(pm.Description)) { db.Remove(origin); } else { origin.Title = pm.Title; origin.Keywords = pm.Keywords; origin.Description = pm.Description; db.Entry(origin).State = EntityState.Modified; } } else { if (!string.IsNullOrEmpty(pm.Title) || !string.IsNullOrEmpty(pm.Keywords) || !string.IsNullOrEmpty(pm.Description)) { db.Add(pm); } } await db.SaveChangesAsync(); }
public void Create(Role role) { _db.Add(role); _db.SaveChanges(); // _unitOfWork.GetRepository<Role>().Insert(role); //_unitOfWork.SaveChanges(); }
public async Task <IActionResult> Edit([Bind("Id,Year,Month,Date,Description,Active")] MemorabiliaIM page) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } try { if (page.Id > 0) { var model = await _context.Memorabilia.SingleOrDefaultAsync(d => d.Id == page.Id); model = _mapper.Map(page, model); model.UpdatedDate = DateTime.Now; model.UpdatedBy = User.Identity.Name; _context.Update(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Memorabilia)); } else { var model = _mapper.Map <Memorabilium>(page); model.CreatedDate = DateTime.Now; model.CreatedBy = User.Identity.Name; _context.Add(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Memorabilia)); } return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!PageExists(page.Id)) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } else { throw; } } }
public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Sketch,Importance,Active,Code")] AdvertisingSpaceIM advertisingSpace) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } try { if (advertisingSpace.Id > 0) { var model = await _context.AdvertisingSpaces.FindAsync(advertisingSpace.Id); model = _mapper.Map(advertisingSpace, model); model.UpdatedDate = DateTime.Now; model.UpdatedBy = User.Identity.Name; _context.Update(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Position)); } else { var model = _mapper.Map <AdvertisingSpace>(advertisingSpace); model.CreatedDate = DateTime.Now; model.CreatedBy = User.Identity.Name; _context.Add(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Page)); } return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!AdvertisingSpaceExists(advertisingSpace.Id)) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } else { throw; } } }
public async Task <IActionResult> Create([Bind("Id,Title,Summary,Body,Author,Description,Keywords,Thumbnail,Pubdate,ViewCount,Active,CategoryId,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate")] Article article) { if (ModelState.IsValid) { _context.Add(article); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.ArticleCategories, "Id", "Alias", article.CategoryId); return(View(article)); }
public async Task <IActionResult> EditCategory([Bind("Id, Title, Importance, Active")] MenuCategoryIM im) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } var model = _mapper.Map <MenuCategory>(im); model.CreatedDate = DateTime.Now; model.CreatedBy = User.Identity.Name; model.IsSys = false; _context.Add(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.MenuCategory)); return(Json(AR)); }
//public User SignIn(string username, string password) //{ // var user = _unitOfWork.GetRepository<User>().GetFirstOrDefault(predicate:u=>u.UserName == username || u.Mobile == username); // if (user == null) return null; // var salt = Convert.FromBase64String(user.SecurityStamp); // var pwdHash = Hash.HashPasswordWithSalt(password, salt); // return user.PasswordHash == pwdHash ? user : null; //} public int CreateUser(string userName, string email, string password, string realName, string mobile) { if (IsExistEmail(email)) { return(1); //1 邮箱已存在 } if (IsExistUserName(userName)) { return(2); //1 用户名已存在 } if (IsExistMobile(mobile)) { return(3); //1 手机号已存在 } var securityStamp = Hash.GenerateSalt(); var passwordHash = Hash.HashPasswordWithSalt(password, securityStamp); var newUser = new User() { Id = IdentityGenerator.SequentialGuid(), UserName = userName, RealName = realName, Email = email, SecurityStamp = Convert.ToBase64String(securityStamp), PasswordHash = passwordHash, CreateDate = DateTime.Now, IsActive = true, Mobile = mobile }; //_logger.LogInformation(string.Format(Logs.CreateMessage, EntityNames.User, userName)); _db.Add(newUser); _db.SaveChanges(); //_unitOfWork.GetRepository<User>().Insert(newUser); //_unitOfWork.SaveChanges(); // SetUserCookies(false, newUser); return(0); }
public async Task <IActionResult> Edit(PhotoIM article) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } try { var pm = new PageMeta { Title = article.SEOTitle, Description = article.SEODescription, Keywords = article.SEOKeywords, ModuleType = (short)ModuleType.ARTICLE }; if (article.Id > 0) { var model = await _context.Photos.FirstOrDefaultAsync(d => d.Id == article.Id); if (model == null) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } model = _mapper.Map(article, model); model.UpdatedBy = User.Identity.Name; model.UpdatedDate = DateTime.Now; _context.Entry(model).State = EntityState.Modified; await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Photo)); pm.ObjectId = model.Id.ToString(); } else { var model = _mapper.Map <Photo>(article); model.CreatedBy = User.Identity.Name; model.CreatedDate = DateTime.Now; //model.Body = WebUtility.HtmlEncode(page.Body); _context.Add(model); await _context.SaveChangesAsync(); pm.ObjectId = model.Id.ToString(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Photo)); } _cacheService.Invalidate("PHOTO"); _cacheService.Invalidate("ALBUM"); await CreatedUpdatedPageMetaAsync(_context, pm); return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!PhotoExists(article.Id)) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } else { throw; } } }
public bool Create(Menu menu) { _db.Add(menu); _db.SaveChanges(); return(true); }
public async Task <IActionResult> Edit(DocumentIM article) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } try { //var pm = new PageMeta //{ // Title = article.SEOTitle, // Description = article.SEODescription, // Keywords = article.SEOKeywords, // ModuleType = (short)ModuleType.ARTICLE //}; string webRootPath = _hostingEnvironment.WebRootPath; if (article.Id > 0) { var model = await _context.Documents.FirstOrDefaultAsync(d => d.Id == article.Id); if (model == null) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } model = _mapper.Map(article, model); model.UpdatedBy = User.Identity.Name; model.UpdatedDate = DateTime.Now; string filePath = webRootPath + model.FileUrl; if (System.IO.File.Exists(filePath)) { var fz = await System.IO.File.ReadAllBytesAsync(filePath); model.FileSize = fz.Length; } _context.Entry(model).State = EntityState.Modified; await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Document)); // pm.ObjectId = model.Id.ToString(); } else { var model = _mapper.Map <Document>(article); model.CreatedBy = User.Identity.Name; model.CreatedDate = DateTime.Now; string filePath = webRootPath + model.FileUrl; if (System.IO.File.Exists(filePath)) { var fz = await System.IO.File.ReadAllBytesAsync(filePath); model.FileSize = fz.Length; } //model.Body = WebUtility.HtmlEncode(page.Body); _context.Add(model); await _context.SaveChangesAsync(); //pm.ObjectId = model.Id.ToString(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Document)); } //await CreatedUpdatedPageMetaAsync(_context, pm); return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!DocumentExists(article.Id)) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } else { throw; } } }
public async Task <IActionResult> Edit(VideoIM article) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } try { if (article.Id > 0) { var model = await _context.Videos.FirstOrDefaultAsync(d => d.Id == article.Id); if (model == null) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } model = _mapper.Map(article, model); model.UpdatedBy = User.Identity.Name; model.UpdatedDate = DateTime.Now; _context.Entry(model).State = EntityState.Modified; await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Video)); } else { var model = _mapper.Map <Video>(article); model.CreatedBy = User.Identity.Name; model.CreatedDate = DateTime.Now; //model.Body = WebUtility.HtmlEncode(page.Body); _context.Add(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Video)); } return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!VideoExists(article.Id)) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } else { throw; } } }
public async Task <IActionResult> Edit([Bind("Id,Title,Importance,Body,SeoName,Active,SEOTitle,SEOKeywords,SEODescription")] PageIM page) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } try { if (page.Id > 0) { var model = await _context.Pages.SingleOrDefaultAsync(d => d.Id == page.Id); model = _mapper.Map(page, model); model.SeoName = model.SeoName.ToLower(); model.Body = WebUtility.HtmlEncode(page.Body); model.UpdatedDate = DateTime.Now; if (User.Identity != null) { model.UpdatedBy = User.Identity.Name; } _context.Update(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Page)); } else { var model = _mapper.Map <Page>(page); model.SeoName = model.SeoName.ToLower(); model.Body = WebUtility.HtmlEncode(page.Body); model.CreatedDate = DateTime.Now; if (User.Identity != null) { model.CreatedBy = User.Identity.Name; } _context.Add(model); await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Page)); } var pm = new PageMeta { Title = page.SEOTitle, Description = page.SEODescription, Keywords = page.SEOKeywords, ModuleType = (short)ModuleType.PAGE, ObjectId = page.SeoName }; await CreatedUpdatedPageMetaAsync(_context, pm); return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!PageExists(page.Id)) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } else { throw; } } }
public async Task <IActionResult> Edit([Bind("Id,Title,ImageUrl,Alias,Description,Importance,Active,SEOTitle,SEOKeywords,SEODescription")] PostCategoryIM im, int id = 0) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } if (id == 0) { var model = _mapper.Map <PostCategory>(im); if (User.Identity != null) { model.CreatedBy = User.Identity.Name; } model.CreatedDate = DateTime.Now; _context.Add(model); await _context.SaveChangesAsync(); using (LogContext.PushProperty("LogEvent", Buttons.Add)) { Serilog.Log.Information("添加博客分类:[{title}]", model.Title); } AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.PostCategory)); return(Json(AR)); } if (id != im.Id) { AR.Setfailure("未发现此分类"); return(Json(AR)); } try { var model = await _context.PostCategories.FindAsync(id); model = _mapper.Map(im, model); if (User.Identity != null) { 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); using (LogContext.PushProperty("LogEvent", Buttons.Update)) { Serilog.Log.Information("修改博客分类:[{title}]", model.Title); } AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.PostCategory)); return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!PostCategoryExists(im.Id)) { AR.Setfailure("未发现此分类"); return(Json(AR)); } else { AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.PostCategory)); return(Json(AR)); } } }
public async Task <IActionResult> Edit([Bind("Id,Title,Alias,Description,Importance,Active,SEOTitle,SEOKeywords,SEODescription")] JobCategoryIM im, int id = 0) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } if (id == 0) { var model = _mapper.Map <JobCategory>(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.JobCategory)); return(Json(AR)); } if (id != im.Id) { AR.Setfailure("未发现此分类"); return(Json(AR)); } try { var model = await _context.JobCategories.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.JobCategory)); return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!JobCategoryExists(im.Id)) { AR.Setfailure("未发现此分类"); return(Json(AR)); } else { AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.JobCategory)); return(Json(AR)); } } }
public async Task <IActionResult> Edit([Bind("Id,Title,SubTitle,Thumbnail,ImageUrl,Body,Description,RelatedProducts,Products,Importance,Active,SEOTitle,SEOKeywords,SEODescription")] SolutionIM im, int id = 0) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } im.RelatedProducts = im.Products != null?string.Join("|", im.Products) : null;; if (id == 0) { var model = _mapper.Map <Solution>(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.Solution)); return(Json(AR)); } else { if (id != im.Id) { AR.Setfailure("未发现此分类"); return(Json(AR)); } try { var model = await _context.Solutions.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.SOLUTION, ObjectId = im.Id.ToString() }; await CreatedUpdatedPageMetaAsync(_context, pm); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Solution)); return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!SolutionExists(im.Id)) { AR.Setfailure("未发现此分类"); return(Json(AR)); } else { AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.Solution)); return(Json(AR)); } } } }