public static void SaveSingleWithUniquenessRelevanceID(this IEntityRepository <BusinessImage> BusinessImageRepository, BusinessImage file) { var pFile = BusinessImageRepository.GetSingleBy(x => x.RelevanceObjectID == file.RelevanceObjectID); if (pFile == null) { BusinessImageRepository.AddAndSave(file); } else { var filePath = file.UploadPath + file.ID + "_" + file.Name + file.UploadFileSuffix; _DeleteFile(filePath); BusinessImageRepository.DeleteAndSave(pFile); BusinessImageRepository.AddAndSave(file); } }
//[ValidateAntiForgeryToken]//防伪造脚本攻击,CSRF public ActionResult CreateOrEdit(ArtistViewModel model) { if (ModelState.IsValid) { Artist entity = new Artist { Id = model.Id, Name = model.Name, Description = model.Description, Tags = model.Tags, ViewCount = model.ViewCount, Collection = model.Collection }; if (model.Id != Guid.Empty) { _Service.Edit(entity); ViewBag.Message = "修改成功"; } else { entity.Id = Guid.NewGuid(); _Service.AddAndSave(entity); ViewBag.Message = "新增成功"; } return(RedirectToAction("Index")); } return(View(model)); }
/// <summary> /// 发送消息通知 /// </summary> /// <param name="notification">消息实体</param> public void SendNotification(Notification notification) { if (notification != null) { _notification.AddAndSave(notification); } }
/// <summary> /// 统一处理图片上传(添加) /// </summary> /// <param name="commodity"></param> private void AddCommodityImg(YZ_Commodity commodity) { try { var images = Request.Form.Files; var isCover = true; if (images != null) { foreach (var image in images) { var currImageName = image.FileName; var timeForFile = (DateTime.Now.ToString("yyyyMMddHHmmss") + "-").Trim(); string extensionName = currImageName.Substring(currImageName.LastIndexOf(".")); var imageName = ContentDispositionHeaderValue .Parse(image.ContentDisposition) .FileName .Trim('"') .Substring(image.FileName.LastIndexOf("\\") + 1); var newImageName = timeForFile + GetUser().Id + extensionName; var boPath = "../../images/UploadImages/" + ImageType.CommodityImgs.ToString() + "/" + newImageName; var imagePath = _hostingEnv.WebRootPath + $@"\images\UploadImages\{ImageType.CommodityImgs.ToString()}"; imageName = _hostingEnv.WebRootPath + $@"\images\UploadImages\{ImageType.CommodityImgs.ToString()}\{newImageName}"; Directory.CreateDirectory(imagePath); //创建目录 using (FileStream fs = System.IO.File.Create(imageName)) { image.CopyTo(fs); fs.Flush(); } var commodityImg = new BusinessImage { Name = string.Empty, DisplayName = string.Empty, OriginalFileName = string.Empty, Description = "这是闲置商品【" + commodity.Name + "】的图片", RelevanceObjectId = commodity.Id, UploaderId = Guid.Parse(GetUser().Id), UploadPath = boPath, PhysicalPath = imageName, FileSize = image.Length, Type = isCover ? ImageType.CommodityCover : ImageType.CommodityImgs }; _businessImage.AddAndSave(commodityImg); isCover = false; } } } catch (Exception) { } }
public async Task <JsonResult> SaveEditCommodity([Bind("Id,State,ExamineDescription")] YZ_CommodityVM boVM) { var commodity = _commodity.GetAllIncluding(x => x.AscriptionUser).FirstOrDefault(x => x.Id == boVM.Id); if (commodity != null) { commodity.State = boVM.State; } var examine = new YZ_CommodityExamine { Description = boVM.ExamineDescription, Commodity = commodity, ExamineTime = DateTime.Now, ExamineUser = GetUser(), State = boVM.State }; _commodityExamine.AddAndSave(examine); var result = await _commodity.AddOrEditAndSaveAsyn(commodity); if (result) { var message = ""; if (boVM.State == YZ_CommodityState.OnSale) { message = "尊敬的用户您好,您的商品 [ " + commodity.Name + " ] 已经审核并通过,已经自动上架。"; } else { message = "尊敬的用户您好,您的商品 [ " + commodity.Name + " ] 已经审核,但未通过已被驳回,原因是[" + boVM.ExamineDescription + "],请您修改后再次提交审核。"; } var notification = new Notification { Receiver = commodity.AscriptionUser, Name = "商品审核", Description = message, Link = "javascript:", IsAbnormal = false, IsRead = false, NotificationSource = NotificationSourceEnum.App }; AppNotification.SendNotification(notification); return(Json(new { result = true, message = "操作成功,请关闭当前页面!" })); } else { return(Json(new { result = false, message = "操作失败,请关闭当前页面!" })); } }
public ActionResult CreateOrEdit(AlbumViewModel model) { if (ModelState.IsValid) { Album entity = new Album(); var minDate = DateTime.Parse("1999-1-1"); var maxDate = DateTime.Now; if (model.IssueDate < minDate || model.IssueDate > maxDate) { model.IssueDate = maxDate; } entity.MapToModel(model); if (model.GenreID != Guid.Empty) { entity.Genre = _Service.GetSingleReleVvance <Genre>(model.GenreID); entity.GenreID = model.GenreID; } if (model.AlbumTypeID != Guid.Empty) { entity.AlbumType = _Service.GetSingleReleVvance <AlbumType>(model.AlbumTypeID); entity.AlbumTypeID = model.AlbumTypeID; } if (model.ArtistID != Guid.Empty) { entity.Artist = _Service.GetSingleReleVvance <Artist>(model.ArtistID); entity.ArtistID = model.ArtistID; } if (model.IssueDate == null) { entity.IssueDate = DateTime.Now; } if (model.ID != Guid.Empty) { _Service.Edit(entity); ViewBag.Messages = "修改成功"; } else { entity.ID = Guid.NewGuid(); _Service.AddAndSave(entity); ViewBag.Messages = "新增成功"; } return(RedirectToAction("Index")); } return(View(model)); }
//[ValidateAntiForgeryToken]//防伪造脚本攻击,CSRF public ActionResult CreateOrEdit(AlbumViewModel model) { if (ModelState.IsValid) { Album entity = new Album { Id = model.Id, Name = model.Name, Description = model.Description, Issuer = model.Issuer, IssueDate = model.IssueDate, UrlString = model.UrlString, Price = model.Price, Language = model.Language, Genre = model.Genre, Artist = model.Artist, AlbumType = model.AlbumType, GenreId = model.GenreId, ArtistId = model.ArtistId, AlbumTypeId = model.AlbumTypeId }; if (model.Id != Guid.Empty) { _Service.Edit(entity); ViewBag.Message = "修改成功"; } else { entity.Id = Guid.NewGuid(); _Service.AddAndSave(entity); ViewBag.Message = "新增成功"; } return(RedirectToAction("Index")); } return(View(model)); }
/// <summary> /// 结束请求 /// </summary> /// <param name="context"></param> public override void OnActionExecuted(ActionExecutedContext context) { //停止计时 Stopwatch stopwatch = (Stopwatch)context.HttpContext.Items[responseTimeKey]; responseTime = Math.Round((stopwatch.ElapsedMilliseconds / 1000.0), 2) + " 秒(s)"; var connection = context.HttpContext.Connection; var request = context.HttpContext.Request; var user = TzUser; var siteLog = new TzSiteLog { RequestType = request.Method, Url = request.Scheme + "://" + request.Host + request.Path + request?.QueryString, IpAddress = connection?.RemoteIpAddress.ToString(), ResponseTime = responseTime, User = user, UserNickname = user == null ? "游客" : user.Nickname }; _tzSiteLog.AddAndSave(siteLog); base.OnActionExecuted(context); }
/// <summary> /// 点赞实现 /// </summary> public JsonResult AddCTR(Guid id) { var result = false; var entity = _phlService.GetSingleById(id); if (entity != null) { entity.CTR += 1; _phlService.Edit(entity); result = true; } else { PopularHotList bo = new PopularHotList(); bo.Id = id; bo.Album = _phlService.GetSingleRelevance <Album>(id); bo.AlbumId = id; bo.CTR += 1; _phlService.AddAndSave(bo); result = true; } return(Json(result)); }
/// <summary> /// 添加搜索记录 /// </summary> /// <returns></returns> public void AddSearchLog(string keyword) { var userIp = ClientIpAddress; var searchLog = new YZ_UserSearchLog(); if (GetUser() != null) { searchLog = _userSearchLog.GetSingleBy(x => x.Name == keyword && x.UserIdOrIp == GetUser().Id); } else { searchLog = _userSearchLog.GetSingleBy(x => x.Name == keyword && x.UserIdOrIp == userIp); } if (searchLog == null) { searchLog = new YZ_UserSearchLog { Name = keyword, Description = string.Empty, UserIdOrIp = GetUser() == null ? userIp : GetUser().Id }; _userSearchLog.AddAndSave(searchLog); } }
public async Task TaskAdminAlbumCreate() { ///TaskAdminGenreCreate()方法会飘绿,因为该方法采用异步实现 ///方法会自动检测方法实现中有无await关键字 ///如果没有await,就会飘绿,方法就会按照同步实现 id = Guid.NewGuid(); //一组数据新增 var taskList = new List <Album>(); var albumType = new AlbumType() { ID = id, Name = "AlbumTypeName单元测试", Description = "Description单元测试" }; var artist = new Artist() { ID = id, Name = "Artist单元测试", Description = "Artist单元测试" }; var genre = new Genre() { ID = id, Name = "Genre单元测试", Description = "Genre单元测试" }; taskList.Add(new Album() { ID = id, Name = "AlbumName单元测试", Description = "Description单元测试", IssueDate = DateTime.Now, Issuer = "太平洋保险", LanguageClassification = "日本", Urlstring = "12345", Price = 0.0M, Genre = genre, Artist = artist, AlbumType = albumType }); var task = new Album() { ID = id, Name = "AlbumName单元测试", Description = "Description单元测试", IssueDate = DateTime.Now, Issuer = "太平洋保险", LanguageClassification = "日本", Urlstring = "12345", Price = 0.0M, Genre = genre, Artist = artist, AlbumType = albumType }; task.Genre.ID = id; task.Artist.ID = id; task.AlbumType.ID = id; _repository.AddAndSave(task); var result = _repository.GetSingleById(id); Assert.NotNull(result); }
public async Task <JsonResult> AddOrEditCommodity([Bind("Id,IsNew,Name,Description,Price,Unit,Stock,State,Way,Range,IsBargain,CategoryId")] YZ_CommodityVM boVM) { var commodity = new YZ_Commodity(); var category = _commodityCategory.GetAll().FirstOrDefault(x => x.Id == boVM.CategoryId); if (boVM.IsNew) { var lookCount = new YZ_CommodityLookCount { LookCount = 0 }; _commodityLookCount.AddAndSave(lookCount); commodity = new YZ_Commodity { Name = EncodeFilterHelper.EncodeHtml(boVM.Name), Description = EncodeFilterHelper.EncodeHtml(boVM.Description), Unit = EncodeFilterHelper.EncodeHtml(boVM.Unit), State = YZ_CommodityState.IsExamine, Category = category == null ? null : category, Price = boVM.Price, Stock = boVM.Stock, LookCount = lookCount, AscriptionUser = GetUser(), EditTime = DateTime.Now, IsBargain = boVM.IsBargain, Way = boVM.Way, Range = boVM.Range }; } else { commodity = await _commodity.GetSingleAsyn(boVM.Id); if (commodity != null) { commodity.Name = EncodeFilterHelper.EncodeHtml(boVM.Name); commodity.Description = EncodeFilterHelper.EncodeHtml(boVM.Description); commodity.Price = boVM.Price; commodity.Unit = EncodeFilterHelper.EncodeHtml(boVM.Unit); commodity.Stock = boVM.Stock; commodity.AddTime = commodity.AddTime; commodity.State = boVM.State; //调试时不修改商品状态 commodity.Category = category == null ? null : category; commodity.EditTime = DateTime.Now; commodity.IsBargain = boVM.IsBargain; commodity.Way = boVM.Way; commodity.Range = boVM.Range; } } //图片上传独立处理 AddCommodityImg(commodity); var result = await _commodity.AddOrEditAndSaveAsyn(commodity); if (result) { if (boVM.IsNew) { return(Json(new { result = true, message = "添加成功!" })); } else { return(Json(new { result = true, message = "修改成功!" })); } } else { return(Json(new { result = false, message = "操作失败!" })); } }
public async Task <IActionResult> SavaShoppingCart(Guid id) { try { var userValue = User.Claims.FirstOrDefault(); var user = await _UserManager.FindByIdAsync(userValue.Value.ToString()); if (user == null) { //return View("../../Views/Home/Logon"); Json(new { isOK = false, message = "请登录后再执行操作" }); } var shopcar = await _ShopCarRepository.FindByAsyn(x => x.BelongToUserID == user.Id); if (shopcar.Count() == 0) { var shopCar = new SK_WM_ShopCar() { BelongToUserID = user.Id, ShopCarForUser = user }; await _ShopCarRepository.AddOrEditAndSaveAsyn(shopCar); shopcar = await _ShopCarRepository.FindByAsyn(x => x.BelongToUserID == user.Id); } var goods = await _SK_WM_Goods.GetSingleAsyn(id, x => x.Shop, y => y.ShopForUser); var imgs = await _ImageRepository.FindByAsyn(x => x.RelevanceObjectID == goods.ID); var img = imgs.Where(x => x.IsForTitle == true).FirstOrDefault(); //查询该用户的购物车下的所有商品 var goodsItem = await _ShopCarGoodsItemRepository.FindByAsyn(x => x.BelongToShopCarID == shopcar.FirstOrDefault().ID.ToString()); //查询该用户购物车内的商品内是否有正要添加的商品 var hasGoods = goodsItem.Where(x => x.GoodsName == goods.Name); if (hasGoods.Count() == 0)//判断原购物车是否有要添加的商品、如果有就在原来的基础上修改数量及总价、否则直接添加新的 { var ShopCarGoodsItem = new SK_WM_ShopCarGoodsItem() { ShopName = goods.Shop.Name, GoodsName = goods.Name, Count = 1, Price = goods.Price, TotalPrice = goods.Price, BelongToShopCarID = shopcar.FirstOrDefault().ID.ToString(), shopCar = shopcar.FirstOrDefault(), GoodsID = goods.ID.ToString(), ImgPath = img.UploadPath, CreateOrderTime = DateTime.Now, }; _ShopCarGoodsItemRepository.AddAndSave(ShopCarGoodsItem); } else { var hItem = hasGoods.FirstOrDefault(); hItem.Count = 1 + hItem.Count; hItem.TotalPrice = (hItem.Count * decimal.Parse(goods.Price)).ToString(); _ShopCarGoodsItemRepository.AddOrEditAndSave(hItem); } return(Json(new { isOK = true, message = "添加购物车成功" })); } catch (Exception) { return(Json(new { isOK = false, message = "添加失败,请检查登录状态后再执行操作" })); } }